[Reprinted] Delphi obtains and sets the system time format (long date and short date)

Source: Internet
Author: User
Tags time and date

Delphi obtains and sets the system time format

In Delphi, formatdatetime is often used to convert the date and time in tdatetime format into a string to display or save the value, or use strtodatetime to convert the date and time in the string form to tdatetime and then perform other operations.
The time or date format currently set by the system is used for time or date conversion. If the time and date formats do not match the representation in the string, the conversion process will fail. For example, if the current short date format is set to 'yyyy/MM/dd', and the string to '2017-10-20 'is a date, an error is returned, this is not a valid date.
Therefore, before conversion, make sure that the system's date and time format is consistent with that used in the program. One way is to set it by the installer during installation, or explicitly tell the user to set it to the required format. The other is that the program is automatically set to the desired format when it is started, and the original settings are restored when it exits.

Two windows APIs are required. One isGetlocaleinfo, One isSetlocaleinfo.

API query description:
Int getlocaleinfo (
Lcid locale, // locale identifier, set the information range, system level, or current user
Lctype, // type of information, set the information type
Lptstr lplcdata, // address of buffer for information, set to value, must be transmitted in pchar format
Int cchdata // size of Buffer
);

Bool setlocaleinfo (
Lcid locale, // locale identifier, same as above
Lctype, // type of information to set
Lpctstr lplcdata // pointer to information to set
);

The parameters of the two functions are similar. locale can take two values: locale_system_default, which indicates that the default settings of the system are to be operated; locale_user_default indicates that the settings of the current user are to be operated. You can also use other values to modify the settings of a language region. Generally, locale_user_default is used.
Lctype indicates the type of the information to be operated, that is, the setting to be operated. There are a lot of available values. There is a large list of Delphi's online help or msdn query. However, there are several issues related to the date and time formats:
Locale_s1_date: Short Date Format
Locale_slongdate: long Date Format
Locale_stimeformat: Time Format
In addition, locale_stime indicates the time separator and locale_sdate indicates the date separator. It can be used independently, or modified together with locale_s1_date and locale_stimeformat. Therefore, you do not need to perform this operation independently.
When getlocaleinfo or setlocaleinfo is used, only one type can be operated at a time. For example, to set the short date format, long date format, and time format at the same time, you must use three types of codes to call setlocaleinfo three times.
It is also worth noting that after the date and time format is set, the wm_settingchange message should be sent to the activation window broadcast of the current system. Otherwise, the program still cannot be used even if it is modified.
The following is a piece of program for obtaining and setting.

1. // obtain the time and date format

Procedure getdatetimeformat ();
VaR
Buf: pchar;
I: integer;
Gprev1_date, gprevlongdate, gprevtimeformat: string;
Begin
Getmem (BUF, 100 );
I: = 100; // I must be assigned the length of the Buf buffer before calling. If it is set to 0 or a negative value, the value cannot be set.
Getlocaleinfo (locale_user_default, locale_s1_date, Buf, I); // get the current user setting, short date format.
Gprev1_date: = string (BUF );
I: = 100;
Getlocaleinfo (locale_user_default, locale_slongdate, Buf, I); // obtain the long Date Format
Gprevlongdate: = string (BUF );
I: = 100;
Getlocaleinfo (locale_user_default, locale_stimeformat, Buf, I); // obtain the time format
Gprevtimeformat: = string (BUF );
Freemem (BUF );
End;
2. // set the time format

Procedure setdatetimeformat ();
VaR
P: DWORD;
Begin
Setlocaleinfo (locale_user_default, locale_s1_date, pchar ('yyyy-mm-dd'); // short date
Setlocaleinfo (locale_user_default, locale_slongdate, pchar ('yyyy'-'M'-'-'D '''));
Setlocaleinfo (locale_user_default, locale_stimeformat, pchar ('H: mm: ss'); // set the time
Sendmessagetimeout (hwnd_broadcast, wm_settingchange, 0, smto_abortifhung, 10, P );
// It must be called after the setting is complete to notify other programs that the format has been changed. Otherwise, even the program itself cannot use the new format.
End;

Call getdatetimeformat during program initialization, save the retrieved settings, and use setdatetimeformat to set the required format. Call setdatetimeformat again when the program exits to write the saved value back.
Of course, if the program is already running and other programs change the format as needed, there is no way to do it. At the same time, only one program can be run, otherwise it will interfere with each other. If you have to find a solution, you may be able to process the wm_settingchange message, and then change the change back? Some robbers

 

Http://hi.baidu.com/northarcher/blog/item/8399e5f3b8543840342acc1a.html

Related Article

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.