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