You Are Here:

Community: Wiki


CS000847 - Converting time zones

From Forum Nokia Wiki



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()

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

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fThemesE3aHomeE5fScreenX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X