examples/Base/DateAndTime/Basics/Basics.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // NOTE, to aid clarity, all literal text constructed using the _LIT macro
00015 // is declared immediately before use.
00016 // In production code, it may be more efficient for _LIT macros to be
00017 // declared at file scope. 
00018 //
00019 
00020 #include "CommonFramework.h" // standard example stuff
00021 
00022 
00023 // advance declarations
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;         // time in microseconds since 0AD nominal Gregorian
00041         TDateTime dateTime; // year-month-day-hour-minute-second-microsecond
00042 
00043 
00044                 //
00045                 // set and print current date/time
00046                 //
00047         time.HomeTime(); // set time to home time
00048 
00049         _LIT(KTxt1,"Current date and time in fields (locale independent):\n");
00050         console->Printf(KTxt1);
00051 
00052 
00053                 //
00054                 // print all seven fields
00055                 //
00056         dateTime=time.DateTime();    // convert to fields
00057         _LIT(KFormat1,"%d %d %d %d:%d:%d.%d\n");
00058         console->Printf(KFormat1,
00059                 dateTime.Year(),
00060                 TInt(dateTime.Month()+1),// <-- Print month as a TInt to preserve
00061                                          //     locale independence
00062                 dateTime.Day()+1,        // <-- Day and month ranges begin 
00063                                          //     at zero (0-30 and 0-11), 
00064                                                  //     so add one when printing
00065                 dateTime.Hour(), dateTime.Minute(), dateTime.Second(),
00066                 dateTime.MicroSecond());
00067                 
00068 
00069                 //
00070                 // reset date to 31st December 1996
00071                 //
00072         TInt year=1996, month=12, day=31; // date, as if from text input
00073         TInt error=dateTime.Set(year,TMonth(month-1),day-1,23,59,59,999999);// note: month and 
00074                                                                             // day are 
00075                                                                             // zero-offset!
00076         User::LeaveIfError(error);        // check date/time set ok
00077         time=dateTime;                    // Time = 31st December 1996 23:59 
00078 
00079         _LIT(KTxt2,"Resetting date and time...\n");
00080         console->Printf(KTxt2);
00081         printDateTimeL(time); 
00082         WaitForKey();
00083 
00084                 //
00085                 // Add intervals to time
00086                 //
00087         TTimeIntervalMonths timeIntervalMonths(2);
00088         time+=timeIntervalMonths;         // Add two months 
00089         _LIT(KTxt3,"Adding 2 months to date...\n");
00090         console->Printf(KTxt3);
00091         printDateTimeL(time); 
00092 
00093         time-=timeIntervalMonths;         // Subtract two months 
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;        // Add max. seconds +1 to time to show overflow
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;        // Add max. seconds to time
00108         _LIT(KTxt6,"Adding 2147483647 seconds to date/time...\n");
00109         console->Printf(KTxt6);
00110         printDateTimeL(time); 
00111 
00112         WaitForKey();
00113 
00114                 //
00115                 // Set date to Monday 6th Jan 1997 
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);         // Date property functions for Mon 6 Jan 1997 
00128         WaitForKey();
00129         showFormattingL(time);            // Date and time formatting
00130         WaitForKey();
00131 
00132                 
00133                 //
00134                 // Create a new date, set it to 6th Jan 1996 to
00135                 // show TTime::MonthsFrom()
00136                 //
00137         TDateTime newDateTime(1996,EJanuary,5,23,59,59,999999);
00138         TTime newTime(newDateTime);
00139         printDifferencesL(time, newTime);
00140 
00141                 //
00142                 // Set newTime to 7th January 1996 00:00:00:000000
00143                 // and print difference in months again
00144                 //
00145         TTimeIntervalMicroSeconds timeIntervalMicroSeconds(1);
00146         newTime+=timeIntervalMicroSeconds;
00147         printDifferencesL(time, newTime);
00148         
00149                 //
00150                 // Set newTime to 6th January 1998 23:23:59:999999
00151                 //
00152         newDateTime.SetYear(1998);
00153         newTime=newDateTime;
00154         printDifferencesL(time, newTime);
00155                 //
00156                 // Set newTime to 7th January 1998 00:00:00:000000
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         // Demonstrate date property functions
00177         //
00178         TBuf<10> nameString;
00179 
00180         _LIT(KFormat4,"%F%N");
00181         aTime.FormatL(nameString,KFormat4); // Get month name
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); // Get day name
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                 // Demonstrate first week in year rules
00197                 //
00198         _LIT(KFormat9,"Week number in year = %d\n");
00199         console->Printf(KFormat9,aTime.WeekNoInYear());
00200         
00201                 //
00202                 // Change week number in year rule
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         // Format date and time, using system default locale settings
00213         //
00214         _LIT(KTxt8,"Formatting the date and time\n");
00215         console->Printf(KTxt8);
00216 
00217                 //
00218                 // Print full day name, day number in month with leading zeros, 
00219                 // date suffix, month name, year
00220                 //      
00221         TBuf<40> dateString; // This holds the formatted date and time string
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                 // Print abbreviated day name, day number in month, 
00231                 // date suffix, abbreviated month name, year
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                 // Print date in numeric form with leading zeros, using default 
00241                 // date ordering (European) and default date separator characters
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                 // Print time using default time separator characters 
00252                 // in 12 and 24 hour clock formats
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         // Print number of months difference between two dates
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 

Generated by  doxygen 1.6.2