views:

52

answers:

2

I am porting some legacy code from windows to Linux (Ubuntu Karmic to be precise).

I have come across a Win32 function GetDateFormat().

The statements I need to port over are called like this:

GetDateFormat(LOCALE_USER_DEFAULT, 0, &datetime, "MMMM", 'January', 31);

OR

GetDateFormat(LOCALE_USER_DEFAULT, 0, &datetime, "MMMM", 'May', 30);

Where datetime is a SYSTEMTIME struct.

Does anyone know where I can get the code for the function - or failing that, tips on how to "roll my own" equivalent function?

A: 

The Win32 GetDateFormat function should be equivalent to the strftime function in the time.h header.

+1  A: 

The Linux equivalent (actually, plain ANSI C) to a call to GetDateFormat like this:

GetDateFormat(LOCALE_USER_DEFAULT, 0, &datetime, "MMMM", date_str, len);

is:

char *old_lc_time;

/* Set LC_TIME locale to user default */
old_lc_time = setlocale(LC_TIME, NULL);
setlocale(LC_TIME, "");

strftime(date_str, len, "%B", &datetime);

/* Set LC_TIME locale back */
setlocale(LC_TIME, old_lc_time);

(where datetime is now a struct tm rather than a SYSTEMTIME)

You may not need to worry about setting the locale each time and setting it back - if you are happy for all of your date/time formatting to be done in the user default locale (which is usual), then you can just call setlocale(LC_TIME, ""); once at program startup and be done with it.

Note however that the values your code is passing to GetDateFormat in the lpDateStr and cchDate parameters (second-last and last respectively) do not make sense. 'January' is a character constant, when it should be a pointer to a buffer where GetDateFormat will place its result.

caf