1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <osl/diagnose.h>
37 /* FIXME: detection should be done in configure script */
38 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || defined(LINUX)
39 #define STRUCT_TM_HAS_GMTOFF 1
41 #elif defined(SOLARIS)
45 /*--------------------------------------------------
47 *-------------------------------------------------*/
49 sal_Bool SAL_CALL
osl_getSystemTime(TimeValue
* TimeValue
)
53 /* FIXME: use higher resolution */
54 gettimeofday(&tp
, NULL
);
56 TimeValue
->Seconds
= tp
.tv_sec
;
57 TimeValue
->Nanosec
= tp
.tv_usec
* 1000;
63 /*--------------------------------------------------
64 * osl_getDateTimeFromTimeValue
65 *-------------------------------------------------*/
67 sal_Bool SAL_CALL
osl_getDateTimeFromTimeValue( TimeValue
* pTimeVal
, oslDateTime
* pDateTime
)
69 struct tm
*pSystemTime
;
73 atime
= (time_t)pTimeVal
->Seconds
;
75 /* Convert time from type time_t to struct tm */
76 pSystemTime
= gmtime_r( &atime
, &tmBuf
);
79 /* Convert struct tm to struct oslDateTime */
80 if ( pSystemTime
!= NULL
)
82 pDateTime
->NanoSeconds
= pTimeVal
->Nanosec
;
83 pDateTime
->Seconds
= pSystemTime
->tm_sec
;
84 pDateTime
->Minutes
= pSystemTime
->tm_min
;
85 pDateTime
->Hours
= pSystemTime
->tm_hour
;
86 pDateTime
->Day
= pSystemTime
->tm_mday
;
87 pDateTime
->DayOfWeek
= pSystemTime
->tm_wday
;
88 pDateTime
->Month
= pSystemTime
->tm_mon
+ 1;
89 pDateTime
->Year
= pSystemTime
->tm_year
+ 1900;
97 /*--------------------------------------------------
98 * osl_getTimeValueFromDateTime
99 *--------------------------------------------------*/
101 sal_Bool SAL_CALL
osl_getTimeValueFromDateTime( oslDateTime
* pDateTime
, TimeValue
* pTimeVal
)
106 /* Convert struct oslDateTime to struct tm */
107 aTime
.tm_sec
= pDateTime
->Seconds
;
108 aTime
.tm_min
= pDateTime
->Minutes
;
109 aTime
.tm_hour
= pDateTime
->Hours
;
110 aTime
.tm_mday
= pDateTime
->Day
;
111 aTime
.tm_wday
= pDateTime
->DayOfWeek
;
113 if ( pDateTime
->Month
> 0 )
114 aTime
.tm_mon
= pDateTime
->Month
- 1;
118 if ( pDateTime
->Year
>= 1900 )
119 aTime
.tm_year
= pDateTime
->Year
- 1900;
127 /* Convert time to calendar value */
128 nSeconds
= mktime( &aTime
);
131 * mktime expects the struct tm to be in local timezone, so we have to adjust
132 * the returned value to be timezone neutral.
135 if ( nSeconds
!= (time_t) -1 )
139 /* timezone corrections */
142 #if defined(STRUCT_TM_HAS_GMTOFF)
143 /* members of struct tm are corrected by mktime */
144 bias
= 0 - aTime
.tm_gmtoff
;
146 #elif defined(HAS_ALTZONE)
147 /* check if daylight saving time is in effect */
148 bias
= aTime
.tm_isdst
> 0 ? altzone
: timezone
;
150 /* exspect daylight saving time to be one hour */
151 bias
= aTime
.tm_isdst
> 0 ? timezone
- 3600 : timezone
;
154 pTimeVal
->Seconds
= nSeconds
;
155 pTimeVal
->Nanosec
= pDateTime
->NanoSeconds
;
157 if ( nSeconds
> bias
)
158 pTimeVal
->Seconds
-= bias
;
167 /*--------------------------------------------------
168 * osl_getLocalTimeFromSystemTime
169 *--------------------------------------------------*/
171 sal_Bool SAL_CALL
osl_getLocalTimeFromSystemTime( TimeValue
* pSystemTimeVal
, TimeValue
* pLocalTimeVal
)
173 struct tm
*pLocalTime
;
178 atime
= (time_t) pSystemTimeVal
->Seconds
;
179 pLocalTime
= localtime_r( &atime
, &tmBuf
);
181 #if defined(STRUCT_TM_HAS_GMTOFF)
182 /* members of struct tm are corrected by mktime */
183 bias
= 0 - pLocalTime
->tm_gmtoff
;
185 #elif defined(HAS_ALTZONE)
186 /* check if daylight saving time is in effect */
187 bias
= pLocalTime
->tm_isdst
> 0 ? altzone
: timezone
;
189 /* exspect daylight saving time to be one hour */
190 bias
= pLocalTime
->tm_isdst
> 0 ? timezone
- 3600 : timezone
;
193 if ( (sal_Int64
) pSystemTimeVal
->Seconds
> bias
)
195 pLocalTimeVal
->Seconds
= pSystemTimeVal
->Seconds
- bias
;
196 pLocalTimeVal
->Nanosec
= pSystemTimeVal
->Nanosec
;
204 /*--------------------------------------------------
205 * osl_getSystemTimeFromLocalTime
206 *--------------------------------------------------*/
208 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
210 struct tm
*pLocalTime
;
215 atime
= (time_t) pLocalTimeVal
->Seconds
;
217 /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
218 * the timezone offset for the local time for the GMT equivalent time. Note
219 * that we cannot directly use local time to determine the timezone offset
220 * because GMT is the only reliable time that we can determine timezone
224 atime
= mktime( gmtime_r( &atime
, &tmBuf
) );
225 pLocalTime
= localtime_r( &atime
, &tmBuf
);
227 #if defined(STRUCT_TM_HAS_GMTOFF)
228 /* members of struct tm are corrected by mktime */
229 bias
= 0 - pLocalTime
->tm_gmtoff
;
231 #elif defined(HAS_ALTZONE)
232 /* check if daylight saving time is in effect */
233 bias
= pLocalTime
->tm_isdst
> 0 ? altzone
: timezone
;
235 /* exspect daylight saving time to be one hour */
236 bias
= pLocalTime
->tm_isdst
> 0 ? timezone
- 3600 : timezone
;
239 if ( (sal_Int64
) pLocalTimeVal
->Seconds
+ bias
> 0 )
241 pSystemTimeVal
->Seconds
= pLocalTimeVal
->Seconds
+ bias
;
242 pSystemTimeVal
->Nanosec
= pLocalTimeVal
->Nanosec
;
252 static struct timeval startTime
;
253 static sal_Bool bGlobalTimer
= sal_False
;
255 sal_uInt32 SAL_CALL
osl_getGlobalTimer()
257 struct timeval currentTime
;
260 // FIXME: not thread safe !!
261 if ( bGlobalTimer
== sal_False
)
263 gettimeofday( &startTime
, NULL
);
264 bGlobalTimer
=sal_True
;
267 gettimeofday( ¤tTime
, NULL
);
269 nSeconds
= (sal_uInt32
)( currentTime
.tv_sec
- startTime
.tv_sec
);
271 return ( nSeconds
* 1000 ) + (long) (( currentTime
.tv_usec
- startTime
.tv_usec
) / 1000 );