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