examples/AppServices/calcon/CalconExample.cpp

00001 /*
00002 Copyright (c) 2005-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:  This example demonstrates conversion of Chinese lunar calendar dates to Gregorian dates
00028               and vice versa.
00029 */
00030 
00031 
00032 #include <e32std.h>
00033 #include <e32base.h>
00034 #include <e32cons.h>
00035 #include <calendarconverter.h> 
00036 
00037 // local definitions 
00038 static CConsoleBase* console;
00039 // Pointer to the CChineseCalendarConverter
00040 static CChineseCalendarConverter*       gCalendarConverter;
00041 // strings for display
00042 _LIT(KMsgPressAnyKey,"\nPress any key to continue\n\n");
00043 _LIT(KConsoleStars,"\n*************************\n");
00044 
00045 static void PrintChineseDate(TChineseDate aDate);
00046 static void PrintGregorianDate(TDateTime aDate);
00047 
00048 // Converts a Gregorian date to a Chinese date
00049 static TChineseDate& GregorianDatesToLunarDatesL(TDateTime aStartDT)
00050         {
00051         _LIT(KConsoleMessage,"This function demonstrates conversion of Gregorian to Chinese dates\n");
00052         console->Printf(KConsoleMessage);
00053                 
00054         _LIT(KConsoleMessage1,"\nBefore conversion, the date in Gregorian format is: ");
00055         console->Printf(KConsoleMessage1);
00056         PrintGregorianDate(aStartDT);
00057         
00058         TInt err1;
00059         static TChineseDate chineseDate;
00060         // Convert the earliest date in the convertible range
00061         TRAP(err1,gCalendarConverter->DateTimeToChineseL(aStartDT,chineseDate));
00062         if (err1)
00063                 {
00064                 _LIT(KConsoleMessage2,"Conversion error");
00065                 console->Printf(KConsoleMessage2);      
00066                 }
00067         else
00068                 {
00069                 _LIT(KConsoleMessage3,"\nAfter conversion, the date in Chinese format is:\n");
00070                 console->Printf(KConsoleMessage3);
00071                 PrintChineseDate(chineseDate);
00072         }
00073         console->Printf(KConsoleStars);
00074         // Wait for user to press a key before destroying console
00075         console->Printf(KMsgPressAnyKey);
00076         console->Getch();
00077         return chineseDate;
00078         }
00079         
00080 // Converts a Chinese date to a Gregorian date
00081 static void LunarDatesToGregorianDatesL(TChineseDate aChineseDate)
00082         {
00083         _LIT(KConsoleMessage,"This function demonstrates conversion of Chinese to Gregorian dates\n");
00084         console->Printf(KConsoleMessage);
00085                 
00086         TDateTime dateTime;
00087 
00088         _LIT(KConsoleMessage1,"\nBefore conversion, the date in Chinese format is:\n");
00089         console->Printf(KConsoleMessage1);
00090         PrintChineseDate(aChineseDate);
00091 
00092         TInt err;
00093         // Convert the last date in the convertible range
00094         TRAP(err,gCalendarConverter->ChineseToDateTimeL(aChineseDate, dateTime));
00095         if (err)
00096                 {
00097                 _LIT(KConsoleMessage2,"Conversion error");
00098                 console->Printf(KConsoleMessage2);      
00099                 }
00100         else
00101                 {
00102                 _LIT(KConsoleMessage3,"\nAfter conversion, the date is: ");
00103                 console->Printf(KConsoleMessage3);
00104 
00105                 PrintGregorianDate(dateTime);
00106         console->Printf(KConsoleStars);
00107 
00108                 }
00109         }
00110         
00111 // Prints Chinese date
00112 static void PrintChineseDate(TChineseDate aDate)        
00113         {
00114         TBuf16<32> buffer;
00115         _LIT(KFormatTxt,"Cycle: %d \n");
00116         buffer.Format(KFormatTxt,aDate.iCycle);
00117         console->Printf(buffer);
00118 
00119         _LIT(KFormatTxt2,"Year: %d \n");
00120         buffer.Format(KFormatTxt2,aDate.iYear);
00121         console->Printf(buffer);
00122   
00123         _LIT(KFormatTxt3,"Month: %d \n");
00124         buffer.Format(KFormatTxt3,aDate.iMonth);
00125         console->Printf(buffer);
00126   
00127         _LIT(KFormatTxt4,"LeapMonth: %d \n");
00128         buffer.Format(KFormatTxt4,aDate.iLeapMonth);
00129         console->Printf(buffer);
00130   
00131         _LIT(KFormatTxt5,"Day: %d \n");
00132         buffer.Format(KFormatTxt5,aDate.iDay);
00133         console->Printf(buffer);
00134         }
00135         
00136 // Prints Gregorian date
00137 static void PrintGregorianDate(TDateTime aDate)
00138         {
00139         TBuf16<32> buffer;
00140         _LIT(KFormatTxt,"%d%S  %S %d\n");
00141         TDateSuffix iDateSuffix = TDateSuffix(aDate.Day());
00142         TMonthName iMonthName = TMonthName(aDate.Month());
00143         buffer.Format(KFormatTxt,aDate.Day()+1,&iDateSuffix,&iMonthName,aDate.Year());
00144     console->Printf(buffer);
00145         }
00146         
00147 static void EgCalconL()
00148         {
00149         gCalendarConverter = CChineseCalendarConverter::NewL();
00150         CleanupStack::PushL(gCalendarConverter);
00151         
00152         TDateTime startDT;
00153         TDateTime finishDT;
00154         
00155         gCalendarConverter->DateRange(startDT, finishDT);
00156         
00157         TChineseDate chineseDate = GregorianDatesToLunarDatesL(startDT);
00158         LunarDatesToGregorianDatesL(chineseDate);
00159         
00160         CleanupStack::PopAndDestroy(gCalendarConverter); 
00161         }
00162         
00163 static void DoExampleL()
00164     {
00165         // Create the console to print the messages to. 
00166         _LIT(KConsoleMessageDisplay, "Calcon Example");
00167         console = Console::NewL(KConsoleMessageDisplay,TSize(KConsFullScreen,KConsFullScreen));
00168         CleanupStack::PushL(console);
00169         console->Printf(KConsoleMessageDisplay);
00170         console->Printf(KConsoleStars);
00171 
00172         TRAPD(err,EgCalconL());
00173         if (err)
00174                 {
00175                 _LIT(KFailed,"\n\nExample failed: leave code=%d");
00176                 console->Printf(KFailed, err);
00177                 }
00178         // wait for user to press a key before destroying console
00179         console->Printf(KMsgPressAnyKey);
00180         console->Getch();
00181     CleanupStack::PopAndDestroy(console);
00182     }
00183 
00184 // Standard entry point function
00185  TInt E32Main()
00186         {
00187         __UHEAP_MARK;
00188         // Active scheduler required as this is a console app
00189         CActiveScheduler* scheduler=new CActiveScheduler;
00190         // If active scheduler has been created, install it.
00191         if (scheduler)
00192                 {
00193                 CActiveScheduler::Install(scheduler); 
00194                 // Cleanup stack needed
00195                 CTrapCleanup* cleanup=CTrapCleanup::New();
00196                 if (cleanup)
00197                         {
00198                         
00199                     TRAP_IGNORE(DoExampleL());
00200                         delete cleanup;
00201                         }
00202                 delete scheduler;
00203                 }
00204         __UHEAP_MARKEND;
00205         return KErrNone;
00206     }
00207   
00208 
00209 

Generated by  doxygen 1.6.2