1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <sal/config.h>
21
22 #include "saltime.hxx"
23 #include "system.hxx"
24
25 #include <osl/time.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #ifdef __MACH__
30 #include <mach/clock.h>
31 #include <mach/mach.h>
32 #endif
33
34 /* FIXME: detection should be done in configure script */
35 #if defined(MACOSX) || defined(IOS) || defined(FREEBSD) || defined(NETBSD) || \
36 defined(LINUX) || defined(OPENBSD) || defined(DRAGONFLY)
37 #define STRUCT_TM_HAS_GMTOFF 1
38
39 #elif defined(__sun)
40 #define HAS_ALTZONE 1
41 #endif
42
43 #ifdef __MACH__
44 typedef mach_timespec_t osl_time_t;
45 #else
46 #if defined(_POSIX_TIMERS)
47 #define USE_CLOCK_GETTIME
48 typedef struct timespec osl_time_t;
49 #else
50 typedef struct timeval osl_time_t;
51 #endif
52 #endif
53 static osl_time_t startTime;
54
osl_getSystemTime(TimeValue * tv)55 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
56 {
57 #ifdef __MACH__
58 clock_serv_t cclock;
59 mach_timespec_t mts;
60
61 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
62 clock_get_time(cclock, &mts);
63 mach_port_deallocate(mach_task_self(), cclock);
64
65 tv->Seconds = mts.tv_sec;
66 tv->Nanosec = mts.tv_nsec;
67 #else
68 int res;
69 osl_time_t tp;
70 #if defined(USE_CLOCK_GETTIME)
71 res = clock_gettime(CLOCK_REALTIME, &tp);
72 #else
73 res = gettimeofday(&tp, NULL);
74 #endif
75
76 if (res != 0)
77 {
78 return false;
79 }
80
81 tv->Seconds = tp.tv_sec;
82 #if defined(USE_CLOCK_GETTIME)
83 tv->Nanosec = tp.tv_nsec;
84 #else
85 tv->Nanosec = tp.tv_usec * 1000;
86 #endif
87 #endif
88 return true;
89 }
90
osl_getDateTimeFromTimeValue(const TimeValue * pTimeVal,oslDateTime * pDateTime)91 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
92 {
93 struct tm *pSystemTime;
94 struct tm tmBuf;
95 time_t atime;
96
97 atime = static_cast<time_t>(pTimeVal->Seconds);
98
99 /* Convert time from type time_t to struct tm */
100 pSystemTime = gmtime_r( &atime, &tmBuf );
101
102 /* Convert struct tm to struct oslDateTime */
103 if ( pSystemTime != nullptr )
104 {
105 pDateTime->NanoSeconds = pTimeVal->Nanosec;
106 pDateTime->Seconds = pSystemTime->tm_sec;
107 pDateTime->Minutes = pSystemTime->tm_min;
108 pDateTime->Hours = pSystemTime->tm_hour;
109 pDateTime->Day = pSystemTime->tm_mday;
110 pDateTime->DayOfWeek = pSystemTime->tm_wday;
111 pDateTime->Month = pSystemTime->tm_mon + 1;
112 pDateTime->Year = pSystemTime->tm_year + 1900;
113
114 return true;
115 }
116
117 return false;
118 }
119
osl_getTimeValueFromDateTime(const oslDateTime * pDateTime,TimeValue * pTimeVal)120 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
121 {
122 struct tm aTime;
123 time_t nSeconds;
124
125 /* Convert struct oslDateTime to struct tm */
126 aTime.tm_sec = pDateTime->Seconds;
127 aTime.tm_min = pDateTime->Minutes;
128 aTime.tm_hour = pDateTime->Hours;
129 aTime.tm_mday = pDateTime->Day;
130
131 if ( pDateTime->Month > 0 )
132 aTime.tm_mon = pDateTime->Month - 1;
133 else
134 return false;
135
136 aTime.tm_year = pDateTime->Year - 1900;
137
138 aTime.tm_isdst = -1;
139 aTime.tm_wday = 0;
140 aTime.tm_yday = 0;
141
142 #if defined(STRUCT_TM_HAS_GMTOFF)
143 aTime.tm_gmtoff = 0;
144 #endif
145
146 /* Convert time to calendar value */
147 nSeconds = mktime( &aTime );
148
149 /*
150 * mktime expects the struct tm to be in local timezone, so we have to adjust
151 * the returned value to be timezone neutral.
152 */
153
154 if ( nSeconds != time_t(-1) )
155 {
156 time_t bias;
157
158 /* timezone corrections */
159 tzset();
160
161 #if defined(STRUCT_TM_HAS_GMTOFF)
162 /* members of struct tm are corrected by mktime */
163 bias = 0 - aTime.tm_gmtoff;
164
165 #elif defined(HAS_ALTZONE)
166 /* check if daylight saving time is in effect */
167 bias = aTime.tm_isdst > 0 ? altzone : timezone;
168 #else
169 /* expect daylight saving time to be one hour */
170 bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
171 #endif
172
173 pTimeVal->Seconds = nSeconds;
174 pTimeVal->Nanosec = pDateTime->NanoSeconds;
175
176 if ( nSeconds > bias )
177 pTimeVal->Seconds -= bias;
178
179 return true;
180 }
181
182 return false;
183 }
184
osl_getLocalTimeFromSystemTime(const TimeValue * pSystemTimeVal,TimeValue * pLocalTimeVal)185 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
186 {
187 struct tm *pLocalTime;
188 struct tm tmBuf;
189 time_t bias;
190 time_t atime;
191
192 atime = static_cast<time_t>(pSystemTimeVal->Seconds);
193 pLocalTime = localtime_r( &atime, &tmBuf );
194
195 #if defined(STRUCT_TM_HAS_GMTOFF)
196 /* members of struct tm are corrected by mktime */
197 bias = -pLocalTime->tm_gmtoff;
198
199 #elif defined(HAS_ALTZONE)
200 /* check if daylight saving time is in effect */
201 bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
202 #else
203 /* expect daylight saving time to be one hour */
204 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
205 #endif
206
207 if ( static_cast<sal_Int64>(pSystemTimeVal->Seconds) > bias )
208 {
209 pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
210 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
211
212 return true;
213 }
214
215 return false;
216 }
217
osl_getSystemTimeFromLocalTime(const TimeValue * pLocalTimeVal,TimeValue * pSystemTimeVal)218 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
219 {
220 struct tm *pLocalTime;
221 struct tm tmBuf;
222 time_t bias;
223 time_t atime;
224
225 atime = static_cast<time_t>(pLocalTimeVal->Seconds);
226
227 /* Convert atime, which is a local time, to its GMT equivalent. Then, get
228 * the timezone offset for the local time for the GMT equivalent time. Note
229 * that we cannot directly use local time to determine the timezone offset
230 * because GMT is the only reliable time that we can determine timezone
231 * offset from.
232 */
233
234 atime = mktime( gmtime_r( &atime, &tmBuf ) );
235 pLocalTime = localtime_r( &atime, &tmBuf );
236
237 #if defined(STRUCT_TM_HAS_GMTOFF)
238 /* members of struct tm are corrected by mktime */
239 bias = 0 - pLocalTime->tm_gmtoff;
240
241 #elif defined(HAS_ALTZONE)
242 /* check if daylight saving time is in effect */
243 bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
244 #else
245 /* expect daylight saving time to be one hour */
246 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
247 #endif
248
249 if ( static_cast<sal_Int64>(pLocalTimeVal->Seconds) + bias > 0 )
250 {
251 pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
252 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
253
254 return true;
255 }
256
257 return false;
258 }
259
sal_initGlobalTimer()260 void sal_initGlobalTimer()
261 {
262 #ifdef __MACH__
263 clock_serv_t cclock;
264
265 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
266 clock_get_time(cclock, &startTime);
267 mach_port_deallocate(mach_task_self(), cclock);
268 #else /* ! (MACOSX || IOS) */
269 #if defined(USE_CLOCK_GETTIME)
270 clock_gettime(CLOCK_REALTIME, &startTime);
271 #else /* Ndef USE_CLOCK_GETTIME */
272 gettimeofday( &startTime, NULL );
273 #endif /* NDef USE_CLOCK_GETTIME */
274 #endif /* ! (MACOSX || IOS) */
275 }
276
osl_getGlobalTimer()277 sal_uInt32 SAL_CALL osl_getGlobalTimer()
278 {
279 sal_uInt32 nSeconds;
280
281 #ifdef __MACH__
282 clock_serv_t cclock;
283 mach_timespec_t currentTime;
284
285 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
286 clock_get_time(cclock, ¤tTime);
287 mach_port_deallocate(mach_task_self(), cclock);
288
289 nSeconds = ( currentTime.tv_sec - startTime.tv_sec );
290 nSeconds = ( nSeconds * 1000 ) + static_cast<long>(( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
291 #else
292 osl_time_t currentTime;
293
294 #if defined(USE_CLOCK_GETTIME)
295 clock_gettime(CLOCK_REALTIME, ¤tTime);
296 #else
297 gettimeofday( ¤tTime, NULL );
298 #endif
299
300 nSeconds = static_cast<sal_uInt32>( currentTime.tv_sec - startTime.tv_sec );
301 #if defined(USE_CLOCK_GETTIME)
302 nSeconds = ( nSeconds * 1000 ) + static_cast<long>(( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
303 #else
304 nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
305 #endif
306 #endif
307 return nSeconds;
308 }
309
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
311