1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski
25*b1cdbd2cSJim Jagielski #include "system.h"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
28*b1cdbd2cSJim Jagielski #include <osl/time.h>
29*b1cdbd2cSJim Jagielski
30*b1cdbd2cSJim Jagielski /* FIXME: detection should be done in configure script */
31*b1cdbd2cSJim Jagielski #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || defined(LINUX)
32*b1cdbd2cSJim Jagielski #define STRUCT_TM_HAS_GMTOFF 1
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski #elif defined(SOLARIS)
35*b1cdbd2cSJim Jagielski #define HAS_ALTZONE 1
36*b1cdbd2cSJim Jagielski #endif
37*b1cdbd2cSJim Jagielski
38*b1cdbd2cSJim Jagielski /*--------------------------------------------------
39*b1cdbd2cSJim Jagielski * osl_getSystemTime
40*b1cdbd2cSJim Jagielski *-------------------------------------------------*/
41*b1cdbd2cSJim Jagielski
osl_getSystemTime(TimeValue * TimeValue)42*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL osl_getSystemTime(TimeValue* TimeValue)
43*b1cdbd2cSJim Jagielski {
44*b1cdbd2cSJim Jagielski struct timeval tp;
45*b1cdbd2cSJim Jagielski
46*b1cdbd2cSJim Jagielski /* FIXME: use higher resolution */
47*b1cdbd2cSJim Jagielski gettimeofday(&tp, NULL);
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim Jagielski TimeValue->Seconds = tp.tv_sec;
50*b1cdbd2cSJim Jagielski TimeValue->Nanosec = tp.tv_usec * 1000;
51*b1cdbd2cSJim Jagielski
52*b1cdbd2cSJim Jagielski return (sal_True);
53*b1cdbd2cSJim Jagielski }
54*b1cdbd2cSJim Jagielski
55*b1cdbd2cSJim Jagielski
56*b1cdbd2cSJim Jagielski /*--------------------------------------------------
57*b1cdbd2cSJim Jagielski * osl_getDateTimeFromTimeValue
58*b1cdbd2cSJim Jagielski *-------------------------------------------------*/
59*b1cdbd2cSJim Jagielski
osl_getDateTimeFromTimeValue(TimeValue * pTimeVal,oslDateTime * pDateTime)60*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
61*b1cdbd2cSJim Jagielski {
62*b1cdbd2cSJim Jagielski struct tm *pSystemTime;
63*b1cdbd2cSJim Jagielski struct tm tmBuf;
64*b1cdbd2cSJim Jagielski time_t atime;
65*b1cdbd2cSJim Jagielski
66*b1cdbd2cSJim Jagielski atime = (time_t)pTimeVal->Seconds;
67*b1cdbd2cSJim Jagielski
68*b1cdbd2cSJim Jagielski /* Convert time from type time_t to struct tm */
69*b1cdbd2cSJim Jagielski pSystemTime = gmtime_r( &atime, &tmBuf );
70*b1cdbd2cSJim Jagielski
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski /* Convert struct tm to struct oslDateTime */
73*b1cdbd2cSJim Jagielski if ( pSystemTime != NULL )
74*b1cdbd2cSJim Jagielski {
75*b1cdbd2cSJim Jagielski pDateTime->NanoSeconds = pTimeVal->Nanosec;
76*b1cdbd2cSJim Jagielski pDateTime->Seconds = pSystemTime->tm_sec;
77*b1cdbd2cSJim Jagielski pDateTime->Minutes = pSystemTime->tm_min;
78*b1cdbd2cSJim Jagielski pDateTime->Hours = pSystemTime->tm_hour;
79*b1cdbd2cSJim Jagielski pDateTime->Day = pSystemTime->tm_mday;
80*b1cdbd2cSJim Jagielski pDateTime->DayOfWeek = pSystemTime->tm_wday;
81*b1cdbd2cSJim Jagielski pDateTime->Month = pSystemTime->tm_mon + 1;
82*b1cdbd2cSJim Jagielski pDateTime->Year = pSystemTime->tm_year + 1900;
83*b1cdbd2cSJim Jagielski
84*b1cdbd2cSJim Jagielski return sal_True;
85*b1cdbd2cSJim Jagielski }
86*b1cdbd2cSJim Jagielski
87*b1cdbd2cSJim Jagielski return sal_False;
88*b1cdbd2cSJim Jagielski }
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski /*--------------------------------------------------
91*b1cdbd2cSJim Jagielski * osl_getTimeValueFromDateTime
92*b1cdbd2cSJim Jagielski *--------------------------------------------------*/
93*b1cdbd2cSJim Jagielski
osl_getTimeValueFromDateTime(oslDateTime * pDateTime,TimeValue * pTimeVal)94*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
95*b1cdbd2cSJim Jagielski {
96*b1cdbd2cSJim Jagielski struct tm aTime;
97*b1cdbd2cSJim Jagielski time_t nSeconds;
98*b1cdbd2cSJim Jagielski
99*b1cdbd2cSJim Jagielski /* Convert struct oslDateTime to struct tm */
100*b1cdbd2cSJim Jagielski aTime.tm_sec = pDateTime->Seconds;
101*b1cdbd2cSJim Jagielski aTime.tm_min = pDateTime->Minutes;
102*b1cdbd2cSJim Jagielski aTime.tm_hour = pDateTime->Hours;
103*b1cdbd2cSJim Jagielski aTime.tm_mday = pDateTime->Day;
104*b1cdbd2cSJim Jagielski aTime.tm_wday = pDateTime->DayOfWeek;
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski if ( pDateTime->Month > 0 )
107*b1cdbd2cSJim Jagielski aTime.tm_mon = pDateTime->Month - 1;
108*b1cdbd2cSJim Jagielski else
109*b1cdbd2cSJim Jagielski return sal_False;
110*b1cdbd2cSJim Jagielski
111*b1cdbd2cSJim Jagielski if ( pDateTime->Year >= 1900 )
112*b1cdbd2cSJim Jagielski aTime.tm_year = pDateTime->Year - 1900;
113*b1cdbd2cSJim Jagielski else
114*b1cdbd2cSJim Jagielski return sal_False;
115*b1cdbd2cSJim Jagielski
116*b1cdbd2cSJim Jagielski aTime.tm_isdst = -1;
117*b1cdbd2cSJim Jagielski aTime.tm_wday = 0;
118*b1cdbd2cSJim Jagielski aTime.tm_yday = 0;
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim Jagielski /* Convert time to calendar value */
121*b1cdbd2cSJim Jagielski nSeconds = mktime( &aTime );
122*b1cdbd2cSJim Jagielski
123*b1cdbd2cSJim Jagielski /*
124*b1cdbd2cSJim Jagielski * mktime expects the struct tm to be in local timezone, so we have to adjust
125*b1cdbd2cSJim Jagielski * the returned value to be timezone neutral.
126*b1cdbd2cSJim Jagielski */
127*b1cdbd2cSJim Jagielski
128*b1cdbd2cSJim Jagielski if ( nSeconds != (time_t) -1 )
129*b1cdbd2cSJim Jagielski {
130*b1cdbd2cSJim Jagielski time_t bias;
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski /* timezone corrections */
133*b1cdbd2cSJim Jagielski tzset();
134*b1cdbd2cSJim Jagielski
135*b1cdbd2cSJim Jagielski #if defined(STRUCT_TM_HAS_GMTOFF)
136*b1cdbd2cSJim Jagielski /* members of struct tm are corrected by mktime */
137*b1cdbd2cSJim Jagielski bias = 0 - aTime.tm_gmtoff;
138*b1cdbd2cSJim Jagielski
139*b1cdbd2cSJim Jagielski #elif defined(HAS_ALTZONE)
140*b1cdbd2cSJim Jagielski /* check if daylight saving time is in effect */
141*b1cdbd2cSJim Jagielski bias = aTime.tm_isdst > 0 ? altzone : timezone;
142*b1cdbd2cSJim Jagielski #else
143*b1cdbd2cSJim Jagielski /* exspect daylight saving time to be one hour */
144*b1cdbd2cSJim Jagielski bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
145*b1cdbd2cSJim Jagielski #endif
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski pTimeVal->Seconds = nSeconds;
148*b1cdbd2cSJim Jagielski pTimeVal->Nanosec = pDateTime->NanoSeconds;
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski if ( nSeconds > bias )
151*b1cdbd2cSJim Jagielski pTimeVal->Seconds -= bias;
152*b1cdbd2cSJim Jagielski
153*b1cdbd2cSJim Jagielski return sal_True;
154*b1cdbd2cSJim Jagielski }
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim Jagielski return sal_False;
157*b1cdbd2cSJim Jagielski }
158*b1cdbd2cSJim Jagielski
159*b1cdbd2cSJim Jagielski
160*b1cdbd2cSJim Jagielski /*--------------------------------------------------
161*b1cdbd2cSJim Jagielski * osl_getLocalTimeFromSystemTime
162*b1cdbd2cSJim Jagielski *--------------------------------------------------*/
163*b1cdbd2cSJim Jagielski
osl_getLocalTimeFromSystemTime(TimeValue * pSystemTimeVal,TimeValue * pLocalTimeVal)164*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
165*b1cdbd2cSJim Jagielski {
166*b1cdbd2cSJim Jagielski struct tm *pLocalTime;
167*b1cdbd2cSJim Jagielski struct tm tmBuf;
168*b1cdbd2cSJim Jagielski time_t bias;
169*b1cdbd2cSJim Jagielski time_t atime;
170*b1cdbd2cSJim Jagielski
171*b1cdbd2cSJim Jagielski atime = (time_t) pSystemTimeVal->Seconds;
172*b1cdbd2cSJim Jagielski pLocalTime = localtime_r( &atime, &tmBuf );
173*b1cdbd2cSJim Jagielski
174*b1cdbd2cSJim Jagielski #if defined(STRUCT_TM_HAS_GMTOFF)
175*b1cdbd2cSJim Jagielski /* members of struct tm are corrected by mktime */
176*b1cdbd2cSJim Jagielski bias = 0 - pLocalTime->tm_gmtoff;
177*b1cdbd2cSJim Jagielski
178*b1cdbd2cSJim Jagielski #elif defined(HAS_ALTZONE)
179*b1cdbd2cSJim Jagielski /* check if daylight saving time is in effect */
180*b1cdbd2cSJim Jagielski bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
181*b1cdbd2cSJim Jagielski #else
182*b1cdbd2cSJim Jagielski /* exspect daylight saving time to be one hour */
183*b1cdbd2cSJim Jagielski bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
184*b1cdbd2cSJim Jagielski #endif
185*b1cdbd2cSJim Jagielski
186*b1cdbd2cSJim Jagielski if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
187*b1cdbd2cSJim Jagielski {
188*b1cdbd2cSJim Jagielski pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
189*b1cdbd2cSJim Jagielski pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
190*b1cdbd2cSJim Jagielski
191*b1cdbd2cSJim Jagielski return sal_True;
192*b1cdbd2cSJim Jagielski }
193*b1cdbd2cSJim Jagielski
194*b1cdbd2cSJim Jagielski return sal_False;
195*b1cdbd2cSJim Jagielski }
196*b1cdbd2cSJim Jagielski
197*b1cdbd2cSJim Jagielski /*--------------------------------------------------
198*b1cdbd2cSJim Jagielski * osl_getSystemTimeFromLocalTime
199*b1cdbd2cSJim Jagielski *--------------------------------------------------*/
200*b1cdbd2cSJim Jagielski
osl_getSystemTimeFromLocalTime(TimeValue * pLocalTimeVal,TimeValue * pSystemTimeVal)201*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
202*b1cdbd2cSJim Jagielski {
203*b1cdbd2cSJim Jagielski struct tm *pLocalTime;
204*b1cdbd2cSJim Jagielski struct tm tmBuf;
205*b1cdbd2cSJim Jagielski time_t bias;
206*b1cdbd2cSJim Jagielski time_t atime;
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski atime = (time_t) pLocalTimeVal->Seconds;
209*b1cdbd2cSJim Jagielski
210*b1cdbd2cSJim Jagielski /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
211*b1cdbd2cSJim Jagielski * the timezone offset for the local time for the GMT equivalent time. Note
212*b1cdbd2cSJim Jagielski * that we cannot directly use local time to determine the timezone offset
213*b1cdbd2cSJim Jagielski * because GMT is the only reliable time that we can determine timezone
214*b1cdbd2cSJim Jagielski * offset from.
215*b1cdbd2cSJim Jagielski */
216*b1cdbd2cSJim Jagielski
217*b1cdbd2cSJim Jagielski atime = mktime( gmtime_r( &atime, &tmBuf ) );
218*b1cdbd2cSJim Jagielski pLocalTime = localtime_r( &atime, &tmBuf );
219*b1cdbd2cSJim Jagielski
220*b1cdbd2cSJim Jagielski #if defined(STRUCT_TM_HAS_GMTOFF)
221*b1cdbd2cSJim Jagielski /* members of struct tm are corrected by mktime */
222*b1cdbd2cSJim Jagielski bias = 0 - pLocalTime->tm_gmtoff;
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski #elif defined(HAS_ALTZONE)
225*b1cdbd2cSJim Jagielski /* check if daylight saving time is in effect */
226*b1cdbd2cSJim Jagielski bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
227*b1cdbd2cSJim Jagielski #else
228*b1cdbd2cSJim Jagielski /* exspect daylight saving time to be one hour */
229*b1cdbd2cSJim Jagielski bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
230*b1cdbd2cSJim Jagielski #endif
231*b1cdbd2cSJim Jagielski
232*b1cdbd2cSJim Jagielski if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
233*b1cdbd2cSJim Jagielski {
234*b1cdbd2cSJim Jagielski pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
235*b1cdbd2cSJim Jagielski pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
236*b1cdbd2cSJim Jagielski
237*b1cdbd2cSJim Jagielski return sal_True;
238*b1cdbd2cSJim Jagielski }
239*b1cdbd2cSJim Jagielski
240*b1cdbd2cSJim Jagielski return sal_False;
241*b1cdbd2cSJim Jagielski }
242*b1cdbd2cSJim Jagielski
243*b1cdbd2cSJim Jagielski
244*b1cdbd2cSJim Jagielski
245*b1cdbd2cSJim Jagielski static struct timeval startTime;
246*b1cdbd2cSJim Jagielski static sal_Bool bGlobalTimer = sal_False;
247*b1cdbd2cSJim Jagielski
osl_getGlobalTimer()248*b1cdbd2cSJim Jagielski sal_uInt32 SAL_CALL osl_getGlobalTimer()
249*b1cdbd2cSJim Jagielski {
250*b1cdbd2cSJim Jagielski struct timeval currentTime;
251*b1cdbd2cSJim Jagielski sal_uInt32 nSeconds;
252*b1cdbd2cSJim Jagielski
253*b1cdbd2cSJim Jagielski // FIXME: not thread safe !!
254*b1cdbd2cSJim Jagielski if ( bGlobalTimer == sal_False )
255*b1cdbd2cSJim Jagielski {
256*b1cdbd2cSJim Jagielski gettimeofday( &startTime, NULL );
257*b1cdbd2cSJim Jagielski bGlobalTimer=sal_True;
258*b1cdbd2cSJim Jagielski }
259*b1cdbd2cSJim Jagielski
260*b1cdbd2cSJim Jagielski gettimeofday( ¤tTime, NULL );
261*b1cdbd2cSJim Jagielski
262*b1cdbd2cSJim Jagielski nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
263*b1cdbd2cSJim Jagielski
264*b1cdbd2cSJim Jagielski return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
265*b1cdbd2cSJim Jagielski }
266