|
1 // Copyright (c) 1998-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 // Mapping between EPOC32 time and libc time |
|
15 // The basic philosophy is to work in time_t units (TInt) which |
|
16 // will be essentially TTimeIntervalSeconds from the start of Unix time. |
|
17 // To stay compliant with the C-Standard (with reference to C99 draft), we |
|
18 // set the meaning of time_t to be Universal time. |
|
19 // |
|
20 // |
|
21 |
|
22 #include <e32std.h> |
|
23 #include <ltime.h> |
|
24 #include "reent.h" // for _ASCTIME_SIZE |
|
25 #include <sys/time.h> // for gettimeofday |
|
26 #include <time.h> |
|
27 #include <string.h> // for strcpy |
|
28 |
|
29 #define UNIX_BASE TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)) // 00:00, Jan 1st 1970 |
|
30 |
|
31 // Utility routines for converting between representations |
|
32 |
|
33 #ifdef __SYMBIAN_COMPILE_UNUSED__ |
|
34 static struct tm& as_struct_tm (const time_t& t, struct tm& res) |
|
35 { |
|
36 TTime us = UNIX_BASE + TTimeIntervalSeconds(t); |
|
37 TDateTime dt = us.DateTime(); |
|
38 |
|
39 res.tm_sec = dt.Second(); |
|
40 res.tm_min = dt.Minute(); |
|
41 res.tm_hour = dt.Hour(); |
|
42 res.tm_mday = dt.Day() + 1; |
|
43 res.tm_mon = dt.Month(); |
|
44 res.tm_year = dt.Year() - 1900; |
|
45 |
|
46 // EPOC32 counts the year day as Jan 1st == day 1 |
|
47 res.tm_yday = us.DayNoInYear() - 1; |
|
48 |
|
49 // EPOC32 counts the weekdays from 0==Monday to 6==Sunday |
|
50 res.tm_wday = us.DayNoInWeek() + 1; |
|
51 if (res.tm_wday==7) |
|
52 res.tm_wday=0; // Sunday==0 in a struct tm |
|
53 |
|
54 // newlib just sets this field to -1 |
|
55 // tm_isdst doesn't really make sense here since we don't |
|
56 // know the locale for which to interpret this time. |
|
57 |
|
58 res.tm_isdst = -1; |
|
59 |
|
60 return res; |
|
61 } |
|
62 |
|
63 static void as_ttime (const struct tm& p, TTime& res, TBool normalise=EFalse) |
|
64 { |
|
65 TDateTime dt; |
|
66 TInt err = dt.Set(p.tm_year+1900, (enum TMonth)p.tm_mon, p.tm_mday-1, |
|
67 p.tm_hour, p.tm_min, p.tm_sec, 0); |
|
68 if (err == KErrNone) |
|
69 { |
|
70 res = dt; |
|
71 return; |
|
72 } |
|
73 if (!normalise) |
|
74 { |
|
75 res = TInt64(-1); |
|
76 return; |
|
77 } |
|
78 // Try to normalise things (for mktime) |
|
79 dt.Set(p.tm_year+1900, EJanuary, 0, 0, 0, 0, 0); |
|
80 res = dt; |
|
81 res += TTimeIntervalMonths (p.tm_mon); |
|
82 res += TTimeIntervalDays (p.tm_mday-1); |
|
83 res += TTimeIntervalHours (p.tm_hour); |
|
84 res += TTimeIntervalMinutes(p.tm_min); |
|
85 res += TTimeIntervalSeconds(p.tm_sec); |
|
86 } |
|
87 |
|
88 inline void as_ttime (const time_t& p, TTime& res) |
|
89 { |
|
90 res = UNIX_BASE + TTimeIntervalSeconds(p); |
|
91 } |
|
92 |
|
93 #endif //__SYMBIAN_COMPILE_UNUSED__ |
|
94 |
|
95 |
|
96 GLDEF_C time_t as_time_t(const TTime& t) |
|
97 { |
|
98 TTimeIntervalSeconds res; |
|
99 TInt err = t.SecondsFrom(UNIX_BASE, res); |
|
100 if (err) |
|
101 return -1; |
|
102 else |
|
103 return res.Int(); |
|
104 } |
|
105 |
|
106 #ifdef __SYMBIAN_COMPILE_UNUSED__ |
|
107 // Utility routine for formatting a TTime into a descriptor using the |
|
108 // UNIX ctime format. NB. EPOC32 abbreviations can be up to KMaxDayNameAbb |
|
109 // and KMaxMonthNameAbb characters (both == 4). The %F is needed to |
|
110 // force the meanings of %D, %Y etc. |
|
111 |
|
112 static TDes8& as_string (TTime& t, TDes8& res) |
|
113 { |
|
114 // UNICODE problem - t.Format operates on TDes => TDes16 |
|
115 |
|
116 #if !defined(_UNICODE) |
|
117 TRAPD(err, t.FormatL(res, _L("%F%*E %*N %D %H:%T:%S %Y"))); |
|
118 #else |
|
119 TBuf<_ASCTIME_SIZE> unires; |
|
120 TRAPD(err, t.FormatL(unires, _L("%F%*E %*N %D %H:%T:%S %Y"))); |
|
121 if (!err) |
|
122 res.Copy(unires); |
|
123 #endif |
|
124 if (err) |
|
125 res = _L8("Error\n"); |
|
126 else |
|
127 res.Append('\n'); |
|
128 return res; |
|
129 } |
|
130 #endif |
|
131 |
|
132 #ifdef __SYMBIAN_COMPILE_UNUSED__ |
|
133 /* |
|
134 Intended Usage: Utility routine for converting from UTC to localtime. |
|
135 */ |
|
136 inline time_t toLocal (const time_t aUniversalTime) |
|
137 { |
|
138 #ifndef __SERIES60_MRT_1_0 |
|
139 TTimeIntervalSeconds offset = User::UTCOffset(); |
|
140 return aUniversalTime + offset.Int(); |
|
141 #else |
|
142 TLocale locale; |
|
143 return aUniversalTime + locale.UniversalTimeOffset().Int(); |
|
144 #endif //__SERIES60_MRT_1_0 |
|
145 } |
|
146 |
|
147 /* |
|
148 Intended Usage: Utility routine for converting from localtime to UTC. |
|
149 However, having decided that time_t is always Universal time, toGMT is empty. |
|
150 */ |
|
151 inline time_t toGMT (const time_t aLocalTime) |
|
152 { |
|
153 return aLocalTime; |
|
154 } |
|
155 #endif //__SYMBIAN_COMPILE_UNUSED__ |
|
156 |
|
157 // external interface for the C library |
|
158 |
|
159 extern "C" { |
|
160 /* |
|
161 Intended Usage: Get current UTC time. |
|
162 Get the number of seconds elapsed since 00:00 hours, |
|
163 Jan 1, 1970 UTC from the system clock. |
|
164 */ |
|
165 EXPORT_C time_t time (time_t* p) |
|
166 { |
|
167 TTime t; |
|
168 t.UniversalTime(); |
|
169 |
|
170 time_t res = as_time_t(t); |
|
171 if (p) |
|
172 *p = res; |
|
173 return res; |
|
174 } |
|
175 |
|
176 |
|
177 /* |
|
178 Return number of clock ticks since process start. |
|
179 Returns the number of clock ticks elapsed. |
|
180 A macro constant called CLK_TCK defines the relation betwen |
|
181 clock tick and second (clock ticks per second). |
|
182 */ |
|
183 |
|
184 EXPORT_C clock_t clock () |
|
185 { |
|
186 int retval=-1; |
|
187 RThread proc; |
|
188 TTimeIntervalMicroSeconds iMicSecsFromEpoc; |
|
189 TInt err=proc.GetCpuTime(iMicSecsFromEpoc); |
|
190 |
|
191 if(err) |
|
192 { |
|
193 return retval; |
|
194 } |
|
195 return I64INT(iMicSecsFromEpoc.Int64()); |
|
196 } |
|
197 }// extern "C" |
|
198 |
|
199 |