examples/AppServices/calcon/CalconExample.cpp

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

Generated by  doxygen 1.6.2