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