by Michael S. Kaplan, published on 2006/07/21 09:42 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/07/21/673813.aspx
Walter asks:
Hi,
It seems the time format in Control Panel Regional and Language Options is only mapped to .NET DateTime.ToString(“T”), i.e. the DateTimeFormatInfo.LongTimePattern.
When using Thread.CurrentThread.CurrentCulture.DateTimeFormat, the LongTimePattern will be changed according to the Control Panel, while the ShortTimePattern is not changed.
But I noticed that the Taskbar’s clock in the notification area (systray) will be changed according to the Control Panel, while keeping the Short Time format. Is there any way to do this conversion in code?
BTW, the Short Date and Long Date in Regional and Language Options map to .NET DateTimeFormatInfo’s ShortDatePattern and LongDatePattern perfectly.
Thanks.
Regards,
Walter
Technically it is not the systray, but I won't quibble; I have Raymond Chen to quibble for me. :-)
Some may immediately think of when Ivan Petrov was asking about Customizing the SHORT time format at the end of last year.
But this question is a little different, since Walter is noticing that there is an obviously unmanaged component that seems to be following the current locale yet also seems to clearly be a short time. Whassup with that?
Well, the secret is in the GetTimeFormat function, to which you can pass any of the following dwFlags values:
Value
Meaning
LOCALE_NOUSEROVERRIDE
Format the string using the system default time format for the specified locale. If this flag is not set, the function formats the string using any user overrides to the default time format for the locale. This flag can only be set if lpFormat is set to a null pointer.
LOCALE_USE_CP_ACP
Use the operating system ANSI code page instead of the locale code page for string translation. See Code Page Identifiers for a list of code pages.
TIME_NOMINUTESORSECONDS
Do not use minutes or seconds.
TIME_NOSECONDS
Do not use seconds.
TIME_NOTIMEMARKER
Donot use a time marker.
TIME_FORCE24HOURFORMAT
Always use a 24-hour time format.
If you use that TIME_NOSECONDS flag I marked in bold, you will essentially get a formatted short time, even though there is no short time pattern available via GetLocaleInfo.
As the GetTimeFormat documentation states, the separator will be removed when the seconds are. :-)
You can also use TIME_NOTIMEMARKER if you'd like here, though in the case of the time value that Walter was thinking about, the AM/PM time marker will usually be there unless the locale uses a 24-hour clock.
So it is not so hard to make your own short times that match the current locale....
This post brought to you by : (U+003a, a.ka. COLON)
referenced by