00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <e32base.h>
00034 #include <e32cons.h>
00035 #include <tz.h>
00036 #include <tzconverter.h>
00037
00038
00039 static CConsoleBase* console;
00040 static RTz tzServer;
00041
00042 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00043
00044
00045 static const TInt KMaxDateTimeStringLength = 30;
00046
00047 static TBuf<KMaxDateTimeStringLength> dateTimeString;
00048
00049 const TInt KMaxTimeZoneStringLength = 20;
00050
00051
00052 static void ConvertUtcToLocalTimeL()
00053 {
00054
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
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
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
00084
00085 static void ConvertLocalToLocalTimeL()
00086 {
00087
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
00094
00095
00096 TInt results=tzServer.ConvertToUniversalTime(time);
00097 if(results == KErrNone)
00098 {
00099
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
00112 CTzId* timezoneId = CTzId::NewL(KAustraliaSydney);
00113 CleanupStack::PushL(timezoneId);
00114
00115 results=tzServer.ConvertToLocalTime(time,*timezoneId);
00116 if(results == KErrNone)
00117
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
00135 {
00136 User::LeaveIfError(tzServer.Connect());
00137 CleanupClosePushL(tzServer);
00138
00139 ConvertUtcToLocalTimeL();
00140 ConvertLocalToLocalTimeL();
00141 CleanupStack::PopAndDestroy(1);
00142 }
00143
00144 static void DoExampleL()
00145 {
00146 _LIT(KTxtConsoleTitle, "Time Zone example");
00147
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
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
00164 TInt E32Main()
00165 {
00166 __UHEAP_MARK;
00167
00168 CActiveScheduler* scheduler=new CActiveScheduler;
00169
00170 if (scheduler)
00171 {
00172 CActiveScheduler::Install(scheduler);
00173
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