Locale aware date formatting and strftime()

3 posts / 0 new
Last post
Hans
Hans's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 2010-12-09 22:04
Locale aware date formatting and strftime()

The strftime() documentation says that formatting strings such as "%x" should follow the date formatting for the current locale. However, in my tests it always prints the date in US format mm/dd/yy. Using setlocale() does nothing, and locale prefs are ignored.

Does AmigaOS have a standard way of always getting the date formatting used by the current locale? Locale.library's FormatDate() still requires you to manually choose the date format, so that's not the answer. Is there a way to get a date format string for the user's locale choice?

Date formatting is a pretty common and basic task, so the OS really should be providing an easy way to get it right for the user's locale settings.

Hans

salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
Re: Locale aware date formatting and strftime()

IDOS->DateToStr() with dat_Format set to FORMAT_DEF does that.

  1. TEXT datebuf[LEN_DATSTRING];
  2. TEXT timebuf[LEN_DATSTRING];
  3.  
  4. datetime.dat_Stamp = datestamp;
  5. datetime.dat_Format = FORMAT_DEF;
  6. datetime.dat_Flags = 0;
  7. datetime.dat_StrDay = NULL;
  8. datetime.dat_StrDate = datebuf;
  9. datetime.dat_StrTime = timebuf;
  10.  
  11. IDOS->DateToStr(&datetime);

If you don't need the time you can set dat_StrTime to NULL.

broadblues
broadblues's picture
Offline
Last seen: 4 years 9 months ago
Joined: 2012-05-02 21:48
Re: Locale aware date formatting and strftime()

Locale.library's FormatDate() still requires you to manually choose the date format, so that's not the answer.

It's half the answer though the other half is the following entries in struct Locale

  1. STRPTR loc_DateTimeFormat; /* regular date & time format */
  2. STRPTR loc_DateFormat; /* date format by itself */
  3. STRPTR loc_TimeFormat; /* time format by itself */
  4.  
  5. STRPTR loc_ShortDateTimeFormat; /* short date & time format */
  6. STRPTR loc_ShortDateFormat; /* short date format by itself */
  7. STRPTR loc_ShortTimeFormat; /* short time format by itself */
Log in or register to post comments