| ID | CS000847 | Creation date | March 7, 2008 |
| Platform | S60 3rd Edition S60 3rd Edition, FP1 S60 3rd Edition, FP2 Beta | Tested on devices | Nokia E61i Nokia E90 Communicator Nokia N95 8GB Nokia 6220 Classic |
| Category | Symbian C++ | Subcategory | Base/System |
| Keywords (APIs, classes, methods, functions): TTime, RTz, CTzConverter, CTzId,TTime::UniversalTime(), TTime::FormatL(), CTzConverter::ConvertToLocalTime() |
This code snippet demonstrates how to convert the time between different time zones. The snippet demonstrates the following functionality:
The following libraries are required:
LIBRARY tzclient.lib
#include <aknnotewrappers.h> // CEikonEnv::InfoWinL()
#include <tz.h> // RTz, CTzId
#include <tzconverter.h> // CTzConverter
TTime utcTime;
TTime time;
TBuf<100> buffer;
// Times are formatted according to the European standard with AM/PM marking.
// For example: 26/02/2008 1:07:03 pm
_LIT(KFormatTxt, "%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
// ----------------------------------------------------------------------------
// Obtain the universal time (UTC), format it, and display it
utcTime.UniversalTime();
utcTime.FormatL(buffer, KFormatTxt);
_LIT(KUtc, "UTC time");
iEikonEnv->InfoWinL(buffer, KUtc);
// ----------------------------------------------------------------------------
// Connect to the time zone server
RTz tzServer;
User::LeaveIfError(tzServer.Connect());
CleanupClosePushL(tzServer);
// ----------------------------------------------------------------------------
// Create a time zone converter
CTzConverter* tzConverter = CTzConverter::NewL(tzServer);
CleanupStack::PushL(tzConverter);
// ----------------------------------------------------------------------------
// Convert from UTC to local time
time = utcTime;
tzConverter->ConvertToLocalTime(time);
// Format and display the local time
time.FormatL(buffer, KFormatTxt);
_LIT(KLocal, "Local time");
iEikonEnv->InfoWinL(buffer, KLocal);
// ----------------------------------------------------------------------------
// Create a time zone ID object for Europe/Helsinki (GMT+2)
_LIT8(KHelsinki, "Europe/Helsinki");
CTzId* tzId = CTzId::NewL(KHelsinki);
CleanupStack::PushL(tzId);
// Convert UTC time to local time for Europe/Helsinki
time = utcTime;
TInt results = tzConverter->ConvertToLocalTime(time, *tzId);
if (results == KErrNone)
{
// Conversion successful. Display the time.
time.FormatL(buffer, KFormatTxt);
_LIT(KLocalHelsinki, "Local time in Europe/Helsinki");
iEikonEnv->InfoWinL(buffer, KLocalHelsinki);
}
else
{
_LIT(KError, "Error while converting");
_LIT(KErrorHeading, "Error");
iEikonEnv->InfoWinL(KError, KErrorHeading);
}
CleanupStack::PopAndDestroy(3); // tzId, tzConverter, tzServer
Several different time conversions are done.
No related wiki articles found