calendarui/regionalplugins/calenregionalutil/src/calensolarterms.cpp
changeset 0 f979ecb2b13e
child 18 c198609911f9
child 65 12af337248b1
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Provides Solar Term dates for Lunar Calendar. Reads and caches
       
    15 *                binary data file that lists solar term dates. 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 //debug
       
    22 #include "calendarui_debug.h"
       
    23 
       
    24 #include "calensolarterms.h"
       
    25 
       
    26 #include "calenlunarpaths.h"
       
    27 
       
    28 #include <f32file.h>
       
    29 #include <s32file.h>
       
    30 
       
    31 
       
    32 // CONSTANTS
       
    33 _LIT(KSolarTermsFile, "SolarItems");
       
    34 // search path for solar item file
       
    35 //Uncomment for emulator
       
    36 _LIT( KSolarTermsPath, "//private//10005901//" );  
       
    37 
       
    38 //_LIT( KSolarTermsPath, "//Data//calenlunarchinese//" );
       
    39 const TInt KFirstSolarTermYear(1900);
       
    40 const TInt KLastSolarTermYear(2100);
       
    41 
       
    42 
       
    43 // ======== MEMBER FUNCTIONS ========
       
    44 
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // 
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CCalenSolarTerms* CCalenSolarTerms::NewL(RFs& aFs)
       
    51     {
       
    52     TRACE_ENTRY_POINT;
       
    53     
       
    54     CCalenSolarTerms* self = new (ELeave) CCalenSolarTerms(aFs);
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop( self );
       
    58     
       
    59     TRACE_EXIT_POINT;
       
    60     return self;
       
    61     }
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CCalenSolarTerms::~CCalenSolarTerms()
       
    69     {
       
    70     TRACE_ENTRY_POINT;
       
    71     TRACE_EXIT_POINT;
       
    72     }
       
    73 
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // 
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 TInt CCalenSolarTerms::CheckSolarTermDateL( const TDateTime& aDate )
       
    80     {
       
    81     TRACE_ENTRY_POINT;
       
    82     
       
    83     ReadSolarTermsL( aDate );
       
    84     if ( HasSolarTermDataAvailable( aDate ) )
       
    85         {
       
    86         // Solar festival data is available for this date 
       
    87         for (TInt i(0); i < KSolarTermCount; i++)
       
    88             {
       
    89             TDateTime date = iSolarTermDates[i];
       
    90             if (aDate.Month() == date.Month() && aDate.Day() == date.Day())
       
    91                 {
       
    92                 // First item in iSolarTermNames is LiChun ("Spring begins")
       
    93                 // occuring around 4.2 in western year. 
       
    94                 // It is first solar term in Chinese Lunar year, but it's 
       
    95                 // third term in western year.
       
    96                 // 
       
    97                 // iSolarTermDates list terms from beginning of western year,
       
    98                 // that's why we subtract 2 and take modulo 24 here, to convert
       
    99                 // order from 
       
   100                 // Xiao Han (~ 6.1.), Da Han (~22.1), Li Chun (~4.2)...
       
   101                 // to 
       
   102                 // Li Chun (~4.2.), ..., ..., Da Han (~22.1)
       
   103                 i += KSolarTermCount - 2;
       
   104                 TInt foundIndex = i % KSolarTermCount;
       
   105                 
       
   106                 TRACE_EXIT_POINT;
       
   107                 return foundIndex;
       
   108                 }
       
   109             }
       
   110         TRACE_EXIT_POINT;
       
   111         return KErrNotFound;
       
   112         }
       
   113     else
       
   114         {
       
   115         // Solar festival data is NOT available for this date 
       
   116         TRACE_EXIT_POINT;
       
   117         return KErrNotSupported;
       
   118         }
       
   119     }
       
   120 
       
   121 
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // ?description_if_needed
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 CCalenSolarTerms::CCalenSolarTerms(RFs& aFs) : iFs( aFs )
       
   128     {
       
   129     TRACE_ENTRY_POINT;
       
   130     TRACE_EXIT_POINT;
       
   131     }
       
   132 
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // ?description_if_needed
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 void CCalenSolarTerms::ConstructL()
       
   139     {
       
   140     TRACE_ENTRY_POINT;
       
   141     TRACE_EXIT_POINT;
       
   142     }
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // ?description_if_needed
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TBool CCalenSolarTerms::HasSolarTermDataAvailable(const TDateTime& aDate) const
       
   150     {
       
   151     TRACE_ENTRY_POINT;
       
   152     
       
   153     // Note: day parameter for TDateTime starts from 0, not from 1
       
   154     const TDateTime KMinAvailable( KFirstSolarTermYear, EJanuary, 0, 0, 0, 0, 0 );
       
   155     const TDateTime KMaxAvailable( KLastSolarTermYear, EDecember, 31 - 1, 23, 59, 59, 0 );
       
   156     
       
   157     TRACE_EXIT_POINT;
       
   158     return TTime(KMinAvailable) <= TTime(aDate) &&
       
   159            TTime(aDate) <= TTime(KMaxAvailable);
       
   160     }
       
   161 
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // Reads and caches 24 solar term dates for one year
       
   165 // from "SolarItems" file
       
   166 // ---------------------------------------------------------
       
   167 //
       
   168 void CCalenSolarTerms::ReadSolarTermsL(TDateTime aDate)
       
   169     {
       
   170     TRACE_ENTRY_POINT;
       
   171     
       
   172     // Caches one year of solar items
       
   173     if ( ! HasSolarTermDataAvailable( aDate ) )
       
   174         {
       
   175         return;
       
   176         }
       
   177 
       
   178     TInt year = aDate.Year();
       
   179 
       
   180     if (iCachedYear != year)
       
   181         {
       
   182         RFile file;
       
   183         RFs& fs = iFs;
       
   184         TFindFile ffile(fs);
       
   185         User::LeaveIfError(ffile.FindByDir(KSolarTermsFile, KSolarTermsPath));
       
   186         User::LeaveIfError(file.Open(fs,
       
   187                                      ffile.File(), EFileRead));
       
   188         CleanupClosePushL(file);
       
   189 
       
   190         TInt seekPos = 
       
   191             (year - KFirstSolarTermYear) * sizeof(TUint8) * KSolarTermCount;
       
   192 
       
   193         RFileReadStream readStream( file, seekPos );
       
   194         readStream.PushL();
       
   195 
       
   196          for (TInt i(0); i < KSolarTermCount; i++)
       
   197              {
       
   198              TMonth month = static_cast<TMonth>( EJanuary + (i / 2) );
       
   199              TInt day = readStream.ReadUint8L();
       
   200              TDateTime dt(year, month, day-1, 0, 0, 0, 0);
       
   201              iSolarTermDates[i] = dt;
       
   202              }
       
   203 
       
   204         CleanupStack::PopAndDestroy(2); // readStream, file
       
   205         iCachedYear = year;
       
   206         }
       
   207     
       
   208     TRACE_EXIT_POINT;
       
   209     }