1 /*****************************************************************************
2  *
3  * Copyright (c) 2008-2010, CoreCodec, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of CoreCodec, Inc. nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CoreCodec, Inc. ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL CoreCodec, Inc. BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ****************************************************************************/
29 
30 #include "date.h"
31 
32 #if defined(TARGET_SYMBIAN)
33 
34 #undef T
35 #undef NULL
36 #include <e32std.h>
37 #include <e32hal.h>
38 
39 #ifdef SYMBIAN90
40 #include <tz.h>
41 #include <tzconverter.h>
42 #endif
43 
44 #ifndef SYMBIAN90
ToInt64(int64_t i)45 static INLINE TInt64 ToInt64(int64_t i) { return TInt64((int32_t)(i>>32),(uint32_t)i); }
46 #else
47 #define ToInt64(a) (a)
48 #endif
49 
GetTimeFreq()50 int GetTimeFreq()
51 {
52     TTimeIntervalMicroSeconds32 n;
53 	UserHal::TickPeriod(n);
54     return Scale32(1000000,1,n.Int());
55 }
56 
GetTimeTick()57 systick_t GetTimeTick()
58 {
59 	return User::TickCount();
60 }
61 
SymbianToDateTime(TTime Time)62 datetime_t SymbianToDateTime(TTime Time)
63 {
64     // reference is 1st January 2001 00:00:00.000 UTC
65 	datetime_t Date = INVALID_DATETIME_T;
66     TTime Reference(ToInt64(LL(0x00e05776f452a000)));
67     TTimeIntervalSeconds Diff;
68 
69     if (Time.SecondsFrom(Reference,Diff) == KErrNone)
70 	{
71 		Date = Diff.Int();
72 		if (Date==INVALID_DATETIME_T) ++Date;
73 	}
74 
75 	return Date;
76 }
77 
DateTimeToSymbian(datetime_t Time)78 TTime DateTimeToSymbian(datetime_t Time)
79 {
80   	// reference is 1st January 2001 00:00:00.000 UTC
81     TTime Reference(ToInt64(LL(0x00e05776f452a000)));
82     TTimeIntervalSeconds Diff;
83     Diff = Time;
84 	return Reference + Diff;
85 }
86 
GetTimeDate()87 datetime_t GetTimeDate()
88 {
89     TTime Now;
90     Now.UniversalTime();
91     return SymbianToDateTime(Now);
92 }
93 
TimePackToRel(const datepack_t * tp,bool_t Local)94 datetime_t TimePackToRel(const datepack_t *tp, bool_t Local)
95 {
96     TDateTime Date;
97     TTime ot;
98 	if (!tp) return INVALID_DATETIME_T;
99     Date.SetYear(tp->Year);
100     Date.SetMonth((TMonth)(tp->Month-1));
101     Date.SetDay(tp->Day-1);
102     Date.SetHour(tp->Hour);
103     Date.SetMinute(tp->Minute);
104     Date.SetSecond(tp->Second);
105 
106     ot = TTime(Date);
107 
108     if (Local)
109     {
110 #ifndef SYMBIAN90
111         TLocale locale;
112         TTimeIntervalSeconds universalTimeOffset(locale.UniversalTimeOffset());
113         ot -= universalTimeOffset;
114         if (locale.QueryHomeHasDaylightSavingOn())
115         {
116             TTimeIntervalHours daylightSaving(1);
117             ot -= daylightSaving;
118         }
119 #else
120         RTz TzServer;
121         if (TzServer.Connect()==KErrNone)
122         {
123             CTzConverter* Converter = CTzConverter::NewL(TzServer);
124             Converter->ConvertToUniversalTime(ot);
125             delete Converter;
126             TzServer.Close();
127         }
128 #endif
129     }
130 
131     return SymbianToDateTime(ot);
132 }
133 
GetDatePacked(datetime_t t,datepack_t * tp,bool_t Local)134 bool_t GetDatePacked(datetime_t t, datepack_t *tp, bool_t Local)
135 {
136 	TDateTime Date;
137     TTime ot;
138 	if (!tp || t == INVALID_DATETIME_T) return 0;
139 
140     ot = DateTimeToSymbian(t);
141 
142     if (Local)
143     {
144 #ifndef SYMBIAN90
145         TLocale locale;
146         TTimeIntervalSeconds universalTimeOffset(locale.UniversalTimeOffset());
147         ot += universalTimeOffset;
148         if (locale.QueryHomeHasDaylightSavingOn())
149         {
150             TTimeIntervalHours daylightSaving(1);
151             ot += daylightSaving;
152         }
153 #else
154         RTz TzServer;
155         if (TzServer.Connect()==KErrNone)
156         {
157             CTzConverter* Converter = CTzConverter::NewL(TzServer);
158             Converter->ConvertToLocalTime(ot);
159             delete Converter;
160             TzServer.Close();
161         }
162 #endif
163     }
164 
165 	Date = ot.DateTime();
166 	tp->Year = Date.Year();
167 	tp->Month = (int)Date.Month() + 1;
168 	tp->Day = Date.Day()+1;
169 	tp->Hour = Date.Hour();
170 	tp->Minute = Date.Minute();
171 	tp->Second = Date.Second();
172 	return 1;
173 }
174 
GetIsDst(datetime_t UNUSED_PARAM (t))175 bool_t GetIsDst(datetime_t UNUSED_PARAM(t))
176 {
177 #ifndef SYMBIAN90
178     TLocale locale;
179     return locale.QueryHomeHasDaylightSavingOn();
180 #else
181     TBool IsDst=EFalse;
182     RTz TzServer;
183     if (TzServer.Connect()==KErrNone)
184     {
185         CTzConverter* Converter = CTzConverter::NewL(TzServer);
186         CTzId* TzId = CTzId::NewL(Converter->CurrentTzId());
187         IsDst = TzServer.IsDaylightSavingOnL(*TzId);
188         delete TzId;
189         delete Converter;
190         TzServer.Close();
191     }
192     return IsDst;
193 #endif
194 }
195 
196 #endif
197