examples/Base/DateAndTime/Basics/Basics.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028  NOTE, to aid clarity, all literal text constructed using the _LIT macro
00029  is declared immediately before use.
00030  In production code, it may be more efficient for _LIT macros to be
00031  declared at file scope. 
00032 */
00033 
00034 
00035 #include "CommonFramework.h" // standard example stuff
00036 
00037 
00038 // advance declarations
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;         // time in microseconds since 0AD nominal Gregorian
00056         TDateTime dateTime; // year-month-day-hour-minute-second-microsecond
00057 
00058 
00059                 //
00060                 // set and print current date/time
00061                 //
00062         time.HomeTime(); // set time to home time
00063 
00064         _LIT(KTxt1,"Current date and time in fields (locale independent):\n");
00065         console->Printf(KTxt1);
00066 
00067 
00068                 //
00069                 // print all seven fields
00070                 //
00071         dateTime=time.DateTime();    // convert to fields
00072         _LIT(KFormat1,"%d %d %d %d:%d:%d.%d\n");
00073         console->Printf(KFormat1,
00074                 dateTime.Year(),
00075                 TInt(dateTime.Month()+1),// <-- Print month as a TInt to preserve
00076                                          //     locale independence
00077                 dateTime.Day()+1,        // <-- Day and month ranges begin 
00078                                          //     at zero (0-30 and 0-11), 
00079                                                  //     so add one when printing
00080                 dateTime.Hour(), dateTime.Minute(), dateTime.Second(),
00081                 dateTime.MicroSecond());
00082                 
00083 
00084                 //
00085                 // reset date to 31st December 1996
00086                 //
00087         TInt year=1996, month=12, day=31; // date, as if from text input
00088         TInt error=dateTime.Set(year,TMonth(month-1),day-1,23,59,59,999999);// note: month and 
00089                                                                             // day are 
00090                                                                             // zero-offset!
00091         User::LeaveIfError(error);        // check date/time set ok
00092         time=dateTime;                    // Time = 31st December 1996 23:59 
00093 
00094         _LIT(KTxt2,"Resetting date and time...\n");
00095         console->Printf(KTxt2);
00096         printDateTimeL(time); 
00097         WaitForKey();
00098 
00099                 //
00100                 // Add intervals to time
00101                 //
00102         TTimeIntervalMonths timeIntervalMonths(2);
00103         time+=timeIntervalMonths;         // Add two months 
00104         _LIT(KTxt3,"Adding 2 months to date...\n");
00105         console->Printf(KTxt3);
00106         printDateTimeL(time); 
00107 
00108         time-=timeIntervalMonths;         // Subtract two months 
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;        // Add max. seconds +1 to time to show overflow
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;        // Add max. seconds to time
00123         _LIT(KTxt6,"Adding 2147483647 seconds to date/time...\n");
00124         console->Printf(KTxt6);
00125         printDateTimeL(time); 
00126 
00127         WaitForKey();
00128 
00129                 //
00130                 // Set date to Monday 6th Jan 1997 
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);         // Date property functions for Mon 6 Jan 1997 
00143         WaitForKey();
00144         showFormattingL(time);            // Date and time formatting
00145         WaitForKey();
00146 
00147                 
00148                 //
00149                 // Create a new date, set it to 6th Jan 1996 to
00150                 // show TTime::MonthsFrom()
00151                 //
00152         TDateTime newDateTime(1996,EJanuary,5,23,59,59,999999);
00153         TTime newTime(newDateTime);
00154         printDifferencesL(time, newTime);
00155 
00156                 //
00157                 // Set newTime to 7th January 1996 00:00:00:000000
00158                 // and print difference in months again
00159                 //
00160         TTimeIntervalMicroSeconds timeIntervalMicroSeconds(1);
00161         newTime+=timeIntervalMicroSeconds;
00162         printDifferencesL(time, newTime);
00163         
00164                 //
00165                 // Set newTime to 6th January 1998 23:23:59:999999
00166                 //
00167         newDateTime.SetYear(1998);
00168         newTime=newDateTime;
00169         printDifferencesL(time, newTime);
00170                 //
00171                 // Set newTime to 7th January 1998 00:00:00:000000
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         // Demonstrate date property functions
00192         //
00193         TBuf<10> nameString;
00194 
00195         _LIT(KFormat4,"%F%N");
00196         aTime.FormatL(nameString,KFormat4); // Get month name
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); // Get day name
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                 // Demonstrate first week in year rules
00212                 //
00213         _LIT(KFormat9,"Week number in year = %d\n");
00214         console->Printf(KFormat9,aTime.WeekNoInYear());
00215         
00216                 //
00217                 // Change week number in year rule
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         // Format date and time, using system default locale settings
00228         //
00229         _LIT(KTxt8,"Formatting the date and time\n");
00230         console->Printf(KTxt8);
00231 
00232                 //
00233                 // Print full day name, day number in month with leading zeros, 
00234                 // date suffix, month name, year
00235                 //      
00236         TBuf<40> dateString; // This holds the formatted date and time string
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                 // Print abbreviated day name, day number in month, 
00246                 // date suffix, abbreviated month name, year
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                 // Print date in numeric form with leading zeros, using default 
00256                 // date ordering (European) and default date separator characters
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                 // Print time using default time separator characters 
00267                 // in 12 and 24 hour clock formats
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         // Print number of months difference between two dates
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 

Generated by  doxygen 1.6.2