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