examples/AppServices/timezoneconversion/TZExample.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� This example demonstrates using the time zone server (RTz) 
00028               to convert a local time to UTC time, then to convert the current 
00029               time for one time zone to another time zone
00030 */
00031  
00032 
00033 #include <e32base.h>
00034 #include <e32cons.h>
00035 #include <tz.h>
00036 #include <tzconverter.h>
00037 
00038 // Local definitions 
00039 static CConsoleBase* console;
00040 static RTz tzServer;
00041 // Date/time format string
00042 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00043 
00044 // length of string to hold the date/time for display
00045 static const TInt KMaxDateTimeStringLength      = 30;
00046 // string to hold the date/time for display
00047 static TBuf<KMaxDateTimeStringLength> dateTimeString;
00048 // length of string to hold the time zone for display
00049 const TInt KMaxTimeZoneStringLength     = 20;
00050 
00051 // converts a UTC time to local time
00052 static void ConvertUtcToLocalTimeL()
00053         {
00054         // randomly choose a historical utc date/time, format it and print it out
00055         TTime time(TDateTime(2001,EJuly,20,10,0,0,0));
00056         time.FormatL(dateTimeString, KDateTimeString);
00057         _LIT(KConsoleMessage,"\n A random date / time in UTC is %S");
00058         console->Printf(KConsoleMessage,&dateTimeString);       
00059         
00060         // now convert the date/time to local time for Australia/Sydney time zone
00061         _LIT8(KAustraliaSydney,"Australia/Sydney");
00062         CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00063         CleanupStack::PushL(timezoneId);
00064         
00065         TInt results=tzServer.ConvertToLocalTime(time,*timezoneId);
00066         if(results == KErrNone)
00067         // conversion successful
00068                 {
00069                 TBuf<KMaxTimeZoneStringLength> timeZoneString;
00070                 timeZoneString.Copy(timezoneId->TimeZoneNameID());
00071                 time.FormatL(dateTimeString, KDateTimeString);
00072                 _LIT(KConsoleMessage1,"\n The date and time in the %S time zone is %S");
00073                 console->Printf(KConsoleMessage1,&timeZoneString, &dateTimeString);     
00074                 }
00075         else
00076                 {
00077                 _LIT(KConsoleMessage2,"\n Error %d while converting ");
00078                 console->Printf(KConsoleMessage2, results);     
00079                 }
00080         CleanupStack::PopAndDestroy(timezoneId);                
00081         }
00082         
00083 // convert the current local time for the system time zone 
00084 // to local time for another time zone.
00085 static void ConvertLocalToLocalTimeL()
00086         {
00087         // first, get the current local time, format it and print it out
00088         TTime time;
00089         time.HomeTime();
00090         time.FormatL(dateTimeString, KDateTimeString);
00091         _LIT(KConsoleMessage3,"\n\n The current local time is %S");
00092         console->Printf(KConsoleMessage3,&dateTimeString);
00093         // this is a 2 stage process - first convert the date/time to UTC.
00094         // No need to specify the time zone because 
00095         // this function uses the current system time zone.
00096         TInt results=tzServer.ConvertToUniversalTime(time);
00097         if(results == KErrNone)
00098                 {
00099                 //conversion successful
00100                 time.FormatL(dateTimeString, KDateTimeString);
00101                 _LIT(KConsoleMessage4,"\n The current UTC time is %S");
00102                 console->Printf(KConsoleMessage4, &dateTimeString);     
00103                 }
00104         else
00105                 {
00106                 _LIT(KConsoleMessage5,"\n Error %d while converting ");
00107                 console->Printf(KConsoleMessage5, results);     
00108                 }
00109 
00110         _LIT8(KAustraliaSydney,"Australia/Sydney");
00111         // create a timezone ID object
00112         CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00113         CleanupStack::PushL(timezoneId);
00114         // convert UTC time to local time for Australia/Sydney
00115         results=tzServer.ConvertToLocalTime(time,*timezoneId);
00116         if(results == KErrNone)
00117                 // conversion successful
00118                 {
00119                 TBuf<KMaxTimeZoneStringLength> timeZoneString;
00120                 timeZoneString.Copy(timezoneId->TimeZoneNameID());
00121                 time.FormatL(dateTimeString, KDateTimeString);
00122                 _LIT(KConsoleMessage6,"\n The current date and time in the %S time zone is %S");
00123                 console->Printf(KConsoleMessage6,&timeZoneString, &dateTimeString);     
00124                 }
00125         else
00126                 {
00127                 _LIT(KConsoleMessage7,"\n Error %d while converting ");
00128                 console->Printf(KConsoleMessage7, results);     
00129                 }
00130         CleanupStack::PopAndDestroy(timezoneId);        
00131         }
00132         
00133 static void ConvertTimeL()
00134         // Connects to the time zone server and does the conversion
00135         {
00136         User::LeaveIfError(tzServer.Connect());
00137         CleanupClosePushL(tzServer);
00138         //do the conversions
00139         ConvertUtcToLocalTimeL();
00140         ConvertLocalToLocalTimeL();
00141         CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
00142         }
00143         
00144 static void DoExampleL()
00145         {
00146         _LIT(KTxtConsoleTitle, "Time Zone example");
00147         // Create the console object to print the messages. 
00148         console = Console::NewL(KTxtConsoleTitle,TSize(KConsFullScreen,KConsFullScreen));
00149         CleanupStack::PushL(console);
00150         TRAPD(err,ConvertTimeL());
00151         if (err)
00152                 {
00153                 _LIT(KFailed,"\n\nConversion failed: leave code=%d");
00154                 console->Printf(KFailed, err);
00155                 }
00156         // wait for user to press a key before destroying console
00157         _LIT(KMsgPressAnyKey,"\n\nPress any key to continue\n\n");
00158         console->Printf(KMsgPressAnyKey);
00159         console->Getch();
00160         CleanupStack::PopAndDestroy(console);
00161         }
00162 
00163 // Standard entry point function
00164 TInt E32Main()
00165         {
00166         __UHEAP_MARK;
00167         // Active scheduler required as this is a console app
00168         CActiveScheduler* scheduler=new CActiveScheduler;
00169         // If active scheduler has been created, install it.
00170         if (scheduler)
00171                 {
00172                 CActiveScheduler::Install(scheduler); 
00173                 // Cleanup stack needed
00174                 CTrapCleanup* cleanup=CTrapCleanup::New();
00175                 if (cleanup)
00176                         {
00177                         TRAP_IGNORE(DoExampleL());
00178                         delete cleanup;
00179                         }
00180                 delete scheduler;
00181                 }
00182         __UHEAP_MARKEND;
00183         return KErrNone;
00184     }
00185   
00186 
00187 

Generated by  doxygen 1.6.2