examples/AppServices/timezoneconversion/TZExample.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This example demonstrates using the time zone server (RTz) 
00015 // to convert a local time to UTC time, then to convert the current 
00016 // time for one time zone to another time zone, 
00017 //
00018 
00019 #include <e32base.h>
00020 #include <e32cons.h>
00021 #include <tz.h>
00022 #include <tzconverter.h>
00023 
00024 // Local definitions 
00025 static CConsoleBase* console;
00026 static RTz tzServer;
00027 // Date/time format string
00028 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00029 
00030 // length of string to hold the date/time for display
00031 static const TInt KMaxDateTimeStringLength      = 30;
00032 // string to hold the date/time for display
00033 static TBuf<KMaxDateTimeStringLength> dateTimeString;
00034 // length of string to hold the time zone for display
00035 const TInt KMaxTimeZoneStringLength     = 20;
00036 
00037 // converts a UTC time to local time
00038 static void ConvertUtcToLocalTimeL()
00039         {
00040         // randomly choose a historical utc date/time, format it and print it out
00041         TTime time(TDateTime(2001,EJuly,20,10,0,0,0));
00042         time.FormatL(dateTimeString, KDateTimeString);
00043         _LIT(KConsoleMessage,"\n A random date / time in UTC is %S");
00044         console->Printf(KConsoleMessage,&dateTimeString);       
00045         
00046         // now convert the date/time to local time for Australia/Sydney time zone
00047         _LIT8(KAustraliaSydney,"Australia/Sydney");
00048         CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00049         CleanupStack::PushL(timezoneId);
00050         
00051         TInt results=tzServer.ConvertToLocalTime(time,*timezoneId);
00052         if(results == KErrNone)
00053         // conversion successful
00054                 {
00055                 TBuf<KMaxTimeZoneStringLength> timeZoneString;
00056                 timeZoneString.Copy(timezoneId->TimeZoneNameID());
00057                 time.FormatL(dateTimeString, KDateTimeString);
00058                 _LIT(KConsoleMessage1,"\n The date and time in the %S time zone is %S");
00059                 console->Printf(KConsoleMessage1,&timeZoneString, &dateTimeString);     
00060                 }
00061         else
00062                 {
00063                 _LIT(KConsoleMessage2,"\n Error %d while converting ");
00064                 console->Printf(KConsoleMessage2, results);     
00065                 }
00066         CleanupStack::PopAndDestroy(timezoneId);                
00067         }
00068         
00069 // convert the current local time for the system time zone 
00070 // to local time for another time zone.
00071 static void ConvertLocalToLocalTimeL()
00072         {
00073         // first, get the current local time, format it and print it out
00074         TTime time;
00075         time.HomeTime();
00076         time.FormatL(dateTimeString, KDateTimeString);
00077         _LIT(KConsoleMessage3,"\n\n The current local time is %S");
00078         console->Printf(KConsoleMessage3,&dateTimeString);
00079         // this is a 2 stage process - first convert the date/time to UTC.
00080         // No need to specify the time zone because 
00081         // this function uses the current system time zone.
00082         TInt results=tzServer.ConvertToUniversalTime(time);
00083         if(results == KErrNone)
00084                 {
00085                 //conversion successful
00086                 time.FormatL(dateTimeString, KDateTimeString);
00087                 _LIT(KConsoleMessage4,"\n The current UTC time is %S");
00088                 console->Printf(KConsoleMessage4, &dateTimeString);     
00089                 }
00090         else
00091                 {
00092                 _LIT(KConsoleMessage5,"\n Error %d while converting ");
00093                 console->Printf(KConsoleMessage5, results);     
00094                 }
00095 
00096         _LIT8(KAustraliaSydney,"Australia/Sydney");
00097         // create a timezone ID object
00098         CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00099         CleanupStack::PushL(timezoneId);
00100         // convert UTC time to local time for Australia/Sydney
00101         results=tzServer.ConvertToLocalTime(time,*timezoneId);
00102         if(results == KErrNone)
00103                 // conversion successful
00104                 {
00105                 TBuf<KMaxTimeZoneStringLength> timeZoneString;
00106                 timeZoneString.Copy(timezoneId->TimeZoneNameID());
00107                 time.FormatL(dateTimeString, KDateTimeString);
00108                 _LIT(KConsoleMessage6,"\n The current date and time in the %S time zone is %S");
00109                 console->Printf(KConsoleMessage6,&timeZoneString, &dateTimeString);     
00110                 }
00111         else
00112                 {
00113                 _LIT(KConsoleMessage7,"\n Error %d while converting ");
00114                 console->Printf(KConsoleMessage7, results);     
00115                 }
00116         CleanupStack::PopAndDestroy(timezoneId);        
00117         }
00118         
00119 static void ConvertTimeL()
00120         // Connects to the time zone server and does the conversion
00121         {
00122         User::LeaveIfError(tzServer.Connect());
00123         CleanupClosePushL(tzServer);
00124         //do the conversions
00125         ConvertUtcToLocalTimeL();
00126         ConvertLocalToLocalTimeL();
00127         CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
00128         }
00129         
00130 static void DoExampleL()
00131         {
00132         _LIT(KTxtConsoleTitle, "Time Zone example");
00133         // Create the console object to print the messages. 
00134         console = Console::NewL(KTxtConsoleTitle,TSize(KConsFullScreen,KConsFullScreen));
00135         CleanupStack::PushL(console);
00136         TRAPD(err,ConvertTimeL());
00137         if (err)
00138                 {
00139                 _LIT(KFailed,"\n\nConversion failed: leave code=%d");
00140                 console->Printf(KFailed, err);
00141                 }
00142         // wait for user to press a key before destroying console
00143         _LIT(KMsgPressAnyKey,"\n\nPress any key to continue\n\n");
00144         console->Printf(KMsgPressAnyKey);
00145         console->Getch();
00146         CleanupStack::PopAndDestroy(console);
00147         }
00148 
00149 // Standard entry point function
00150 TInt E32Main()
00151         {
00152         __UHEAP_MARK;
00153         // Active scheduler required as this is a console app
00154         CActiveScheduler* scheduler=new CActiveScheduler;
00155         // If active scheduler has been created, install it.
00156         if (scheduler)
00157                 {
00158                 CActiveScheduler::Install(scheduler); 
00159                 // Cleanup stack needed
00160                 CTrapCleanup* cleanup=CTrapCleanup::New();
00161                 if (cleanup)
00162                         {
00163                         TRAP_IGNORE(DoExampleL());
00164                         delete cleanup;
00165                         }
00166                 delete scheduler;
00167                 }
00168         __UHEAP_MARKEND;
00169         return KErrNone;
00170     }
00171   
00172 
00173 

Generated by  doxygen 1.6.2