1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/time.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,199 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Mapping between EPOC32 time and libc time
1.18 +// The basic philosophy is to work in time_t units (TInt) which
1.19 +// will be essentially TTimeIntervalSeconds from the start of Unix time.
1.20 +// To stay compliant with the C-Standard (with reference to C99 draft), we
1.21 +// set the meaning of time_t to be Universal time.
1.22 +//
1.23 +//
1.24 +
1.25 +#include <e32std.h>
1.26 +#include <ltime.h>
1.27 +#include "reent.h" // for _ASCTIME_SIZE
1.28 +#include <sys/time.h> // for gettimeofday
1.29 +#include <time.h>
1.30 +#include <string.h> // for strcpy
1.31 +
1.32 +#define UNIX_BASE TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)) // 00:00, Jan 1st 1970
1.33 +
1.34 +// Utility routines for converting between representations
1.35 +
1.36 +#ifdef __SYMBIAN_COMPILE_UNUSED__
1.37 +static struct tm& as_struct_tm (const time_t& t, struct tm& res)
1.38 + {
1.39 + TTime us = UNIX_BASE + TTimeIntervalSeconds(t);
1.40 + TDateTime dt = us.DateTime();
1.41 +
1.42 + res.tm_sec = dt.Second();
1.43 + res.tm_min = dt.Minute();
1.44 + res.tm_hour = dt.Hour();
1.45 + res.tm_mday = dt.Day() + 1;
1.46 + res.tm_mon = dt.Month();
1.47 + res.tm_year = dt.Year() - 1900;
1.48 +
1.49 + // EPOC32 counts the year day as Jan 1st == day 1
1.50 + res.tm_yday = us.DayNoInYear() - 1;
1.51 +
1.52 + // EPOC32 counts the weekdays from 0==Monday to 6==Sunday
1.53 + res.tm_wday = us.DayNoInWeek() + 1;
1.54 + if (res.tm_wday==7)
1.55 + res.tm_wday=0; // Sunday==0 in a struct tm
1.56 +
1.57 + // newlib just sets this field to -1
1.58 + // tm_isdst doesn't really make sense here since we don't
1.59 + // know the locale for which to interpret this time.
1.60 +
1.61 + res.tm_isdst = -1;
1.62 +
1.63 + return res;
1.64 + }
1.65 +
1.66 +static void as_ttime (const struct tm& p, TTime& res, TBool normalise=EFalse)
1.67 + {
1.68 + TDateTime dt;
1.69 + TInt err = dt.Set(p.tm_year+1900, (enum TMonth)p.tm_mon, p.tm_mday-1,
1.70 + p.tm_hour, p.tm_min, p.tm_sec, 0);
1.71 + if (err == KErrNone)
1.72 + {
1.73 + res = dt;
1.74 + return;
1.75 + }
1.76 + if (!normalise)
1.77 + {
1.78 + res = TInt64(-1);
1.79 + return;
1.80 + }
1.81 + // Try to normalise things (for mktime)
1.82 + dt.Set(p.tm_year+1900, EJanuary, 0, 0, 0, 0, 0);
1.83 + res = dt;
1.84 + res += TTimeIntervalMonths (p.tm_mon);
1.85 + res += TTimeIntervalDays (p.tm_mday-1);
1.86 + res += TTimeIntervalHours (p.tm_hour);
1.87 + res += TTimeIntervalMinutes(p.tm_min);
1.88 + res += TTimeIntervalSeconds(p.tm_sec);
1.89 + }
1.90 +
1.91 +inline void as_ttime (const time_t& p, TTime& res)
1.92 + {
1.93 + res = UNIX_BASE + TTimeIntervalSeconds(p);
1.94 + }
1.95 +
1.96 +#endif //__SYMBIAN_COMPILE_UNUSED__
1.97 +
1.98 +
1.99 +GLDEF_C time_t as_time_t(const TTime& t)
1.100 + {
1.101 + TTimeIntervalSeconds res;
1.102 + TInt err = t.SecondsFrom(UNIX_BASE, res);
1.103 + if (err)
1.104 + return -1;
1.105 + else
1.106 + return res.Int();
1.107 + }
1.108 +
1.109 +#ifdef __SYMBIAN_COMPILE_UNUSED__
1.110 +// Utility routine for formatting a TTime into a descriptor using the
1.111 +// UNIX ctime format. NB. EPOC32 abbreviations can be up to KMaxDayNameAbb
1.112 +// and KMaxMonthNameAbb characters (both == 4). The %F is needed to
1.113 +// force the meanings of %D, %Y etc.
1.114 +
1.115 +static TDes8& as_string (TTime& t, TDes8& res)
1.116 + {
1.117 + // UNICODE problem - t.Format operates on TDes => TDes16
1.118 +
1.119 +#if !defined(_UNICODE)
1.120 + TRAPD(err, t.FormatL(res, _L("%F%*E %*N %D %H:%T:%S %Y")));
1.121 +#else
1.122 + TBuf<_ASCTIME_SIZE> unires;
1.123 + TRAPD(err, t.FormatL(unires, _L("%F%*E %*N %D %H:%T:%S %Y")));
1.124 + if (!err)
1.125 + res.Copy(unires);
1.126 +#endif
1.127 + if (err)
1.128 + res = _L8("Error\n");
1.129 + else
1.130 + res.Append('\n');
1.131 + return res;
1.132 + }
1.133 +#endif
1.134 +
1.135 +#ifdef __SYMBIAN_COMPILE_UNUSED__
1.136 +/*
1.137 +Intended Usage: Utility routine for converting from UTC to localtime.
1.138 +*/
1.139 +inline time_t toLocal (const time_t aUniversalTime)
1.140 + {
1.141 +#ifndef __SERIES60_MRT_1_0
1.142 + TTimeIntervalSeconds offset = User::UTCOffset();
1.143 + return aUniversalTime + offset.Int();
1.144 +#else
1.145 + TLocale locale;
1.146 + return aUniversalTime + locale.UniversalTimeOffset().Int();
1.147 +#endif //__SERIES60_MRT_1_0
1.148 + }
1.149 +
1.150 +/*
1.151 +Intended Usage: Utility routine for converting from localtime to UTC.
1.152 + However, having decided that time_t is always Universal time, toGMT is empty.
1.153 +*/
1.154 +inline time_t toGMT (const time_t aLocalTime)
1.155 + {
1.156 + return aLocalTime;
1.157 + }
1.158 +#endif //__SYMBIAN_COMPILE_UNUSED__
1.159 +
1.160 +// external interface for the C library
1.161 +
1.162 +extern "C" {
1.163 +/*
1.164 +Intended Usage: Get current UTC time.
1.165 + Get the number of seconds elapsed since 00:00 hours,
1.166 + Jan 1, 1970 UTC from the system clock.
1.167 +*/
1.168 +EXPORT_C time_t time (time_t* p)
1.169 + {
1.170 + TTime t;
1.171 + t.UniversalTime();
1.172 +
1.173 + time_t res = as_time_t(t);
1.174 + if (p)
1.175 + *p = res;
1.176 + return res;
1.177 + }
1.178 +
1.179 +
1.180 +/*
1.181 +Return number of clock ticks since process start.
1.182 +Returns the number of clock ticks elapsed.
1.183 +A macro constant called CLK_TCK defines the relation betwen
1.184 +clock tick and second (clock ticks per second).
1.185 +*/
1.186 +
1.187 +EXPORT_C clock_t clock ()
1.188 + {
1.189 + int retval=-1;
1.190 + RThread proc;
1.191 + TTimeIntervalMicroSeconds iMicSecsFromEpoc;
1.192 + TInt err=proc.GetCpuTime(iMicSecsFromEpoc);
1.193 +
1.194 + if(err)
1.195 + {
1.196 + return retval;
1.197 + }
1.198 + return I64INT(iMicSecsFromEpoc.Int64());
1.199 + }
1.200 +}// extern "C"
1.201 +
1.202 +