2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FLclTimeZone.cpp
19 * @brief This is the implementation file for TimeZone class.
24 #include <FBaseSysLog.h>
25 #include <FApp_AppInfo.h>
26 #include <FLclTimeZone.h>
27 #include <FLclTimeRule.h>
29 #include "FLcl_TimeZoneImpl.h"
32 using namespace Tizen::Base;
34 namespace Tizen { namespace Locales
37 enum GregorianCalendarEras
39 GREGORIAN_CALENDAR_BC = 0, // BC
40 GREGORIAN_CALENDAR_AD = 1, // AD
43 TimeZone::TimeZone(void)
44 : __pTimeZoneImpl(null)
46 __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl;
47 SysAssert(__pTimeZoneImpl);
51 TimeZone::~TimeZone(void)
53 delete __pTimeZoneImpl;
57 TimeZone::TimeZone(const TimeZone& tz)
58 : __pTimeZoneImpl(null)
60 __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(*tz.__pTimeZoneImpl);
61 SysAssert(__pTimeZoneImpl);
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)
70 __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(rawOffset, id);
71 SysAssert(__pTimeZoneImpl);
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)
79 __pTimeZoneImpl = new (std::nothrow) _TimeZoneImpl(rawOffset, id, startingRule, endingRule, dstOffset);
80 SysAssert(__pTimeZoneImpl);
84 // This method compares the equality of obj and the current instance.
86 TimeZone::operator ==(const TimeZone& timeZone) const
88 return *__pTimeZoneImpl == *timeZone.__pTimeZoneImpl;
92 TimeZone::Equals(const Object& obj) const
95 const TimeZone* pOtherTimeZone = dynamic_cast< const TimeZone* >(&obj);
96 if (pOtherTimeZone != null)
98 return *this == *pOtherTimeZone;
104 TimeZone::GetHashCode(void) const
107 return __pTimeZoneImpl->GetHashCode();
111 // This method gets the amount of time to be added to local standard time to get local wall clock time.
113 TimeZone::GetDstSavings(void) const
116 return __pTimeZoneImpl->GetDstSavings(); // method return value in minutes
121 TimeZone::GetDstStartingYear(void) const
124 return __pTimeZoneImpl->GetDstStartingYear();
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.
131 TimeZone::GetOffset(const DateTime& date, bool local, int& rawOffset, int& dstOffset) const
134 return __pTimeZoneImpl->GetOffset(date, local, rawOffset, dstOffset);
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.
141 TimeZone::GetOffset(long long ticks, int& offset) const
144 return __pTimeZoneImpl->GetOffset(ticks, offset);
148 // This method gets the difference in milliseconds between local time and GMT, not including pDst.(i.e. Raw offset)
150 TimeZone::GetRawOffset(void) const
153 return __pTimeZoneImpl->GetRawOffset(); // method return value in minutes
157 // This method gets the id of time zone.
159 TimeZone::GetId(void) const
162 return __pTimeZoneImpl->GetId();
167 TimeZone::GetDstStartingRule(void) const
170 return __pTimeZoneImpl->GetDstStartingRule();
175 TimeZone::GetDstEndingRule(void) const
178 return __pTimeZoneImpl->GetDstEndingRule();
182 // This method checks that the current instance uses pDst.
184 TimeZone::IsDstUsed(void) const
187 return __pTimeZoneImpl->IsDstUsed();
191 // This method sets the amount of time in minute that the clock is advanced during pDst.
193 TimeZone::SetDstSavings(int dstSavings)
196 __pTimeZoneImpl->SetDstSavings(dstSavings);
200 // This method sets the pDst starting/end rule.
202 TimeZone::SetDstRules(const TimeRule& startingRule, const TimeRule& endingRule, int dstSavings)
205 return __pTimeZoneImpl->SetDstRules(startingRule, endingRule, dstSavings);
209 // This method sets the pDst end rule
211 TimeZone::SetDstEndingRule(const TimeRule& endingRule)
214 __pTimeZoneImpl->SetDstEndingRule(endingRule);
218 // This method sets the pDst starting rule
220 TimeZone::SetDstStartingRule(const TimeRule& startingRule)
223 __pTimeZoneImpl->SetDstStartingRule(startingRule);
227 // This method sets the difference in minutes between local time and GMT, not including pDst.(i.e. Raw offset)
229 TimeZone::SetRawOffset(int rawOffset)
232 __pTimeZoneImpl->SetRawOffset(rawOffset); // rawOffset is in minutes
236 // This method sets the pDst starting year.
238 TimeZone::SetDstStartingYear(int year)
241 __pTimeZoneImpl->SetDstStartingYear(year);
245 // This method sets the id of time zone.
247 TimeZone::SetId(const String& id)
250 __pTimeZoneImpl->SetId(id);
254 // This operator assigns the specified @c TimeZone instance to the current instance.
256 TimeZone::operator =(const TimeZone& otherTimeZone)
259 if (otherTimeZone != *this)
261 delete __pTimeZoneImpl;
262 __pTimeZoneImpl = otherTimeZone.__pTimeZoneImpl->CloneN();
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.
271 TimeZone::operator !=(const TimeZone& otherTimeZone) const
274 return !operator ==(otherTimeZone);
278 // This method converts the UTC time to the standard time.
279 Tizen::Base::DateTime
280 TimeZone::UtcTimeToStandardTime(const Tizen::Base::DateTime& utcTime)
283 DateTime standardTime = utcTime;
284 standardTime.AddMinutes(__pTimeZoneImpl->GetRawOffset());
289 // This method converts the UTC time to the wall time.
290 Tizen::Base::DateTime
291 TimeZone::UtcTimeToWallTime(const Tizen::Base::DateTime& utcTime)
294 DateTime wallTime = utcTime;
298 result r = __pTimeZoneImpl->GetOffset(utcTime, false, rawOffset, dstOffset);
301 wallTime.AddMinutes(rawOffset + dstOffset);
307 // This method converts the standard time to the UTC time.
308 Tizen::Base::DateTime
309 TimeZone::StandardTimeToUtcTime(const Tizen::Base::DateTime& standardTime)
312 DateTime utcTime = standardTime;
313 utcTime.AddMinutes(-GetRawOffset());
318 // This method converts the wall time to the UTC time.
319 Tizen::Base::DateTime
320 TimeZone::WallTimeToUtcTime(const Tizen::Base::DateTime& wallTime)
323 DateTime utcTime = wallTime;
327 result r = GetOffset(wallTime, true, rawOffset, dstOffset);
328 SysAssert(r == E_SUCCESS);
330 utcTime.AddMinutes(-(rawOffset + dstOffset));
335 // This method gets the GMT.
336 // The GMT time zone has a raw offset of zero and does not use daylight savings time.
338 TimeZone::GetGmtTimeZone(void)
341 return TimeZone(0, L"Europe/London");
345 TimeZone::GetTimeZone(const Tizen::Base::String& id, Tizen::Locales::TimeZone& timeZone)
348 return _TimeZoneImpl::GetTimeZone(id, timeZone);
352 TimeZone::GetTimeZone(const Tizen::Base::String& id, const Tizen::Base::DateTime& utcTime, Tizen::Locales::TimeZone& timeZone)
355 return _TimeZoneImpl::GetTimeZone(id, utcTime, timeZone);
360 TimeZone::UtcTimeToStandardTime(const DateTime& utcTime, int rawOffset)
363 DateTime dt = utcTime;
364 dt.AddMinutes(rawOffset);
371 TimeZone::StandardTimeToUtcTime(const DateTime& standardTime, int rawOffset)
374 DateTime dt = standardTime;
375 dt.AddMinutes(-rawOffset);
382 TimeZone::UtcTimeToWallTime(const DateTime& utcTime, int rawOffset, int dstOffset)
385 DateTime dt = utcTime;
386 dt.AddMinutes(rawOffset + dstOffset);
393 TimeZone::WallTimeToUtcTime(const DateTime& wallTime, int rawOffset, int dstOffset)
396 DateTime dt = wallTime;
397 dt.AddMinutes(-(rawOffset + dstOffset));