1 /* |
|
2 * Copyright (c) 2010 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "KoreanLunarDateUtil.h" |
|
20 // debug |
|
21 #include "calendarui_debug.h" |
|
22 |
|
23 const TInt KMaxLunarYear = 60; |
|
24 |
|
25 /** |
|
26 * Public methods for constructing a CKoreanLunarDateUtil object. |
|
27 */ |
|
28 CKoreanLunarDateUtil* CKoreanLunarDateUtil::NewL(MCalenServices* aServices) |
|
29 { |
|
30 TRACE_ENTRY_POINT; |
|
31 CKoreanLunarDateUtil* self = CKoreanLunarDateUtil::NewLC(aServices); |
|
32 CleanupStack::Pop( self ); |
|
33 |
|
34 TRACE_EXIT_POINT; |
|
35 return self; |
|
36 } |
|
37 |
|
38 CKoreanLunarDateUtil* CKoreanLunarDateUtil::NewLC(MCalenServices* aServices) |
|
39 { |
|
40 TRACE_ENTRY_POINT; |
|
41 CKoreanLunarDateUtil* self = new (ELeave) CKoreanLunarDateUtil(aServices); |
|
42 CleanupStack::PushL( self ); |
|
43 self->ConstructL(); |
|
44 return self; |
|
45 } |
|
46 |
|
47 |
|
48 CKoreanLunarDateUtil::CKoreanLunarDateUtil(MCalenServices* aServices):iServices(aServices) |
|
49 { |
|
50 TRACE_ENTRY_POINT; |
|
51 } |
|
52 |
|
53 |
|
54 void CKoreanLunarDateUtil::ConstructL() |
|
55 { |
|
56 TRACE_ENTRY_POINT; |
|
57 iConverter = CKoreanCalConv::NewL(); |
|
58 } |
|
59 |
|
60 CKoreanLunarDateUtil::~CKoreanLunarDateUtil() |
|
61 { |
|
62 TRACE_ENTRY_POINT; |
|
63 delete iConverter; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Method for getting nearest gregorian date from |
|
68 * lunar day and month and gregorian reference. |
|
69 */ |
|
70 TDateTime CKoreanLunarDateUtil::GetNearestGregorianDateL( const TInt aMonth, |
|
71 const TInt aDay, |
|
72 const TBool aLeap, |
|
73 const TDateTime& aReference ) const |
|
74 { |
|
75 TRACE_ENTRY_POINT; |
|
76 TKoreanDate refLunarDate; |
|
77 TKoreanDate lunarDate; |
|
78 TDateTime resDateTime; |
|
79 TInt err = KErrNone; |
|
80 |
|
81 iConverter->DateTimeToKoreanL( aReference, refLunarDate ); |
|
82 /* Try to convert with the year and cycle we got with the reference */ |
|
83 lunarDate.iCycle = refLunarDate.iCycle; |
|
84 lunarDate.iYear = refLunarDate.iYear; |
|
85 lunarDate.iMonth = aMonth; |
|
86 lunarDate.iDay = aDay; |
|
87 lunarDate.iLeapMonth = aLeap; |
|
88 TRAP( err, iConverter->KoreanToDateTimeL( lunarDate, resDateTime ) ); |
|
89 if( err == KErrArgument ) |
|
90 { /* Date is invalid for this year (day 30 on a month |
|
91 that has only 29 days on selected year or a leap month |
|
92 not in this year), try to find suitable */ |
|
93 TKoreanDate lowerLimit; |
|
94 TKoreanDate upperLimit; |
|
95 iConverter->DateRange( lowerLimit, upperLimit ); |
|
96 while( 1 ) |
|
97 { /* Search by increasing year */ |
|
98 if( lunarDate.iYear < KMaxLunarYear ) |
|
99 { |
|
100 lunarDate.iYear++; |
|
101 } |
|
102 else |
|
103 { |
|
104 lunarDate.iYear = 1; |
|
105 lunarDate.iCycle++; |
|
106 } |
|
107 if( (lunarDate.iCycle == upperLimit.iCycle && |
|
108 lunarDate.iYear > upperLimit.iYear) || |
|
109 lunarDate.iCycle > upperLimit.iCycle ) |
|
110 { /* Could not find, just break */ |
|
111 break; |
|
112 } |
|
113 TRAP( err, iConverter->KoreanToDateTimeL( lunarDate, resDateTime ) ); |
|
114 if( err == KErrNone ) |
|
115 { /* Found suitable */ |
|
116 break; |
|
117 } |
|
118 } |
|
119 if( err != KErrNone ) |
|
120 { /* Suitable not found by increasing, |
|
121 reset values and search by decreasing year */ |
|
122 lunarDate.iCycle = refLunarDate.iCycle; |
|
123 lunarDate.iYear = refLunarDate.iYear; |
|
124 lunarDate.iMonth = aMonth; |
|
125 lunarDate.iDay = aDay; |
|
126 while( 1 ) |
|
127 { /* Search by decreasing year */ |
|
128 if( lunarDate.iYear > 1 ) |
|
129 { |
|
130 lunarDate.iYear--; |
|
131 } |
|
132 else |
|
133 { |
|
134 if( lunarDate.iCycle == 0 ) |
|
135 { /* Run out of cycles, break with error */ |
|
136 break; |
|
137 } |
|
138 lunarDate.iYear = KMaxLunarYear; |
|
139 lunarDate.iCycle--; |
|
140 } |
|
141 if( (lunarDate.iCycle == lowerLimit.iCycle && |
|
142 lunarDate.iYear < lowerLimit.iYear) || |
|
143 lunarDate.iCycle < lowerLimit.iCycle ) |
|
144 { /* Could not find, just break */ |
|
145 break; |
|
146 } |
|
147 TRAP( err, iConverter->KoreanToDateTimeL( lunarDate, resDateTime ) ); |
|
148 if( err == KErrNone ) |
|
149 { /* Found suitable */ |
|
150 break; |
|
151 } |
|
152 } |
|
153 } |
|
154 } |
|
155 /* If still not found, leave with error */ |
|
156 User::LeaveIfError( err ); |
|
157 |
|
158 TRACE_ENTRY_POINT; |
|
159 /* Otherwise return the result */ |
|
160 return resDateTime; |
|
161 } |
|
162 |
|
163 /** |
|
164 * Method for getting lunar yearly repeats in gregorian dates. |
|
165 */ |
|
166 void CKoreanLunarDateUtil::GetLunarYearlyRepeatsL( RArray<TCalTime>& aRDates, |
|
167 const TDateTime& aEntryDate, |
|
168 const TBool aFloating ) const |
|
169 { |
|
170 TInt err = KErrNone; |
|
171 TCalTime repeatDate; |
|
172 TDateTime resDateTime; |
|
173 TDateTime lowerLimit; |
|
174 TKoreanDate upperKoreanLimit; |
|
175 TKoreanDate lunarStartDate; |
|
176 |
|
177 /* Get the upper limit for dates in Korean lunar format */ |
|
178 iConverter->DateRange( lunarStartDate, upperKoreanLimit ); |
|
179 |
|
180 /* Get the entry date in Korean lunar format */ |
|
181 iConverter->DateTimeToKoreanL( aEntryDate, lunarStartDate ); |
|
182 |
|
183 /* Get the lower limit for dates in Gregorian format */ |
|
184 iConverter->DateRange( lowerLimit, resDateTime ); |
|
185 |
|
186 /* Get the first occurrence of the event after |
|
187 the lower limit in Korean lunar format */ |
|
188 iConverter->DateTimeToKoreanL( GetNearestGregorianDateL( lunarStartDate.iMonth, |
|
189 lunarStartDate.iDay, |
|
190 lunarStartDate.iLeapMonth, |
|
191 lowerLimit ), |
|
192 lunarStartDate ); |
|
193 |
|
194 while( 1 ) |
|
195 { |
|
196 TRAP( err, iConverter->KoreanToDateTimeL( lunarStartDate, resDateTime ) ); |
|
197 if( err == KErrNone ) |
|
198 { /* Date exists in current year */ |
|
199 if( aFloating ) |
|
200 { |
|
201 repeatDate.SetTimeLocalFloatingL( resDateTime ); |
|
202 } |
|
203 else |
|
204 { |
|
205 repeatDate.SetTimeLocalL( resDateTime ); |
|
206 } |
|
207 aRDates.AppendL( repeatDate ); |
|
208 } |
|
209 /* Increase to next lunar year */ |
|
210 if( lunarStartDate.iYear < KMaxLunarYear ) |
|
211 { |
|
212 lunarStartDate.iYear++; |
|
213 } |
|
214 else |
|
215 { |
|
216 lunarStartDate.iYear = 1; |
|
217 lunarStartDate.iCycle++; |
|
218 } |
|
219 if( (lunarStartDate.iCycle == upperKoreanLimit.iCycle && |
|
220 lunarStartDate.iYear > upperKoreanLimit.iYear) || |
|
221 lunarStartDate.iCycle > upperKoreanLimit.iCycle ) |
|
222 { /* Reached the limit, break */ |
|
223 break; |
|
224 } |
|
225 } |
|
226 } |
|
227 |
|
228 // End of file |
|