Formatted Date and Time Strings

Glossary

One of the most useful sets of NLSAPI calls—GetDateFormat, GetTimeFormat, EnumDateFormats, EnumTimeFormats, and EnumCalendarInfo—returns formatted date and time picture strings. EnumDateFormats and EnumTimeFormats, as their names suggest, enumerate the date and time picture strings that the system carries for a particular locale. With these two functions, you can build a list of possible date and time strings to present to the user, such as the one in Figure 5-1 below.

Figure 5-1 The Date and Time dialog box from Microsoft Word 6 for Windows.

GetDateFormat and GetTimeFormat return a single string each. When you use GetDateFormat, your application can request a string containing the correct date in the default short-date format (DATE_SHORTDATE) or the default long-date format (DATE_LONGDATE) for a particular locale, as in the following sample code:

DWORD dwRC = ERROR_SUCCESS
LPTSTR pszDate = (LPTSTR) GlobalAlloc(GPTR, MEMSIZE(nLen + 1));

if ( !pszDate )
return (GetLastError());

int nLen = GetDateFormat(GetThreadLocale(), // Use this thread's
// locale.
DATE_LONGDATE, // Flags are passed
// into this function.
NULL, // Use current system
// time.
pszDateFormat, // format selected by
// user
pszDate, // used for formatted
// date
nLen); // used for date buffer
// length

if ( nLen == 0 )
{
dwRC = GetLastError();
GlobalFree(pszDate);
return (dwRC);
}

The short-date and long-date formats correspond to the two date formats displayed in the system Control Panel shown in Figure 5-6. Similarly, you can call GetTimeFormat to request a string containing the current time in one of several default formats. If you want to specify a different date and time, you can fill in a SYSTEMTIME structure, shown below, and pass it into a date and time formatting API.

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME

Although the system's default date and time formats are the most commonly used formats for each locale, your application is not limited to these choices. You can construct a picture string that can be passed to either GetDateFormat or GetTimeFormat. For example, on March 15, 1995, the system would interpret the picture string

"'This is 'dddd', day 'dd' of the month of 'MMMM'.'"

for an English-speaking locale as

This is Wednesday, day 15 of the month of March.

For a Spanish-speaking locale, the system would interpret the picture as

This is miercoles, day 15 of the month of marzo.

Windows replaces the dddd, dd, and MMMM combinations with system strings, leaving all letters inside the single quotes untouched. Of course, you could localize the picture string into Spanish so that it would make more sense to a Spanish speaker. Figure 5-2 below summarizes the syntax for customized date and time picture strings.

If you want to retrieve pieces of a date string, such as the localized names of the days of the week or months of the year, you can call the all-purpose function GetLocaleInfo. GetLocaleInfo takes a DWORD parameter called an LCTYPE, which specifies which piece of locale-related information the system should return. GetLocaleInfoW returns a null-terminated Unicode string, and GetLocaleInfoA returns a null-terminated string in the system's default local code page. Figure 5-3 below lists the LCTYPES associated with date and time. You can find descriptions of them in Appendix I.

Syntax Description
d Numeric representation of the day of the month, without leading zeros
   
dd Numeric representation of the day of the month, with leading zeros
   
ddd Three-letter abbreviation for the day of the week (SABBREVDAYNAME)
   
dddd Full name of the day of the week (SDAYNAME)
   
M Numeric representation of the month, without leading zeros
   
MM Numeric representation of the month, with leading zeros
   
MMM Three-letter abbreviation for the name of the month (SABBREVMONTHNAME)
   
MMMM Full name of the month (SMONTHNAME)
   
y Year represented by only the last two digits, without leading zeros if less than 10
   
yy Year represented by only the last two digits
   
yyyy Year represented by all four digits
   
gg Period/era string, found in the CALTYPE CAL_SERASTRING
   
h Hours without leading zeros (12-hour clock)
   
hh Hours with leading zeros (12-hour clock)
   
H Hours without leading zeros (24-hour clock)
   
HH Hours with leading zeros (24-hour clock)
   
m Minutes without leading zeros
   
mm Minutes with leading zeros
   
s Seconds without leading zeros
   
ss Seconds with leading zeros
   
t One-character time-marker string (for example, "a" and "p")
   
tt Multicharacter time-marker string (for example, "AM" and "PM")


Figure 5-2 Date and time picture string elements.

Some languages, such as Finnish, German, Polish, and Russian, have several noun forms. If you plan to use localized names provided by the system, have a linguist check to make sure you are using them in the right context. Windows carries both the nominative and genitive forms of Polish and Russian month names; the form changes depending on the month name's position in the string relative to the day name. Windows returns both forms in a single string separated by a null value. The system carries only one form of each month name or day name for all other languages.

LOCALE_ICENTURY LOCALE_SDATE
LOCALE_IDATE LOCALE_SDAYNAME1
LOCALE_IDAYLZERO LOCALE_SDAYNAME2
LOCALE_ILDATE LOCALE_SDAYNAME3
LOCALE_IMONLZERO LOCALE_SDAYNAME4
LOCALE_ITIME LOCALE_SDAYNAME5
LOCALE_ITIMEMARKPOSN LOCALE_SDAYNAME6
LOCALE_ITLZERO LOCALE_SDAYNAME7
LOCALE_S1159 LOCALE_SLONGDATE
LOCALE_S2359 LOCALE_SMONTHNAME1
LOCALE_SABBREVDAYNAME1 LOCALE_SMONTHNAME2
LOCALE_SABBREVDAYNAME2 LOCALE_SMONTHNAME3
LOCALE_SABBREVDAYNAME3 LOCALE_SMONTHNAME4
LOCALE_SABBREVDAYNAME4 LOCALE_SMONTHNAME5
LOCALE_SABBREVDAYNAME5 LOCALE_SMONTHNAME6
LOCALE_SABBREVDAYNAME6 LOCALE_SMONTHNAME7
LOCALE_SABBREVDAYNAME7 LOCALE_SMONTHNAME8
LOCALE_SABBREVMONTHNAME1 LOCALE_SMONTHNAME9
LOCALE_SABBREVMONTHNAME2 LOCALE_SMONTHNAME10
LOCALE_SABBREVMONTHNAME3 LOCALE_SMONTHNAME11
LOCALE_SABBREVMONTHNAME4 LOCALE_SMONTHNAME12
LOCALE_SABBREVMONTHNAME5 LOCALE_SMONTHNAME13
LOCALE_SABBREVMONTHNAME6 LOCALE_SSHORTDATE
LOCALE_SABBREVMONTHNAME7 LOCALE_STIME
LOCALE_SABBREVMONTHNAME8 LOCALE_STIMEFORMAT
LOCALE_SABBREVMONTHNAME9  
LOCALE_SABBREVMONTHNAME10  
LOCALE_SABBREVMONTHNAME11  
LOCALE_SABBREVMONTHNAME12  
LOCALE_SABBREVMONTHNAME13  


Figure 5-3 LCTYPEs for date and time. See Appendix I for full descriptions. The 13th month is for lunar calendars.