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

Generated by  doxygen 1.6.2