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
00034
00035 #include "CommonFramework.h"
00036
00037
00038
00039 void printDateTimeL(TTime);
00040 void showDatePropertiesL(TTime);
00041 void showFormattingL(TTime);
00042 void printDifferencesL(TTime, TTime);
00043
00044
00045 void WaitForKey()
00046 {
00047 _LIT(KMsgPressAnyKey,"Press any key to continue\n\n");
00048 console->Printf(KMsgPressAnyKey);
00049 console->Getch();
00050 }
00051
00052
00053 LOCAL_C void doExampleL()
00054 {
00055 TTime time;
00056 TDateTime dateTime;
00057
00058
00059
00060
00061
00062 time.HomeTime();
00063
00064 _LIT(KTxt1,"Current date and time in fields (locale independent):\n");
00065 console->Printf(KTxt1);
00066
00067
00068
00069
00070
00071 dateTime=time.DateTime();
00072 _LIT(KFormat1,"%d %d %d %d:%d:%d.%d\n");
00073 console->Printf(KFormat1,
00074 dateTime.Year(),
00075 TInt(dateTime.Month()+1),
00076
00077 dateTime.Day()+1,
00078
00079
00080 dateTime.Hour(), dateTime.Minute(), dateTime.Second(),
00081 dateTime.MicroSecond());
00082
00083
00084
00085
00086
00087 TInt year=1996, month=12, day=31;
00088 TInt error=dateTime.Set(year,TMonth(month-1),day-1,23,59,59,999999);
00089
00090
00091 User::LeaveIfError(error);
00092 time=dateTime;
00093
00094 _LIT(KTxt2,"Resetting date and time...\n");
00095 console->Printf(KTxt2);
00096 printDateTimeL(time);
00097 WaitForKey();
00098
00099
00100
00101
00102 TTimeIntervalMonths timeIntervalMonths(2);
00103 time+=timeIntervalMonths;
00104 _LIT(KTxt3,"Adding 2 months to date...\n");
00105 console->Printf(KTxt3);
00106 printDateTimeL(time);
00107
00108 time-=timeIntervalMonths;
00109 _LIT(KTxt4,"Subtracting 2 months from date...\n");
00110 console->Printf(KTxt4);
00111 printDateTimeL(time);
00112
00113 WaitForKey();
00114
00115 TTimeIntervalSeconds timeIntervalSeconds(2147483647);
00116 time+=timeIntervalSeconds;
00117 _LIT(KTxt5,"Adding 2147483648 seconds to date/time - should overflow...\n");
00118 console->Printf(KTxt5);
00119 printDateTimeL(time);
00120
00121 timeIntervalSeconds=2147483647;
00122 time+=timeIntervalSeconds;
00123 _LIT(KTxt6,"Adding 2147483647 seconds to date/time...\n");
00124 console->Printf(KTxt6);
00125 printDateTimeL(time);
00126
00127 WaitForKey();
00128
00129
00130
00131
00132 day=5, month=1, year=1997;
00133 dateTime.SetMonth(TMonth(month-1));
00134 dateTime.SetDay(day);
00135 dateTime.SetYear(year);
00136 time=dateTime;
00137
00138 _LIT(KTxt7,"Resetting date ...\n");
00139 console->Printf(KTxt7);
00140 printDateTimeL(time);
00141
00142 showDatePropertiesL(time);
00143 WaitForKey();
00144 showFormattingL(time);
00145 WaitForKey();
00146
00147
00148
00149
00150
00151
00152 TDateTime newDateTime(1996,EJanuary,5,23,59,59,999999);
00153 TTime newTime(newDateTime);
00154 printDifferencesL(time, newTime);
00155
00156
00157
00158
00159
00160 TTimeIntervalMicroSeconds timeIntervalMicroSeconds(1);
00161 newTime+=timeIntervalMicroSeconds;
00162 printDifferencesL(time, newTime);
00163
00164
00165
00166
00167 newDateTime.SetYear(1998);
00168 newTime=newDateTime;
00169 printDifferencesL(time, newTime);
00170
00171
00172
00173 newTime+=timeIntervalMicroSeconds;
00174 printDifferencesL(time, newTime);
00175 }
00176
00177 void printDateTimeL(TTime aTime)
00178 {
00179 TBuf<40> dateTimeString;
00180
00181 _LIT(KFormat2,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
00182 aTime.FormatL(dateTimeString,KFormat2);
00183
00184 _LIT(KFormat3,"new date and time: %S \n");
00185 console->Printf(KFormat3,&dateTimeString);
00186 }
00187
00188 void showDatePropertiesL(TTime aTime)
00189 {
00190
00191
00192
00193 TBuf<10> nameString;
00194
00195 _LIT(KFormat4,"%F%N");
00196 aTime.FormatL(nameString,KFormat4);
00197
00198 _LIT(KFormat5,"Days in month (%S) = %d\n");
00199 console->Printf(KFormat5,&nameString,aTime.DaysInMonth());
00200
00201 _LIT(KFormat6,"%E");
00202 aTime.FormatL(nameString,KFormat6);
00203
00204 _LIT(KFormat7,"Day number in year is %d\n");
00205 console->Printf(KFormat7,aTime.DayNoInYear());
00206
00207 _LIT(KFormat8,"Day number in week = %d (%S)\n");
00208 console->Printf(KFormat8,aTime.DayNoInWeek(),&nameString);
00209
00210
00211
00212
00213 _LIT(KFormat9,"Week number in year = %d\n");
00214 console->Printf(KFormat9,aTime.WeekNoInYear());
00215
00216
00217
00218
00219 _LIT(KFormat10,"Counting first full week as week 1,\nweek number in year = %d\n");
00220 console->Printf(KFormat10,aTime.WeekNoInYear(EFirstFullWeek));
00221 }
00222
00223
00224 void showFormattingL(TTime aTime)
00225 {
00226
00227
00228
00229 _LIT(KTxt8,"Formatting the date and time\n");
00230 console->Printf(KTxt8);
00231
00232
00233
00234
00235
00236 TBuf<40> dateString;
00237
00238 _LIT(KFormat11,"%E%D%X%N%Y %1 %2 %3");
00239 aTime.FormatL(dateString,KFormat11);
00240
00241 _LIT(KFormat12,"Full date with date suffix = %S\n");
00242 console->Printf(KFormat12,&dateString);
00243
00244
00245
00246
00247
00248 _LIT(KFormat13,"%*E%*D%X%*N%Y %1 %2 %3");
00249 aTime.FormatL(dateString,KFormat13);
00250
00251 _LIT(KFormat14,"Full date with abbreviated names= %S\n");
00252 console->Printf(KFormat14,&dateString);
00253
00254
00255
00256
00257
00258 _LIT(KFormat15,"%D%M%Y%/0%1%/1%2%/2%3%/3");
00259 aTime.FormatL(dateString,KFormat15);
00260
00261 _LIT(KFormat16,"dd/mm/yyyy = %S\n");
00262 console->Printf(KFormat16,&dateString);
00263
00264
00265
00266
00267
00268
00269 _LIT(KFormat17,"%:0%H%:1%T%:2%S.%*C3%:3");
00270 aTime.FormatL(dateString,KFormat17);
00271
00272 _LIT(KFormat18,"Hour, minute, second, microsecond (24 hr clock) = %S\n");
00273 console->Printf(KFormat18,&dateString);
00274
00275 _LIT(KFormat19,"%:0%I%:1%T%:2%S.%*C3%:3%A");
00276 aTime.FormatL(dateString,KFormat19);
00277
00278 _LIT(KFormat20,"Hour, minute, second, microsecond (12 hr clock) = %S\n");
00279 console->Printf(KFormat20,&dateString);
00280 }
00281
00282 void printDifferencesL(TTime aTime, TTime aNewTime)
00283 {
00284
00285
00286
00287 TBuf<40> dateString;
00288 TBuf<40> newDateString;
00289 TInt numberOfMonths;
00290
00291 _LIT(KFormat21,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
00292 aTime.FormatL(dateString,KFormat21);
00293
00294 _LIT(KFormat22,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
00295 aNewTime.FormatL(newDateString,KFormat22);
00296
00297 _LIT(KFormat23,"Number of months between:\n%S and\n%S = %d\n");
00298 numberOfMonths = (aTime.MonthsFrom(aNewTime)).Int();
00299 console->Printf(KFormat23,&dateString,&newDateString,numberOfMonths);
00300 }
00301