update dev300-m58
[ooovba.git] / sal / osl / os2 / time.c
bloba48a47af1b8af913f82d8b2957cdc37c06c01e97
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: time.c,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
32 #include "system.h"
34 #include <osl/diagnose.h>
35 #include <osl/time.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)
42 #define HAS_ALTZONE 1
43 #endif
45 /*--------------------------------------------------
46 * osl_getSystemTime
47 *-------------------------------------------------*/
49 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* TimeValue)
51 struct timeval tp;
53 /* FIXME: use higher resolution */
54 gettimeofday(&tp, NULL);
56 TimeValue->Seconds = tp.tv_sec;
57 TimeValue->Nanosec = tp.tv_usec * 1000;
59 return (sal_True);
63 /*--------------------------------------------------
64 * osl_getDateTimeFromTimeValue
65 *-------------------------------------------------*/
67 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
69 struct tm *pSystemTime;
70 struct tm tmBuf;
71 time_t atime;
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;
91 return sal_True;
94 return sal_False;
97 /*--------------------------------------------------
98 * osl_getTimeValueFromDateTime
99 *--------------------------------------------------*/
101 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
103 struct tm aTime;
104 time_t nSeconds;
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;
115 else
116 return sal_False;
118 if ( pDateTime->Year >= 1900 )
119 aTime.tm_year = pDateTime->Year - 1900;
120 else
121 return sal_False;
123 aTime.tm_isdst = -1;
124 aTime.tm_wday = 0;
125 aTime.tm_yday = 0;
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 )
137 time_t bias;
139 /* timezone corrections */
140 tzset();
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;
149 #else
150 /* exspect daylight saving time to be one hour */
151 bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
152 #endif
154 pTimeVal->Seconds = nSeconds;
155 pTimeVal->Nanosec = pDateTime->NanoSeconds;
157 if ( nSeconds > bias )
158 pTimeVal->Seconds -= bias;
160 return sal_True;
163 return sal_False;
167 /*--------------------------------------------------
168 * osl_getLocalTimeFromSystemTime
169 *--------------------------------------------------*/
171 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
173 struct tm *pLocalTime;
174 struct tm tmBuf;
175 time_t bias;
176 time_t atime;
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;
188 #else
189 /* exspect daylight saving time to be one hour */
190 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
191 #endif
193 if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
195 pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
196 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
198 return sal_True;
201 return sal_False;
204 /*--------------------------------------------------
205 * osl_getSystemTimeFromLocalTime
206 *--------------------------------------------------*/
208 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
210 struct tm *pLocalTime;
211 struct tm tmBuf;
212 time_t bias;
213 time_t atime;
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
221 * offset from.
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;
234 #else
235 /* exspect daylight saving time to be one hour */
236 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
237 #endif
239 if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
241 pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
242 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
244 return sal_True;
247 return sal_False;
252 static struct timeval startTime;
253 static sal_Bool bGlobalTimer = sal_False;
255 sal_uInt32 SAL_CALL osl_getGlobalTimer()
257 struct timeval currentTime;
258 sal_uInt32 nSeconds;
260 // FIXME: not thread safe !!
261 if ( bGlobalTimer == sal_False )
263 gettimeofday( &startTime, NULL );
264 bGlobalTimer=sal_True;
267 gettimeofday( &currentTime, NULL );
269 nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
271 return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );