Delphi gets and sets the system time format, i.e. GetLocaleInfo and SetLocaleInfo

Source: Internet
Author: User
Tags time and date

In Delphi, especially when writing management system software, it is often necessary to use the FormatDateTime to display or save the date time of the Tdatetime format into a string, or to convert the DateTime in string form to a value of strtodatetime. Tdatetime and then do other things.

When a time or date conversion is made, the time and date format currently set by the system is used. If the time-date format does not match the representation in the string, the conversion process will fail. For example, the current short date format is set to ' Yyyy/mm/dd ', and the string to be converted to ' 2006-10-20 ' is a date, which will cause an error, saying this is not a valid date.
So before converting, make sure that the date and time format of the system matches the usage in the program. One way is to set it up at installation time, or explicitly tell the user that it must be formatted as required. The other is the program at startup, automatically set to the desired format, exit the program and then restore the original settings.


There are two Windows APIs that need to be used, one is getlocaleinfo and the other is setlocaleinfo.

Check API Description:
int GetLocaleInfo (
LCID locale,//locale identifier, set the scope of the information, whether the system level, or the current user
Lctype Lctype,//type of information, setting types of information
LPTSTR Lplcdata,//address of buffer for information, set to a value that must be passed in Pchar format
int Cchdata//size of buffer
);
BOOL SetLocaleInfo (
LCID locale,//locale identifier, ibid.
Lctype Lctype,//type of information to set
LPCTSTR lplcdata//Pointer to information to set
);
The parameters of the two functions are similar, where locale can take two values: Locale_system_default, which indicates that the system default settings are to be manipulated; Locale_user_default, which indicates that the current user's settings are to be manipulated. You can also take other values that specifically modify the settings for a locale. Generally take locale_user_default.
A lctype that represents the type of information to manipulate, that is, which setting to manipulate. There are a lot of available values, Delphi's online Help, or check MSDN, all with a large list. But these are the ones associated with the DateTime format:
Locale_sshortdate: Short Date format
Locale_slongdate: Long Date format
Locale_stimeformat: Time Format
Another locale_stime represents a time separator, and locale_sdate represents a date separator. Can be used alone, or can be modified with locale_sshortdate and Locale_stimeformat, so there is no need to operate separately.
When using GetLocaleInfo or setlocaleinfo, only one type can be manipulated at a time. For example, to set a short date format, long date format, time format, you must use three type code number three times setlocaleinfo.
Another problem to note is that after you set the DateTime format, you should broadcast the WM_SETTINGCHANGE message to the active window of the current system, or the program will not work even if it is modified.
The following is a fragment of the program that gets and sets.

1.//Get Time date format
Procedure Getdatetimeformat ();
var
Buf:pchar;
I:integer;
Gprevshortdate,gprevlongdate,gprevtimeformat:string;
Begin
Getmem (buf,100);
i:=100; I must assign a value to the length of the BUF buffer before the call. If set to 0 or negative, the set value of
GetLocaleInfo (locale_user_default,locale_sshortdate,buf,i) is not taken, and the current user setting, Short date format, is taken.
Gprevshortdate:=string (BUF);
I:=100;
GetLocaleInfo (locale_user_default,locale_slongdate,buf,i);//take long date format
gprevlongdate:=string (BUF);
i:=100;
GetLocaleInfo (locale_user_default,locale_stimeformat,buf,i);//Take time format
gprevtimeformat:=string (BUF);
Freemem (BUF);
End;

2.//Setting the time format
Procedure Setdatetimeformat ();
Var
P:dword;
Begin
SetLocaleInfo (Locale_user_default,locale_sshortdate,pchar (' yyyy-mm-dd ')); Short Date
SetLocaleInfo (Locale_user_default,locale_slongdate,pchar (' yyyy ' year ' m ' ' d ' '));
SetLocaleInfo (Locale_user_default,locale_stimeformat,pchar (' h:mm:ss ')); Set the time
SendMessageTimeout (HWND_BROADCAST,WM_SETTINGCHANGE,0,0,SMTO_ABORTIFHUNG,10,P);
Must be called when the setting is complete, notifying other program formats that the format has changed, or even the program itself cannot use the newly formatted format
End
Call Getdatetimeformat at program initialization, save the removed settings, and then set the desired format with Setdatetimeformat. Call Setdatetimeformat again when the program exits to write back the saved values.
Of course, if the program

Delphi gets and sets the system time format, i.e. GetLocaleInfo and SetLocaleInfo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.