| | | | Browse by category |
This article addresses the use of the ISO 8601 standard when converting a LEIF::DateTime to a string. The standard permits the inclusion of fractions of seconds in addition to hours, minutes and seconds in the representation of time. The basic form for time is: HHMMSS,ss or HH:MM:SS.ss where HH is hours, MM is minutes, SS is seconds and ss is the decimal fraction of a second. ISO 8601 specifies that the character separating the seconds and the fractional seconds may be either a comma or a period. When a LEIF::DateTime is converted to a string using the asString( ) method the string will use a comma as the separator.
Action
LEIF provides three overloaded versions of the asString( ) method for DateTime objects. Two of these methods are passed a format string for converting the DateTime to a string. The third method is passed a Format enum value. The format enum is defined as:
enum Format { iso8601, http };
The code below would display an ISO 8601 representation of the current date and time:
#include <rw/leif/core/DateTime.h>
LEIF::DateTime dt(LEIF::DateTime::setCurrentTime);
std::cout << "Current time is: " << dt.asString(LEIF::DateTime::iso8601).data() << std::endl;
The resulting string has a comma as the separator:
Current time is: 2005-10-24T14:29:08,875-
The DateTime constructor will accept either separator. So the following statements will result in the same data being stored in each object.
LEIF::DateTime dtPeriod("2001-12-24T01:12:30.90", LEIF::DateTime::iso8601);
LEIF::DateTime dtComma("2001-12-24T01:
If dtPeriod and dtComma are both coverted back to strings using asString( ) with the Format LEIF::DateTime::iso8601 both will result in equivalent strings with the comma.
Resulting string: 2001-12-24T01:12:30,090-
A complete example is available to download: click here to download
Reference:
LEIF Core Library Reference Guide ? LEIF::DateTime & LEIF::Locale
Essential Tools Module User's Guide ? 3.3 International Standards for Dates and Times
W3 Schools http://www.w3.org/TR/NOTE-datetime