calendarui/regionalplugins/KoreanLunar/src/CalenSolarTerms.cpp
branchRCL_3
changeset 31 97232defd20e
equal deleted inserted replaced
30:bd7edf625bdd 31:97232defd20e
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 : Class looking after alarm fields for forms.
       
    15 *
       
    16 */
       
    17 
       
    18 //debug
       
    19 #include "calendarui_debug.h"
       
    20 
       
    21 //user includes
       
    22 #include "CalenSolarTerms.h"
       
    23 #include "CalenLunarPaths.h"
       
    24 
       
    25 //system includes
       
    26 #include <f32file.h>
       
    27 #include <s32file.h>
       
    28 
       
    29 
       
    30 // CONSTANTS
       
    31 _LIT( KSolarTermsFile, "KoreanSolarItems" );
       
    32 const TInt KFirstSolarTermYear( 1900 );
       
    33 const TInt KLastSolarTermYear( 2100 );
       
    34 
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // CCalenSolarTerms::NewL()
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CCalenSolarTerms* CCalenSolarTerms::NewL( RFs& aFs )
       
    44     {
       
    45     TRACE_ENTRY_POINT;
       
    46     
       
    47     CCalenSolarTerms* self = new (ELeave) CCalenSolarTerms(aFs);
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     
       
    52     TRACE_EXIT_POINT;
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CCalenSolarTerms::~CCalenSolarTerms()
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CCalenSolarTerms::~CCalenSolarTerms()
       
    61     {
       
    62     TRACE_ENTRY_POINT;
       
    63     TRACE_EXIT_POINT;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CCalenSolarTerms::CheckSolarTermDateL()
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 TInt CCalenSolarTerms::CheckSolarTermDateL( const TDateTime& aDate )
       
    71     {
       
    72     TRACE_ENTRY_POINT;
       
    73     
       
    74     ReadSolarTermsL( aDate );
       
    75     if ( HasSolarTermDataAvailable( aDate ) )
       
    76         {
       
    77         // Solar festival data is available for this date 
       
    78         for ( TInt i( 0 ); i < KSolarTermCount; i++ )
       
    79             {
       
    80             TDateTime date = iSolarTermDates[i];
       
    81             if ( aDate.Month() == date.Month() && aDate.Day() == date.Day() )
       
    82                 {
       
    83                 // First item in iSolarTermNames is LiChun ("Spring begins")
       
    84                 // occuring around 4.2 in western year. 
       
    85                 // It is first solar term in Chinese Lunar year, but it's 
       
    86                 // third term in western year.
       
    87                 // 
       
    88                 // iSolarTermDates list terms from beginning of western year,
       
    89                 // that's why we subtract 2 and take modulo 24 here, to convert
       
    90                 // order from 
       
    91                 // Xiao Han (~ 6.1.), Da Han (~22.1), Li Chun (~4.2)...
       
    92                 // to 
       
    93                 // Li Chun (~4.2.), ..., ..., Da Han (~22.1)
       
    94                 i += KSolarTermCount - 2;
       
    95                 TInt foundIndex = i % KSolarTermCount;
       
    96                 
       
    97                 TRACE_EXIT_POINT;
       
    98                 return foundIndex;
       
    99                 }
       
   100             }
       
   101         TRACE_EXIT_POINT;
       
   102         return KErrNotFound;
       
   103         }
       
   104     else
       
   105         {
       
   106         // Solar festival data is NOT available for this date 
       
   107         TRACE_EXIT_POINT;
       
   108         return KErrNotSupported;
       
   109         }
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CCalenSolarTerms::CCalenSolarTerms()
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 CCalenSolarTerms::CCalenSolarTerms( RFs& aFs ) : iFs( aFs )
       
   117     {
       
   118     TRACE_ENTRY_POINT;
       
   119     TRACE_EXIT_POINT;
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CCalenSolarTerms::ConstructL()
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CCalenSolarTerms::ConstructL()
       
   127     {
       
   128     TRACE_ENTRY_POINT;
       
   129     TRACE_EXIT_POINT;
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CCalenSolarTerms::HasSolarTermDataAvailable()
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 TBool CCalenSolarTerms::HasSolarTermDataAvailable( const TDateTime& aDate ) const
       
   137     {
       
   138     TRACE_ENTRY_POINT;
       
   139     
       
   140     // Note: day parameter for TDateTime starts from 0, not from 1
       
   141     const TDateTime KMinAvailable( KFirstSolarTermYear, EJanuary, 0, 0, 0, 0, 0 );
       
   142     const TDateTime KMaxAvailable( KLastSolarTermYear, EDecember, 31 - 1, 23, 59, 59, 0 );
       
   143     
       
   144     TRACE_EXIT_POINT;
       
   145     return TTime( KMinAvailable ) <= TTime( aDate ) &&
       
   146            TTime( aDate ) <= TTime( KMaxAvailable );
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------
       
   150 // CCalenSolarTerms::ReadSolarTermsL()
       
   151 // Reads and caches 24 solar term dates for one year
       
   152 // from "SolarItems" file
       
   153 // ---------------------------------------------------------
       
   154 //
       
   155 void CCalenSolarTerms::ReadSolarTermsL( TDateTime aDate )
       
   156     {
       
   157     TRACE_ENTRY_POINT;
       
   158     
       
   159     // Caches one year of solar items
       
   160     if ( ! HasSolarTermDataAvailable( aDate ) )
       
   161         {
       
   162         return;
       
   163         }
       
   164 
       
   165     TInt year = aDate.Year();
       
   166 
       
   167     if ( iCachedYear != year )
       
   168         {
       
   169         RFile file;
       
   170         RFs& fs = iFs;
       
   171         TFindFile ffile( fs );
       
   172         User::LeaveIfError( ffile.FindByDir( KSolarTermsFile, KSolarTermsPath ) );
       
   173         User::LeaveIfError( file.Open( fs, ffile.File(), EFileRead ) );
       
   174         CleanupClosePushL( file );
       
   175 
       
   176         TInt seekPos = 
       
   177             ( year - KFirstSolarTermYear ) * sizeof( TUint8 ) * KSolarTermCount;
       
   178 
       
   179         RFileReadStream readStream( file, seekPos );
       
   180         readStream.PushL();
       
   181 
       
   182          for ( TInt i( 0 ); i < KSolarTermCount; i++ )
       
   183              {
       
   184              TMonth month = static_cast<TMonth>( EJanuary + (i / 2) );
       
   185              TInt day = readStream.ReadUint8L();
       
   186              TDateTime dt( year, month, day-1, 0, 0, 0, 0 );
       
   187              iSolarTermDates[i] = dt;
       
   188              }
       
   189 
       
   190         CleanupStack::PopAndDestroy( 2 ); // readStream, file
       
   191         iCachedYear = year;
       
   192         }
       
   193     
       
   194     TRACE_EXIT_POINT;
       
   195     }
       
   196 
       
   197 // End of file