Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to format date and time independent of locale

In these code fragments, all output is purely numerical, date and time separators and formatting is fixed and locale independent.

To display the universal time and current local time implement code as follows:


TBuf<256> buffer;

    // Time in microseconds since 0 AD nominal Gregorian
TTime time;

    // year-month-day-hour-minute-second-microsecond
TDateTime dateTime;

    // Set and print Universal date/time
    // Get Universal time (= GMT)
time.UniversalTime();

    // Convert to fields
dateTime=time.DateTime();

    // Format universal time numerically
formatDateTime(buffer,dateTime);

    // Get current local time, taking daylight saving
    // into account, if in effect
time.HomeTime();

    // Convert to fields
dateTime=time.DateTime();

    // Format current local date/time numerically
formatDateTime(buffer,dateTime);

Here, the formatting is handled by the formatDateTime() function. This is implemented:

LOCAL_C void formatDateTime(TDes& aBuffer,TDateTime aDateTime)
    {
    _LIT(KFormatTxt,"%d %d %d %d:%d:%d.%d\n");
    aBuffer.Format(KFormatTxt,
                   aDateTime.Year(),
                   TInt(aDateTime.Month()+1),
                       // Format the month as a TInt to preserve locale independence
                   aDateTime.Day()+1,
                       // Day and month ranges begin at zero (0-30 and 0-11),
                       // so add one when formatting
                   aDateTime.Hour(), aDateTime.Minute(), aDateTime.Second(),
                   aDateTime.MicroSecond()
                  );
    {


Notes