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