×
Namespaces

Variants
Actions

Archived:Converting time zones using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia E61i
Nokia E90 Communicator
Nokia N95 8GB
Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition
S60 3rd Edition, FP1
S60 3rd Edition, FP2 Beta
S60 3rd Edition FP2
S60 3rd Edition FP1
S60 3rd Edition (initial release)
Article
Keywords: TTime, RTz, CTzConverter, CTzId,TTime::UniversalTime(), TTime::FormatL(), CTzConverter::ConvertToLocalTime()
Created: tapiolaitinen (26 Feb 2008)
Last edited: lpvalente (04 Aug 2012)

Contents

Overview

This code snippet demonstrates how to convert the time between different time zones. The snippet demonstrates the following functionality:

  • Displaying UTC time
  • Converting UTC time to local time
  • Converting UTC time to a specific time zone (Europe/Helsinki)

MMP file

The following libraries are required:

LIBRARY  tzclient.lib

Source file

#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

Postconditions

Several different time conversions are done.

See also

This page was last modified on 4 August 2012, at 21:27.
97 page views in the last 30 days.