First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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.
24 #include "reent.h" // for _ASCTIME_SIZE
25 #include <sys/time.h> // for gettimeofday
27 #include <string.h> // for strcpy
29 #define UNIX_BASE TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)) // 00:00, Jan 1st 1970
31 // Utility routines for converting between representations
33 #ifdef __SYMBIAN_COMPILE_UNUSED__
34 static struct tm& as_struct_tm (const time_t& t, struct tm& res)
36 TTime us = UNIX_BASE + TTimeIntervalSeconds(t);
37 TDateTime dt = us.DateTime();
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;
46 // EPOC32 counts the year day as Jan 1st == day 1
47 res.tm_yday = us.DayNoInYear() - 1;
49 // EPOC32 counts the weekdays from 0==Monday to 6==Sunday
50 res.tm_wday = us.DayNoInWeek() + 1;
52 res.tm_wday=0; // Sunday==0 in a struct tm
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.
63 static void as_ttime (const struct tm& p, TTime& res, TBool normalise=EFalse)
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);
78 // Try to normalise things (for mktime)
79 dt.Set(p.tm_year+1900, EJanuary, 0, 0, 0, 0, 0);
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);
88 inline void as_ttime (const time_t& p, TTime& res)
90 res = UNIX_BASE + TTimeIntervalSeconds(p);
93 #endif //__SYMBIAN_COMPILE_UNUSED__
96 GLDEF_C time_t as_time_t(const TTime& t)
98 TTimeIntervalSeconds res;
99 TInt err = t.SecondsFrom(UNIX_BASE, res);
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.
112 static TDes8& as_string (TTime& t, TDes8& res)
114 // UNICODE problem - t.Format operates on TDes => TDes16
116 #if !defined(_UNICODE)
117 TRAPD(err, t.FormatL(res, _L("%F%*E %*N %D %H:%T:%S %Y")));
119 TBuf<_ASCTIME_SIZE> unires;
120 TRAPD(err, t.FormatL(unires, _L("%F%*E %*N %D %H:%T:%S %Y")));
125 res = _L8("Error\n");
132 #ifdef __SYMBIAN_COMPILE_UNUSED__
134 Intended Usage: Utility routine for converting from UTC to localtime.
136 inline time_t toLocal (const time_t aUniversalTime)
138 #ifndef __SERIES60_MRT_1_0
139 TTimeIntervalSeconds offset = User::UTCOffset();
140 return aUniversalTime + offset.Int();
143 return aUniversalTime + locale.UniversalTimeOffset().Int();
144 #endif //__SERIES60_MRT_1_0
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.
151 inline time_t toGMT (const time_t aLocalTime)
155 #endif //__SYMBIAN_COMPILE_UNUSED__
157 // external interface for the C library
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.
165 EXPORT_C time_t time (time_t* p)
170 time_t res = as_time_t(t);
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).
184 EXPORT_C clock_t clock ()
188 TTimeIntervalMicroSeconds iMicSecsFromEpoc;
189 TInt err=proc.GetCpuTime(iMicSecsFromEpoc);
195 return I64INT(iMicSecsFromEpoc.Int64());