00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <e32base.h>
00020 #include <e32cons.h>
00021 #include <tz.h>
00022 #include <tzconverter.h>
00023
00024
00025 static CConsoleBase* console;
00026 static RTz tzServer;
00027
00028 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00029
00030
00031 static const TInt KMaxDateTimeStringLength = 30;
00032
00033 static TBuf<KMaxDateTimeStringLength> dateTimeString;
00034
00035 const TInt KMaxTimeZoneStringLength = 20;
00036
00037
00038 static void ConvertUtcToLocalTimeL()
00039 {
00040
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
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
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
00070
00071 static void ConvertLocalToLocalTimeL()
00072 {
00073
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
00080
00081
00082 TInt results=tzServer.ConvertToUniversalTime(time);
00083 if(results == KErrNone)
00084 {
00085
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
00098 CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00099 CleanupStack::PushL(timezoneId);
00100
00101 results=tzServer.ConvertToLocalTime(time,*timezoneId);
00102 if(results == KErrNone)
00103
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
00121 {
00122 User::LeaveIfError(tzServer.Connect());
00123 CleanupClosePushL(tzServer);
00124
00125 ConvertUtcToLocalTimeL();
00126 ConvertLocalToLocalTimeL();
00127 CleanupStack::PopAndDestroy(1);
00128 }
00129
00130 static void DoExampleL()
00131 {
00132 _LIT(KTxtConsoleTitle, "Time Zone example");
00133
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
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
00150 TInt E32Main()
00151 {
00152 __UHEAP_MARK;
00153
00154 CActiveScheduler* scheduler=new CActiveScheduler;
00155
00156 if (scheduler)
00157 {
00158 CActiveScheduler::Install(scheduler);
00159
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