Time and Date Functions
General Advice
In general, you should store dates and times using ISO 8601 format and the UTC time zone.
You should display dates and times using the user's locale, usually in the user's time zone.
Dates
Different locales represent dates differently. For example, in
the United States, a valid date may be 12/6/2002 , which
is represented in England as 6/12/2002 . Additionally,
days of the week have different spellings in non-US locales, e.g.
Friday corresponds with Freitag in German. As
a rule of thumb, dates should be stored in a neutral and consistent
manner throughout the application. Locale-specific date formatting
should be applied as late in the UI construction process as possible.
C Example
The following example illustrates how to format a short date for
the fr_FR locale.
/* Parse a date with short French date/time formatter */
UDateFormat* df = udat_open(UDAT_SHORT, UDAT_SHORT, "fr_FR", "GMT", &status);
UErrorCode status = U_ZERO_ERROR;
int32_t parsepos=0;
UDate myDate = udat_parse(df, myString, u_strlen(myString), &parsepos, &status);
Times
Times also vary by locale. For example, in the United States, a
valid time may be 2:45 PM, which corresponds with 14:45 in Germany.
As a rule of thumb, times should be stored in a neutral and consistent
manner throughout the application. Locale-specific time formatting
should be applied as late in the UI construction process as possible.
Where possible, a time-zone code or offset should be retained as
well.
Time Formatting in ICU
A general discussion of ICU's locale-specific time formatting
can be found at ICU
Time Formatting. ICU also provides support for dealing with
multiple time zones at ICU
Time Zones.
Time Formatting in Java
The java.text.TimeZone class is used in conjunction
with the DateFormat class to tell how the time zone
should be interpreted.
Calendar
International calendar issues include:
- The type of calendar, e.g. Gregorian, Julian, Hijri, lunar.
For business applications it is acceptable to use the Gregorian
calendar.
- The first day of the week, e.g. Sunday is the first day of the
week in the USA, but Monday is the first day of the week in most
European countries.
- Workdays and working hours are not supported by any package
and must be determined by someone familiar with local customs.
For example, workdays in the US differ for different industries.
Java and the IBM ICU package contain calendar class support for
the Gregorian calendar. Constructors are available for instantiating
calendar objects that are time zone and locale specific:
Calendar(TimeZone zone, Locale aLocale)
Visual C++ Example
In Visual C++ time formatting is accomplished by the setlocale()
function:
wchar_t fr_FR L'French_France';
_wsetlocale(LC_ALL,fr_FR);
_wsetlocale(LC_TIME,fr_FR);
This code will set the locale to fr_FR . The first
call to _wsetlocale() sets all locale information to
fr_FR , while the second sets only the time locale information
to fr_FR . Subsequent calls to wcsftime
will return locale-sensitive time information. Subsequent calls
to wprintf() will display date and time information
in a manner appropriate to setlocale information.
Java, C and C++
Java, the IBM ICU package, Visual C++, SQL Server 2000, Oracle
8Ii, and IIS support locale-sensitive date and time displays. The
Java locale example provides
more information.
Database Support for Date/Time
Databases support date and time displays in many international formats.
Click
here
for information on SQL date and time formatting.
Click
here
for information on Oracle date and time formatting.
As mentioned previously, it is advisable to format dates and
times in the GUI.
|