pimappsupport/chinesecalendaralg/calcontablesrc/CalconTable.cpp
branchRCL_3
changeset 12 38571fd2a704
child 51 0b38fc5b94c6
equal deleted inserted replaced
5:42814f902fe6 12:38571fd2a704
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Creates the Calcon look-up table for the date range:
       
    15 // 1/1/StartYear & 31/12/KEndYear
       
    16 // The Linda Agenda date range is 1980-2100
       
    17 // Creates CALCONDATA.CPP and CALCONDATA.H in \epoc32\wins\c
       
    18 // These should then be placed in  \calcon\src and \calcon\inc respectively
       
    19 // and then Calcon should be rebuilt.
       
    20 // Uses the original Calcon library, now called CalconOriginal.dll
       
    21 //
       
    22 
       
    23 void mainL();
       
    24 
       
    25 #include "CalconTable.h"
       
    26 #include "calconv.h"
       
    27 #include <utf.h>
       
    28 
       
    29 // Type definitions
       
    30 #define UNUSED_VAR(a) a = a
       
    31 
       
    32 //Change these to alter the date range for Calcon
       
    33 const TInt KStartYear=-587;
       
    34 const TInt KEndYear=2100;
       
    35 
       
    36 CCalconTableEngine::CCalconTableEngine() :
       
    37 	iTest(_L("CalconTable"))
       
    38 		{}
       
    39 
       
    40 void CCalconTableEngine::GenerateTableL()
       
    41 	{
       
    42 	TTime timeStarted;
       
    43 	timeStarted.UniversalTime();
       
    44 
       
    45 	iTest.Printf(_L("Generating Calcon table for dates:\n"));
       
    46 	iTest.Printf(_L("Start: 1st January %d\n"),KStartYear);
       
    47 	iTest.Printf(_L("End: 31st December %d\n\n"),KEndYear);
       
    48 	iTest.Printf(_L("Creating c:\\calcondata.cpp\n\n"));
       
    49 
       
    50 	TDateTime dateTime(KStartYear,EJanuary,0,0,0,0,0);
       
    51 	TDateTime endTime(KEndYear,EDecember,31-1,0,0,0,0);
       
    52 
       
    53 	iStartJulianDate=JulianDate(dateTime);
       
    54 	iEndJulianDate=JulianDate(endTime);
       
    55 
       
    56 	TChineseCalendar calendar;
       
    57 
       
    58 	TInt numYears=endTime.Year()-dateTime.Year()+4;
       
    59 	iYearArray = new (ELeave) TUint[numYears];
       
    60 
       
    61 //Set date to beginning of current Chinese year
       
    62 	TChineseDate initialChineseDate;
       
    63 	calendar.DateTimeToChinese(dateTime);
       
    64 	calendar.GetDate(initialChineseDate);
       
    65 
       
    66 	initialChineseDate.iMonth=1;
       
    67 	initialChineseDate.iDay=1;
       
    68 	calendar.SetDate(initialChineseDate);
       
    69 	calendar.ChineseToDateTime(dateTime);
       
    70 
       
    71 	TTime time=dateTime;
       
    72 	TReal chineseNewYear=JulianDate(dateTime);
       
    73 
       
    74 	TChineseDate lastDate;
       
    75 	TChineseDate chineseDate=initialChineseDate;
       
    76 
       
    77 //Move near the end of the current month
       
    78 	time+=TTimeIntervalDays(27);
       
    79 	TBool hadLeapMonth=EFalse;	
       
    80 
       
    81 	iSession.Connect();
       
    82 	TInt err=iFile.Replace(iSession, _L("c:\\calcondata.cpp"), EFileShareExclusive);
       
    83 	if (err!=KErrNone)
       
    84 		return;
       
    85 
       
    86 	err=iFile2.Replace(iSession, _L("c:\\calcondata.h"), EFileShareExclusive);
       
    87 	if (err!=KErrNone)
       
    88 		return;
       
    89 
       
    90 	StartPrint(initialChineseDate.iCycle);
       
    91 
       
    92 	while (chineseNewYear<iEndJulianDate)
       
    93 		{
       
    94 		lastDate=chineseDate;
       
    95 		time+=TTimeIntervalDays(1);
       
    96 
       
    97 		dateTime=time.DateTime();
       
    98 		calendar.DateTimeToChinese(dateTime);
       
    99 		calendar.GetDate(chineseDate);
       
   100 
       
   101 		if ((chineseDate.iMonth!=lastDate.iMonth)
       
   102 			|| (chineseDate.iLeapMonth!=lastDate.iLeapMonth))
       
   103 			{
       
   104 			TInt monthFlag= lastDate.iLeapMonth ? 2 : 0;
       
   105 			ASSERT(lastDate.iDay==29 || lastDate.iDay==30);
       
   106 		
       
   107 			monthFlag += lastDate.iDay==29 ? 1 : 2;
       
   108 
       
   109 			if (lastDate.iLeapMonth)
       
   110 				hadLeapMonth=ETrue;
       
   111 
       
   112 			TInt cycle=lastDate.iCycle-initialChineseDate.iCycle;
       
   113 			TInt year=lastDate.iYear-1;
       
   114 			TInt month=lastDate.iMonth-1;//ADDED -1
       
   115 			if (hadLeapMonth)
       
   116 				month++;
       
   117 
       
   118 			if(month==0)	//was 0
       
   119 				AddNewYear((TInt)chineseNewYear);
       
   120 			
       
   121 			AddMonth(cycle,year,month,monthFlag);
       
   122 
       
   123 			if (chineseDate.iYear!=lastDate.iYear)
       
   124 				{
       
   125 				hadLeapMonth=EFalse;
       
   126 				TDateTime dt(time.DateTime());
       
   127 				chineseNewYear=(TInt)JulianDate(dt);
       
   128 				}
       
   129 
       
   130 			time+=TTimeIntervalDays(27);
       
   131 			}
       
   132 		}
       
   133 
       
   134 	StopPrint(chineseDate.iCycle);
       
   135 
       
   136 	TTime timeFinished;
       
   137 	timeFinished.UniversalTime();
       
   138 	TInt64 duration=timeFinished.Int64()-timeStarted.Int64();
       
   139 
       
   140 	Print(1,_L8("\n//Took %u seconds to create\n"),duration/1000000);
       
   141 	iFile.Close();
       
   142 	iFile2.Close();
       
   143 	iSession.Close();
       
   144 
       
   145 	delete[] iYearArray;
       
   146 	iTest.Close();
       
   147 	}
       
   148 
       
   149 TInt CCalconTableEngine::JulianDate(TDateTime& aDate)
       
   150 	{
       
   151 
       
   152 	TGregorianCalendar greg;
       
   153 	greg.DateTimeToGregorian( aDate);
       
   154 	return (TInt)greg.JulianDay();
       
   155 
       
   156 /*	An alternative method for calculating the Julian Date, used in Chinese Linda.
       
   157 //	Results differ from Calcon for -ve Gregorian years.
       
   158 
       
   159 	TInt Month = STATIC_CAST(TInt, aDate.Month());
       
   160 	TInt Year;
       
   161 	Month++;
       
   162 	
       
   163 	if (Month > 2)
       
   164 		{
       
   165 		Month = Month - 3;
       
   166 		Year = aDate.Year();
       
   167 		}
       
   168 	else
       
   169 		{
       
   170 		Month = Month + 9;
       
   171 		Year = aDate.Year() - 1;
       
   172 		}
       
   173 
       
   174 	TInt Centuries = Year / 100; 
       
   175 	TInt Decades = Year - 100 * Centuries;
       
   176 	TInt julian = (146097 * Centuries)/4 + (1461 * Decades) /4 + (153 * Month + 2) / 5 + aDate.Day() + 1 + 1721119;
       
   177 	return julian;
       
   178 */
       
   179 	}
       
   180 
       
   181 void CCalconTableEngine::Print(TInt aFile,TPtrC8 aFormat, ...)
       
   182 	{
       
   183 	VA_LIST   args;
       
   184 	VA_START( args, aFormat);
       
   185 
       
   186 	TBuf8<1024> buffer;
       
   187 	TBuf16<1024> buffer2;
       
   188 
       
   189 	buffer.FormatList(aFormat, args);
       
   190 	CnvUtfConverter::ConvertToUnicodeFromUtf8(buffer2,buffer);
       
   191 
       
   192 	if (aFile==1)
       
   193 		iFile.Write(buffer);
       
   194 	if (aFile==2)
       
   195 		iFile2.Write(buffer);
       
   196 
       
   197 	iTest.Printf(buffer2);
       
   198 	}
       
   199 
       
   200 void CCalconTableEngine::AddNewYear(TInt aChineseNewYear)
       
   201 	{
       
   202 	iYearArray[iYearCounter++]=aChineseNewYear;
       
   203 	}
       
   204 
       
   205 void CCalconTableEngine::AddMonth(TInt aCycle, TInt aYear, TInt aMonth, TInt aMonthFlag)
       
   206 	{
       
   207 	if (iFirstPrint)
       
   208 		{
       
   209 		iFirstPrint=EFalse;
       
   210 		iFirstYear=aYear;
       
   211 
       
   212 		Print(2,_L8("const TInt KFirstJulianDate=%d;\n"),iStartJulianDate);
       
   213 		Print(2,_L8("const TInt KLastJulianDate=%d;\n"),iEndJulianDate);
       
   214 		Print(2,_L8("const TInt KFirstYear=%d; //1..60\n"),aYear+1);
       
   215 		Print(1,_L8("const TUint16 TCalconData::iCalConDataMonth[]={\n"));
       
   216 		
       
   217 		iMonthFlag=0;
       
   218 		iMonthFlagBit=1<<15;
       
   219 		iCyclePrint=aCycle;
       
   220 		iMonthPrint=aMonth;
       
   221 		iYearPrint=aYear;
       
   222 		}
       
   223 	else
       
   224 		{
       
   225 		if (aMonth==0)
       
   226 			{//print out the month details
       
   227 			Print(1,_L8(" %uU, // %u - %u\n"),iMonthFlag, iCyclePrint,iYearPrint);
       
   228 
       
   229 			iMonthFlag=0;
       
   230 			iMonthFlagBit=1<<15;
       
   231 			iCyclePrint=aCycle;
       
   232 			iMonthPrint=aMonth;
       
   233 			iYearPrint=aYear;
       
   234 			iLastYear=iYearPrint+1;
       
   235 			}
       
   236 		}
       
   237 
       
   238 	TBool bit=EFalse;	//29 days
       
   239 	if (aMonthFlag==2 || aMonthFlag==4)	//30 days
       
   240 		bit=ETrue;
       
   241 
       
   242 	if (aMonthFlag==3 || aMonthFlag==4)
       
   243 		{//set leap
       
   244 		TUint shiftedLeapMonth=TUint(aMonth+1)<<28;
       
   245 		iYearArray[iYearCounter-1]|=shiftedLeapMonth;
       
   246 		}
       
   247 	
       
   248 	if (bit)
       
   249 		iMonthFlag|=iMonthFlagBit;
       
   250 	
       
   251 	iMonthFlagBit>>=1;
       
   252 	}
       
   253 
       
   254 void CCalconTableEngine::StartPrint(TInt aFirstCycle)
       
   255 	{
       
   256 	iFirstPrint=ETrue;
       
   257 
       
   258 	Print(1,_L8("//\n"));
       
   259 	Print(1,_L8("// CALCONDATA.CPP\n"));
       
   260 	Print(1,_L8("//\n"));
       
   261 	Print(1,_L8("// Copyright (c) 2002 Symbian Ltd. All rights reserved\n"));
       
   262 	Print(1,_L8("//\n"));
       
   263 	Print(1,_L8("// Generated by CalconTable.exe\n"));
       
   264 	Print(1,_L8("//\n"));
       
   265 
       
   266 	Print(2,_L8("//\n"));
       
   267 	Print(2,_L8("// CALCONDATA.H\n"));
       
   268 	Print(2,_L8("//\n"));
       
   269 	Print(2,_L8("// Copyright (c) 2002 Symbian Ltd. All rights reserved\n"));
       
   270 	Print(2,_L8("//\n"));
       
   271 	Print(2,_L8("// Generated by CalconTable.exe\n"));
       
   272 	Print(2,_L8("//\n"));
       
   273 
       
   274 	Print(1,_L8("#include \"calconvusingtable.h\"\n"));
       
   275 	Print(2,_L8("const TInt KFirstCycle=%d;//1..n\n"),aFirstCycle);
       
   276 	}
       
   277 
       
   278 void CCalconTableEngine::StopPrint(TInt aLastCycle)
       
   279 	{
       
   280 	Print(1,_L8("};\n"));
       
   281 	Print(1,_L8("const TUint32 TCalconData::iCalConDataYear[]={\n"));
       
   282 
       
   283 	for (TInt i=0;i<iYearCounter;i++)
       
   284 		{
       
   285 		Print(1,_L8(" %u,"),iYearArray[i]);
       
   286 		Print(1,_L8(" // %d - %d\n"),(i+iFirstYear)/60, (i+iFirstYear)%60);;
       
   287 		}
       
   288 	
       
   289 	Print(1,_L8("0xffffffff\n};\n"));
       
   290 	Print(2,_L8("const TInt KLastCycle=%d;//1..n\n"),aLastCycle);
       
   291 	Print(2,_L8("const TInt KLastYear=%d;//1..60\n"),iLastYear);
       
   292 	Print(2,_L8("const TInt KNumberOfYears=%d;\n"),iYearCounter);
       
   293 	}
       
   294 
       
   295 GLDEF_C TInt E32Main()
       
   296     {
       
   297 	__UHEAP_MARK;
       
   298 	CTrapCleanup* cleanupStack=CTrapCleanup::New();
       
   299 
       
   300 	TRAPD(error,mainL());
       
   301 	UNUSED_VAR(error); //Used to supress build warnings
       
   302 
       
   303 	delete cleanupStack;
       
   304 	__UHEAP_MARKEND;
       
   305 	return KErrNone;
       
   306     }
       
   307 
       
   308 void mainL()
       
   309     {
       
   310 	CCalconTableEngine* engine = new (ELeave) CCalconTableEngine();
       
   311 	CleanupStack::PushL(engine);
       
   312 	engine->GenerateTableL();
       
   313 	CleanupStack::PopAndDestroy();  //delete ConvDates;
       
   314 	}