Refactory NumberFormatter.
[platform/framework/native/appfw.git] / src / locales / FLclTimeZone.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FLclTimeZone.cpp
19  * @brief       This is the implementation file for TimeZone class.
20  */
21
22 // Includes
23
24 #include <FBaseSysLog.h>
25 #include <FApp_AppInfo.h>
26 #include <FLclTimeZone.h>
27 #include <FLclTimeRule.h>
28
29 #include "FLcl_TimeZoneImpl.h"
30
31
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Locales
35 {
36
37 enum GregorianCalendarEras
38 {
39         GREGORIAN_CALENDAR_BC = 0,      // BC
40         GREGORIAN_CALENDAR_AD = 1,      // AD
41 };
42
43 TimeZone::TimeZone(void)
44         : __pTimeZoneImpl(null)
45 {
46         __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl;
47         SysAssert(__pTimeZoneImpl);
48 }
49
50
51 TimeZone::~TimeZone(void)
52 {
53         delete __pTimeZoneImpl;
54 }
55
56
57 TimeZone::TimeZone(const TimeZone& tz)
58         : __pTimeZoneImpl(null)
59 {
60         __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(*tz.__pTimeZoneImpl);
61         SysAssert(__pTimeZoneImpl);
62 }
63
64
65 // This method constructs an instance of TimeZone with the given raw GMT offset
66 // and the name of time zone with no pDst.
67 TimeZone::TimeZone(int rawOffset, const String& id)
68         : __pTimeZoneImpl(null)
69 {
70         __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(rawOffset, id);
71         SysAssert(__pTimeZoneImpl);
72 }
73
74
75 // This method constructs an instance of TimeZone with the name, offset, and given pDst rules.
76 TimeZone::TimeZone(int rawOffset, const String& id, const TimeRule& startingRule, const TimeRule& endingRule, int dstOffset)
77         : __pTimeZoneImpl(null)
78 {
79         __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(rawOffset, id, startingRule, endingRule, dstOffset);
80         SysAssert(__pTimeZoneImpl);
81 }
82
83
84 // This method compares the equality of obj and the current instance.
85 bool
86 TimeZone::operator ==(const TimeZone& timeZone) const
87 {
88         return *__pTimeZoneImpl == *timeZone.__pTimeZoneImpl;
89 }
90
91 bool
92 TimeZone::Equals(const Object& obj) const
93 {
94         ClearLastResult();
95         const TimeZone* pOtherTimeZone = dynamic_cast< const TimeZone* >(&obj);
96         if (pOtherTimeZone != null)
97         {
98                 return *this == *pOtherTimeZone;
99         }
100         return false;
101 }
102
103 int
104 TimeZone::GetHashCode(void) const
105 {
106         ClearLastResult();
107         return __pTimeZoneImpl->GetHashCode();
108 }
109
110
111 // This method gets the amount of time to be added to local standard time to get local wall clock time.
112 int
113 TimeZone::GetDstSavings(void) const
114 {
115         ClearLastResult();
116         return __pTimeZoneImpl->GetDstSavings();                                // method return value in minutes
117 }
118
119
120 int
121 TimeZone::GetDstStartingYear(void) const
122 {
123         ClearLastResult();
124         return __pTimeZoneImpl->GetDstStartingYear();
125 }
126
127
128 // This method gets the difference in minutes between local time and GMT,
129 // taking into consideration both the raw offset and the effect of dst offset.
130 result
131 TimeZone::GetOffset(const DateTime& date, bool local, int& rawOffset, int& dstOffset) const
132 {
133         ClearLastResult();
134         return __pTimeZoneImpl->GetOffset(date, local, rawOffset, dstOffset);
135 }
136
137
138 // This method gets the difference in minutes between local time and GMT,
139 // taking into consideration both the raw offset and the effect of pDst.
140 result
141 TimeZone::GetOffset(long long ticks, int& offset) const
142 {
143         ClearLastResult();
144         return __pTimeZoneImpl->GetOffset(ticks, offset);
145 }
146
147
148 // This method gets the difference in milliseconds between local time and GMT, not including pDst.(i.e. Raw offset)
149 int
150 TimeZone::GetRawOffset(void) const
151 {
152         ClearLastResult();
153         return __pTimeZoneImpl->GetRawOffset();                                 // method return value in minutes
154 }
155
156
157 // This method gets the id of time zone.
158 String
159 TimeZone::GetId(void) const
160 {
161         ClearLastResult();
162         return __pTimeZoneImpl->GetId();
163 }
164
165
166 const TimeRule*
167 TimeZone::GetDstStartingRule(void) const
168 {
169         ClearLastResult();
170         return __pTimeZoneImpl->GetDstStartingRule();
171 }
172
173
174 const TimeRule*
175 TimeZone::GetDstEndingRule(void) const
176 {
177         ClearLastResult();
178         return __pTimeZoneImpl->GetDstEndingRule();
179 }
180
181
182 // This method checks that the current instance uses pDst.
183 bool
184 TimeZone::IsDstUsed(void) const
185 {
186         ClearLastResult();
187         return __pTimeZoneImpl->IsDstUsed();
188 }
189
190
191 // This method sets the amount of time in minute that the clock is advanced during pDst.
192 void
193 TimeZone::SetDstSavings(int dstSavings)
194 {
195         ClearLastResult();
196         __pTimeZoneImpl->SetDstSavings(dstSavings);
197 }
198
199
200 // This method sets the pDst starting/end rule.
201 result
202 TimeZone::SetDstRules(const TimeRule& startingRule, const TimeRule& endingRule, int dstSavings)
203 {
204         ClearLastResult();
205         return __pTimeZoneImpl->SetDstRules(startingRule, endingRule, dstSavings);
206 }
207
208
209 // This method sets the pDst end rule
210 void
211 TimeZone::SetDstEndingRule(const TimeRule& endingRule)
212 {
213         ClearLastResult();
214         __pTimeZoneImpl->SetDstEndingRule(endingRule);
215 }
216
217
218 // This method sets the pDst starting rule
219 void
220 TimeZone::SetDstStartingRule(const TimeRule& startingRule)
221 {
222         ClearLastResult();
223         __pTimeZoneImpl->SetDstStartingRule(startingRule);
224 }
225
226
227 // This method sets the difference in minutes between local time and GMT, not including pDst.(i.e. Raw offset)
228 void
229 TimeZone::SetRawOffset(int rawOffset)
230 {
231         ClearLastResult();
232         __pTimeZoneImpl->SetRawOffset(rawOffset);                              // rawOffset is in minutes
233 }
234
235
236 // This method sets the pDst starting year.
237 void
238 TimeZone::SetDstStartingYear(int year)
239 {
240         ClearLastResult();
241         __pTimeZoneImpl->SetDstStartingYear(year);
242 }
243
244
245 // This method sets the id of time zone.
246 void
247 TimeZone::SetId(const String& id)
248 {
249         ClearLastResult();
250         __pTimeZoneImpl->SetId(id);
251 }
252
253
254 // This operator assigns the specified @c TimeZone instance to the current instance.
255 TimeZone&
256 TimeZone::operator =(const TimeZone& otherTimeZone)
257 {
258         ClearLastResult();
259         if (otherTimeZone != *this)
260         {
261                 delete __pTimeZoneImpl;
262                 __pTimeZoneImpl = otherTimeZone.__pTimeZoneImpl->CloneN();
263         }
264         return *this;
265 }
266
267
268 // This inequality operator returns @c true if the specified @c TimeZone instance is not equals to the
269 // current instance. Otherwise, the operator returns @c false.
270 bool
271 TimeZone::operator !=(const TimeZone& otherTimeZone) const
272 {
273         ClearLastResult();
274         return !operator ==(otherTimeZone);
275 }
276
277
278 // This method converts the UTC time to the standard time.
279 Tizen::Base::DateTime
280 TimeZone::UtcTimeToStandardTime(const Tizen::Base::DateTime& utcTime)
281 {
282         ClearLastResult();
283         DateTime standardTime = utcTime;
284         standardTime.AddMinutes(__pTimeZoneImpl->GetRawOffset());
285         return standardTime;
286 }
287
288
289 // This method converts the UTC time to the wall time.
290 Tizen::Base::DateTime
291 TimeZone::UtcTimeToWallTime(const Tizen::Base::DateTime& utcTime)
292 {
293         ClearLastResult();
294         DateTime wallTime = utcTime;
295         int rawOffset = 0;
296         int dstOffset = 0;
297
298         result r = __pTimeZoneImpl->GetOffset(utcTime, false, rawOffset, dstOffset);
299         if (!IsFailed(r))
300         {
301                 wallTime.AddMinutes(rawOffset + dstOffset);
302         }
303         return wallTime;
304 }
305
306
307 // This method converts the standard time to the UTC time.
308 Tizen::Base::DateTime
309 TimeZone::StandardTimeToUtcTime(const Tizen::Base::DateTime& standardTime)
310 {
311         ClearLastResult();
312         DateTime utcTime = standardTime;
313         utcTime.AddMinutes(-GetRawOffset());
314         return utcTime;
315 }
316
317
318 // This method converts the wall time to the UTC time.
319 Tizen::Base::DateTime
320 TimeZone::WallTimeToUtcTime(const Tizen::Base::DateTime& wallTime)
321 {
322         ClearLastResult();
323         DateTime utcTime = wallTime;
324         int rawOffset = 0;
325         int dstOffset = 0;
326
327         result r = GetOffset(wallTime, true, rawOffset, dstOffset);
328         SysAssert(r == E_SUCCESS);
329
330         utcTime.AddMinutes(-(rawOffset + dstOffset));
331         return utcTime;
332 }
333
334
335 // This method gets the GMT.
336 // The GMT time zone has a raw offset of zero and does not use daylight savings time.
337 TimeZone
338 TimeZone::GetGmtTimeZone(void)
339 {
340         ClearLastResult();
341         return TimeZone(0, L"Europe/London");
342 }
343
344 result
345 TimeZone::GetTimeZone(const Tizen::Base::String& id, Tizen::Locales::TimeZone& timeZone)
346 {
347         ClearLastResult();
348         return _TimeZoneImpl::GetTimeZone(id, timeZone);
349 }
350
351 result
352 TimeZone::GetTimeZone(const Tizen::Base::String& id, const Tizen::Base::DateTime& utcTime, Tizen::Locales::TimeZone& timeZone)
353 {
354         ClearLastResult();
355         return _TimeZoneImpl::GetTimeZone(id, utcTime, timeZone);
356 }
357
358
359 DateTime
360 TimeZone::UtcTimeToStandardTime(const DateTime& utcTime, int rawOffset)
361 {
362         ClearLastResult();
363         DateTime dt = utcTime;
364         dt.AddMinutes(rawOffset);
365
366         return dt;
367 }
368
369
370 DateTime
371 TimeZone::StandardTimeToUtcTime(const DateTime& standardTime, int rawOffset)
372 {
373         ClearLastResult();
374         DateTime dt = standardTime;
375         dt.AddMinutes(-rawOffset);
376
377         return dt;
378 }
379
380
381 DateTime
382 TimeZone::UtcTimeToWallTime(const DateTime& utcTime, int rawOffset, int dstOffset)
383 {
384         ClearLastResult();
385         DateTime dt = utcTime;
386         dt.AddMinutes(rawOffset + dstOffset);
387
388         return dt;
389 }
390
391
392 DateTime
393 TimeZone::WallTimeToUtcTime(const DateTime& wallTime, int rawOffset, int dstOffset)
394 {
395         ClearLastResult();
396         DateTime dt = wallTime;
397         dt.AddMinutes(-(rawOffset + dstOffset));
398
399         return dt;
400 }
401
402
403 };
404 };      // Tizen::Locales