Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "system.h"
31 : :
32 : : #include <osl/diagnose.h>
33 : : #include <osl/time.h>
34 : :
35 : : /* FIXME: detection should be done in configure script */
36 : : #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
37 : : defined(LINUX) || defined(OPENBSD) || defined(DRAGONFLY)
38 : : #define STRUCT_TM_HAS_GMTOFF 1
39 : :
40 : : #elif defined(SOLARIS)
41 : : #define HAS_ALTZONE 1
42 : : #endif
43 : :
44 : : /*--------------------------------------------------
45 : : * osl_getSystemTime
46 : : *-------------------------------------------------*/
47 : :
48 : 34415 : sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
49 : : {
50 : : struct timeval tp;
51 : :
52 : : /* FIXME: use higher resolution */
53 : 34415 : gettimeofday(&tp, NULL);
54 : :
55 : 34415 : tv->Seconds = tp.tv_sec;
56 : 34415 : tv->Nanosec = tp.tv_usec * 1000;
57 : :
58 : 34415 : return (sal_True);
59 : : }
60 : :
61 : :
62 : : /*--------------------------------------------------
63 : : * osl_getDateTimeFromTimeValue
64 : : *-------------------------------------------------*/
65 : :
66 : 62435 : sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
67 : : {
68 : : struct tm *pSystemTime;
69 : : struct tm tmBuf;
70 : : time_t atime;
71 : :
72 : 62435 : atime = (time_t)pTimeVal->Seconds;
73 : :
74 : : /* Convert time from type time_t to struct tm */
75 : 62435 : pSystemTime = gmtime_r( &atime, &tmBuf );
76 : :
77 : :
78 : : /* Convert struct tm to struct oslDateTime */
79 [ + - ]: 62435 : if ( pSystemTime != NULL )
80 : : {
81 : 62435 : pDateTime->NanoSeconds = pTimeVal->Nanosec;
82 : 62435 : pDateTime->Seconds = pSystemTime->tm_sec;
83 : 62435 : pDateTime->Minutes = pSystemTime->tm_min;
84 : 62435 : pDateTime->Hours = pSystemTime->tm_hour;
85 : 62435 : pDateTime->Day = pSystemTime->tm_mday;
86 : 62435 : pDateTime->DayOfWeek = pSystemTime->tm_wday;
87 : 62435 : pDateTime->Month = pSystemTime->tm_mon + 1;
88 : 62435 : pDateTime->Year = pSystemTime->tm_year + 1900;
89 : :
90 : 62435 : return sal_True;
91 : : }
92 : :
93 : 62435 : return sal_False;
94 : : }
95 : :
96 : : /*--------------------------------------------------
97 : : * osl_getTimeValueFromDateTime
98 : : *--------------------------------------------------*/
99 : :
100 : 0 : sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
101 : : {
102 : : struct tm aTime;
103 : : time_t nSeconds;
104 : :
105 : : /* Convert struct oslDateTime to struct tm */
106 : 0 : aTime.tm_sec = pDateTime->Seconds;
107 : 0 : aTime.tm_min = pDateTime->Minutes;
108 : 0 : aTime.tm_hour = pDateTime->Hours;
109 : 0 : aTime.tm_mday = pDateTime->Day;
110 : 0 : aTime.tm_wday = pDateTime->DayOfWeek;
111 : :
112 [ # # ]: 0 : if ( pDateTime->Month > 0 )
113 : 0 : aTime.tm_mon = pDateTime->Month - 1;
114 : : else
115 : 0 : return sal_False;
116 : :
117 [ # # ]: 0 : if ( pDateTime->Year >= 1900 )
118 : 0 : aTime.tm_year = pDateTime->Year - 1900;
119 : : else
120 : 0 : return sal_False;
121 : :
122 : 0 : aTime.tm_isdst = -1;
123 : 0 : aTime.tm_wday = 0;
124 : 0 : aTime.tm_yday = 0;
125 : :
126 : : /* Convert time to calendar value */
127 : 0 : nSeconds = mktime( &aTime );
128 : :
129 : : /*
130 : : * mktime expects the struct tm to be in local timezone, so we have to adjust
131 : : * the returned value to be timezone neutral.
132 : : */
133 : :
134 [ # # ]: 0 : if ( nSeconds != (time_t) -1 )
135 : : {
136 : : time_t bias;
137 : :
138 : : /* timezone corrections */
139 : 0 : tzset();
140 : :
141 : : #if defined(STRUCT_TM_HAS_GMTOFF)
142 : : /* members of struct tm are corrected by mktime */
143 : 0 : bias = 0 - aTime.tm_gmtoff;
144 : :
145 : : #elif defined(HAS_ALTZONE)
146 : : /* check if daylight saving time is in effect */
147 : : bias = aTime.tm_isdst > 0 ? altzone : timezone;
148 : : #else
149 : : /* exspect daylight saving time to be one hour */
150 : : bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
151 : : #endif
152 : :
153 : 0 : pTimeVal->Seconds = nSeconds;
154 : 0 : pTimeVal->Nanosec = pDateTime->NanoSeconds;
155 : :
156 [ # # ]: 0 : if ( nSeconds > bias )
157 : 0 : pTimeVal->Seconds -= bias;
158 : :
159 : 0 : return sal_True;
160 : : }
161 : :
162 : 0 : return sal_False;
163 : : }
164 : :
165 : :
166 : : /*--------------------------------------------------
167 : : * osl_getLocalTimeFromSystemTime
168 : : *--------------------------------------------------*/
169 : :
170 : 60126 : sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
171 : : {
172 : : struct tm *pLocalTime;
173 : : struct tm tmBuf;
174 : : time_t bias;
175 : : time_t atime;
176 : :
177 : 60126 : atime = (time_t) pSystemTimeVal->Seconds;
178 : 60126 : pLocalTime = localtime_r( &atime, &tmBuf );
179 : :
180 : : #if defined(STRUCT_TM_HAS_GMTOFF)
181 : : /* members of struct tm are corrected by mktime */
182 : 60126 : bias = 0 - pLocalTime->tm_gmtoff;
183 : :
184 : : #elif defined(HAS_ALTZONE)
185 : : /* check if daylight saving time is in effect */
186 : : bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
187 : : #else
188 : : /* exspect daylight saving time to be one hour */
189 : : bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
190 : : #endif
191 : :
192 [ + - ]: 60126 : if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
193 : : {
194 : 60126 : pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
195 : 60126 : pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
196 : :
197 : 60126 : return sal_True;
198 : : }
199 : :
200 : 60126 : return sal_False;
201 : : }
202 : :
203 : : /*--------------------------------------------------
204 : : * osl_getSystemTimeFromLocalTime
205 : : *--------------------------------------------------*/
206 : :
207 : 0 : sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
208 : : {
209 : : struct tm *pLocalTime;
210 : : struct tm tmBuf;
211 : : time_t bias;
212 : : time_t atime;
213 : :
214 : 0 : atime = (time_t) pLocalTimeVal->Seconds;
215 : :
216 : : /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
217 : : * the timezone offset for the local time for the GMT equivalent time. Note
218 : : * that we cannot directly use local time to determine the timezone offset
219 : : * because GMT is the only reliable time that we can determine timezone
220 : : * offset from.
221 : : */
222 : :
223 : 0 : atime = mktime( gmtime_r( &atime, &tmBuf ) );
224 : 0 : pLocalTime = localtime_r( &atime, &tmBuf );
225 : :
226 : : #if defined(STRUCT_TM_HAS_GMTOFF)
227 : : /* members of struct tm are corrected by mktime */
228 : 0 : bias = 0 - pLocalTime->tm_gmtoff;
229 : :
230 : : #elif defined(HAS_ALTZONE)
231 : : /* check if daylight saving time is in effect */
232 : : bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
233 : : #else
234 : : /* exspect daylight saving time to be one hour */
235 : : bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
236 : : #endif
237 : :
238 [ # # ]: 0 : if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
239 : : {
240 : 0 : pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
241 : 0 : pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
242 : :
243 : 0 : return sal_True;
244 : : }
245 : :
246 : 0 : return sal_False;
247 : : }
248 : :
249 : :
250 : :
251 : : static struct timeval startTime;
252 : : static sal_Bool bGlobalTimer = sal_False;
253 : :
254 : 8426 : sal_uInt32 SAL_CALL osl_getGlobalTimer()
255 : : {
256 : : struct timeval currentTime;
257 : : sal_uInt32 nSeconds;
258 : :
259 : : // FIXME: not thread safe !!
260 [ + + ]: 8426 : if ( bGlobalTimer == sal_False )
261 : : {
262 : 3013 : gettimeofday( &startTime, NULL );
263 : 3013 : bGlobalTimer=sal_True;
264 : : }
265 : :
266 : 8426 : gettimeofday( ¤tTime, NULL );
267 : :
268 : 8426 : nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
269 : :
270 : 8426 : return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
271 : : }
272 : :
273 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|