Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to get a LOCALE_SSHORTDATE that is guaranteed to have a full (4-digit) year number?

Tags:

c

locale

winapi

I want to create a date-time picker control that shows both date and time, as a combination of the DTS_SHORTDATECENTURYFORMAT and DTS_TIMEFORMAT styles. Since there is no such style built into the date-time picker, I have to do it myself with GetLocaleInfoEx().

I notice that by default, GetLocaleInfoEx(LOCALE_SSHORTDATE) seems to return a four-digit year, which is what I want. Is this reliable? It's not documented anywhere, only that there can be more than one string for LOCALE_SSHORTDATE. If I have to enumerate the possible strings, how would I know which one is the one I want? Do I just look for the first string to contain yyyy? That doesn't sound reliable to me, at least not on its own...

Thanks.

like image 527
andlabs Avatar asked Dec 05 '25 21:12

andlabs


2 Answers

No you can't rely on LOCALE_SSHORTDATE having a four-digit year, since the user is able to configure this through the Region control panel and options with two-digit years are available.

If you want to enforce a four digit year (and override the user's preference) you may as well just provide your own short date string. Alternatively, you could scan the user's string and change "yy" to "yyyy".

like image 67
Jonathan Potter Avatar answered Dec 08 '25 14:12

Jonathan Potter


You can also use GetDateFormat and GetTimeFormat in combination with LOCALE_SSHORTDATE etc.

wchar_t buf[100];

SYSTEMTIME st;
GetSystemTime(&st);
GetDateFormat(0, DATE_LONGDATE, &st, 0, buf, 100);
GetDateFormat(0, DATE_SHORTDATE, &st, 0, buf, 100);
GetTimeFormat(0, 0, &st, 0, buf, 100);
GetTimeFormat(0, TIME_NOSECONDS, &st, 0, buf, 100);

Then maybe you don't have to worry about how the formatting is done. Year is sometimes set to yy even for long date format. For example in ControlPanel->Region->Format: switch to French language, it allows different options than US English.

like image 43
Barmak Shemirani Avatar answered Dec 08 '25 13:12

Barmak Shemirani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!