|
1 // Copyright (c) 1997-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 // |
|
15 |
|
16 #include "agmdate.h" |
|
17 #include "agmtlsproxy.h" |
|
18 #include "agmtzrules.h" |
|
19 |
|
20 const TInt KAgnDateMonthsInAYear = 12; |
|
21 |
|
22 EXPORT_C TTime AgnDateTime::MinDate() |
|
23 /** Gets the minimum date/time allowed for agenda entries as a TTime. |
|
24 |
|
25 This value marks the start of the agenda model's valid date range. |
|
26 |
|
27 @internalAll |
|
28 @return The agenda model's minimum date/time. |
|
29 */ |
|
30 { |
|
31 //Midnight, January 1st 1900 |
|
32 return (TTime(MAKE_TINT64(0x00D504A2,0xC672E000))); |
|
33 } |
|
34 |
|
35 EXPORT_C TTime AgnDateTime::MaxDate() |
|
36 /** Gets the maximum date/time allowed for agenda entries as a TTime. |
|
37 |
|
38 This value marks the end of the agenda model's valid date range. |
|
39 |
|
40 @internalAll |
|
41 @return The agenda model's maximum date/time. |
|
42 */ |
|
43 { |
|
44 //Midnight, December 31st 2100 |
|
45 return (TTime(MAKE_TINT64(0x00eb8d74,0x5a6fc000))); |
|
46 } |
|
47 |
|
48 |
|
49 void AgnDateTime::AddTo(TDateTime& aDate,TTimeIntervalMonths aMonth) |
|
50 /** Adds a time interval in months to a date/time. This takes account of the date of the month, which simply |
|
51 adding the TTimeIntervalMonths would not do. |
|
52 |
|
53 @internalAll |
|
54 @param aDate Reference to a date/time. |
|
55 @param aMonth The number of months to add to the date/time. |
|
56 */ |
|
57 { |
|
58 TInt month = aDate.Month() + (aDate.Year()*KAgnDateMonthsInAYear) + aMonth.Int(); |
|
59 TInt day = aDate.Day(); |
|
60 TInt year = month / KAgnDateMonthsInAYear; |
|
61 month %= KAgnDateMonthsInAYear; |
|
62 TInt daysInMonth = Time::DaysInMonth(year,TMonth(month))-1; |
|
63 if (day > daysInMonth) |
|
64 { |
|
65 day = daysInMonth; |
|
66 } |
|
67 aDate.Set(year,TMonth(month),day,aDate.Hour(),aDate.Minute(),aDate.Second(),aDate.MicroSecond()); |
|
68 } |
|
69 |
|
70 |
|
71 |
|
72 void AgnDateTime::AddTo(TDateTime& aDate,TTimeIntervalYears aYear) |
|
73 /** Adds a time interval in years to a date/time. |
|
74 |
|
75 @internalAll |
|
76 @param aDate Reference to a date/time. On return, the date is modified and all |
|
77 time components are set to zero. |
|
78 @param aYear The number of years to add to the date/time. |
|
79 */ |
|
80 { |
|
81 AddTo(aDate,TTimeIntervalMonths(aYear.Int() * KAgnDateMonthsInAYear)); |
|
82 } |
|
83 |
|
84 |
|
85 |
|
86 TTimeIntervalMonths AgnDateTime::MonthsFrom(const TDateTime& aStartDate,const TDateTime& aEndDate) |
|
87 /** Calculates the number of whole months between two date/times. |
|
88 |
|
89 @internalAll |
|
90 @param aStartDate The date to start calculating from. |
|
91 @param aEndDate The date to calculate to. |
|
92 @return The number of whole months between the two date/times. |
|
93 */ |
|
94 { |
|
95 |
|
96 TInt monthsDifference = (aEndDate.Year()-aStartDate.Year())*12+(aEndDate.Month()-aStartDate.Month()); |
|
97 |
|
98 if (aEndDate.Day() < aStartDate.Day()) |
|
99 { // check relative positions within the month |
|
100 TInt endDaysInMonth = Time::DaysInMonth(aEndDate.Year(),aEndDate.Month()); |
|
101 TInt startDaysInMonth = Time::DaysInMonth(aStartDate.Year(),aStartDate.Month()); |
|
102 if (! ((aEndDate.Day() == (endDaysInMonth-1))&&(aStartDate.Day() == (startDaysInMonth-1)))) |
|
103 --monthsDifference; |
|
104 } |
|
105 return(monthsDifference); |
|
106 } |
|
107 |
|
108 |
|
109 TTimeIntervalYears AgnDateTime::YearsFrom(const TDateTime& aStartDate,const TDateTime& aEndDate) |
|
110 /** Calculates the number of whole years between two date/times. |
|
111 |
|
112 @internalAll |
|
113 @param aStartDate The date to start calculating from. |
|
114 @param aEndDate The date to calculate to. |
|
115 @return The number of whole years between the two date/times. |
|
116 */ |
|
117 { |
|
118 |
|
119 TTimeIntervalMonths mos = MonthsFrom(aStartDate,aEndDate); |
|
120 TTimeIntervalYears ret = mos.Int() / 12; |
|
121 return(ret); |
|
122 } |
|
123 |
|
124 TBool AgnDateTime::IsLessThan(const TDateTime& aDateLeft, const TDateTime& aDateRight) |
|
125 /** Tests whether one TDateTime instance is earlier than another. |
|
126 |
|
127 @internalAll |
|
128 @param aDateLeft First date/time. |
|
129 @param aDateRight Second date/time. |
|
130 @return True if the first date/time specified is earlier than the second. False |
|
131 if it is more recent, or they are the same. |
|
132 */ |
|
133 { |
|
134 return aDateLeft.Year() < aDateRight.Year() || |
|
135 (aDateLeft.Year() == aDateRight.Year() && aDateLeft.Month() < aDateRight.Month()) || |
|
136 (aDateLeft.Year() == aDateRight.Year() && |
|
137 aDateLeft.Month() == aDateRight.Month() && |
|
138 aDateLeft.Day() < aDateRight.Day()); |
|
139 } |
|
140 |
|
141 /** Is time between min and max time. */ |
|
142 EXPORT_C TBool AgnDateTime::IsValidAgendaTTime(const TTime& aDate) |
|
143 { |
|
144 return (aDate >= AgnDateTime::MinDate() && aDate <= AgnDateTime::MaxDate()); |
|
145 } |
|
146 |
|
147 /** Fetch a pointer to the fixed time mode converter */ |
|
148 const MAgnCalendarTimeMode* AgnDateTime::FixedTimeMode() |
|
149 { |
|
150 CAgnTlsProxy* proxy = static_cast<CAgnTlsProxy*>(Dll::Tls()); |
|
151 __ASSERT_DEBUG(proxy != NULL, User::Invariant()); |
|
152 return static_cast<const MAgnCalendarTimeMode*>(&proxy->FixedTimeMode()); |
|
153 } |
|
154 |
|
155 /** Fetch a pointer to the floating time mode converter */ |
|
156 const MAgnCalendarTimeMode* AgnDateTime::FloatingTimeMode() |
|
157 { |
|
158 |
|
159 CAgnTlsProxy* proxy = static_cast<CAgnTlsProxy*>(Dll::Tls()); |
|
160 __ASSERT_DEBUG(proxy != NULL, User::Invariant()); |
|
161 return static_cast<const MAgnCalendarTimeMode*>(&proxy->FloatingTimeMode()); |
|
162 } |
|
163 |
|
164 /** Convert a time from system local to UTC, without caching the result. */ |
|
165 EXPORT_C TTime AgnDateTime::ConvertToLocalTimeL(const TTime& aUtcTime) |
|
166 { |
|
167 CAgnTlsProxy* proxy = CAgnTlsProxy::CreateL(CAgnTlsProxy::TAgnTlsTzRulesType_None); |
|
168 CleanupStack::PushL(TCleanupItem(CAgnTlsProxy::Release, proxy)); |
|
169 TAgnCalendarTime utcTime; |
|
170 utcTime.SetUtcL(aUtcTime); |
|
171 TTime ret = utcTime.LocalL(); |
|
172 CleanupStack::PopAndDestroy(proxy); |
|
173 return ret; |
|
174 } |
|
175 |
|
176 /** Convert a time from UTC to system local, without caching the result. */ |
|
177 EXPORT_C TTime AgnDateTime::ConvertToUtcTimeL(const TTime& aLocalTime) |
|
178 { |
|
179 CAgnTlsProxy* proxy = CAgnTlsProxy::CreateL(CAgnTlsProxy::TAgnTlsTzRulesType_None); |
|
180 CleanupStack::PushL(TCleanupItem(CAgnTlsProxy::Release, proxy)); |
|
181 TAgnCalendarTime utcTime; |
|
182 utcTime.SetLocalL(aLocalTime); |
|
183 TTime ret = utcTime.UtcL(); |
|
184 CleanupStack::PopAndDestroy(proxy); |
|
185 return ret; |
|
186 } |
|
187 |
|
188 /** This function resets the hours, minutes, seconds and microseconds to 0 |
|
189 more quickly than using a TDateTime. */ |
|
190 EXPORT_C TTime AgnDateTime::ResetToMidnight(const TTime& aTime) |
|
191 { |
|
192 const TInt64 KMicrosecondsInADay (MAKE_TINT64(0x00000014,0x1DD76000)); |
|
193 const TInt64 newTime((aTime.Int64() / KMicrosecondsInADay) * KMicrosecondsInADay); |
|
194 return TTime(newTime); |
|
195 } |
|
196 |
|
197 /** Correctly calculate the number of days between two TTimes. DaysFrom gives unexpected results if the |
|
198 time of day is different for each TTime. */ |
|
199 TInt AgnDateTime::DaysBetweenDates(const TTime& aTimeLater, const TTime& aTimeEarlier) |
|
200 { |
|
201 TTime later = AgnDateTime::ResetToMidnight(aTimeLater); |
|
202 TTime earlier = AgnDateTime::ResetToMidnight(aTimeEarlier); |
|
203 return later.DaysFrom(earlier).Int(); |
|
204 } |