lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sal / osl / unx / time.cxx
blob4bd3003ef7d5126d381951048e03ad73d3199825
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include "saltime.hxx"
23 #include "system.hxx"
25 #include <osl/time.h>
26 #include <time.h>
27 #include <unistd.h>
29 #ifdef __MACH__
30 #include <mach/clock.h>
31 #include <mach/mach.h>
32 #endif
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
39 #elif defined(__sun)
40 #define HAS_ALTZONE 1
41 #endif
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;
55 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
57 #ifdef __MACH__
58 clock_serv_t cclock;
59 mach_timespec_t mts;
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);
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
76 if (res != 0)
78 return false;
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;
91 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
93 struct tm *pSystemTime;
94 struct tm tmBuf;
95 time_t atime;
97 atime = static_cast<time_t>(pTimeVal->Seconds);
99 /* Convert time from type time_t to struct tm */
100 pSystemTime = gmtime_r( &atime, &tmBuf );
102 /* Convert struct tm to struct oslDateTime */
103 if ( pSystemTime != nullptr )
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;
114 return true;
117 return false;
120 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
122 struct tm aTime;
123 time_t nSeconds;
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 aTime.tm_wday = pDateTime->DayOfWeek;
132 if ( pDateTime->Month > 0 )
133 aTime.tm_mon = pDateTime->Month - 1;
134 else
135 return false;
137 aTime.tm_year = pDateTime->Year - 1900;
139 aTime.tm_isdst = -1;
140 aTime.tm_wday = 0;
141 aTime.tm_yday = 0;
143 #if defined(STRUCT_TM_HAS_GMTOFF)
144 aTime.tm_gmtoff = 0;
145 #endif
147 /* Convert time to calendar value */
148 nSeconds = mktime( &aTime );
151 * mktime expects the struct tm to be in local timezone, so we have to adjust
152 * the returned value to be timezone neutral.
155 if ( nSeconds != time_t(-1) )
157 time_t bias;
159 /* timezone corrections */
160 tzset();
162 #if defined(STRUCT_TM_HAS_GMTOFF)
163 /* members of struct tm are corrected by mktime */
164 bias = 0 - aTime.tm_gmtoff;
166 #elif defined(HAS_ALTZONE)
167 /* check if daylight saving time is in effect */
168 bias = aTime.tm_isdst > 0 ? altzone : timezone;
169 #else
170 /* exspect daylight saving time to be one hour */
171 bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
172 #endif
174 pTimeVal->Seconds = nSeconds;
175 pTimeVal->Nanosec = pDateTime->NanoSeconds;
177 if ( nSeconds > bias )
178 pTimeVal->Seconds -= bias;
180 return true;
183 return false;
186 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
188 struct tm *pLocalTime;
189 struct tm tmBuf;
190 time_t bias;
191 time_t atime;
193 atime = static_cast<time_t>(pSystemTimeVal->Seconds);
194 pLocalTime = localtime_r( &atime, &tmBuf );
196 #if defined(STRUCT_TM_HAS_GMTOFF)
197 /* members of struct tm are corrected by mktime */
198 bias = -pLocalTime->tm_gmtoff;
200 #elif defined(HAS_ALTZONE)
201 /* check if daylight saving time is in effect */
202 bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
203 #else
204 /* expect daylight saving time to be one hour */
205 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
206 #endif
208 if ( static_cast<sal_Int64>(pSystemTimeVal->Seconds) > bias )
210 pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
211 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
213 return true;
216 return false;
219 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
221 struct tm *pLocalTime;
222 struct tm tmBuf;
223 time_t bias;
224 time_t atime;
226 atime = static_cast<time_t>(pLocalTimeVal->Seconds);
228 /* Convert atime, which is a local time, to its GMT equivalent. Then, get
229 * the timezone offset for the local time for the GMT equivalent time. Note
230 * that we cannot directly use local time to determine the timezone offset
231 * because GMT is the only reliable time that we can determine timezone
232 * offset from.
235 atime = mktime( gmtime_r( &atime, &tmBuf ) );
236 pLocalTime = localtime_r( &atime, &tmBuf );
238 #if defined(STRUCT_TM_HAS_GMTOFF)
239 /* members of struct tm are corrected by mktime */
240 bias = 0 - pLocalTime->tm_gmtoff;
242 #elif defined(HAS_ALTZONE)
243 /* check if daylight saving time is in effect */
244 bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
245 #else
246 /* exspect daylight saving time to be one hour */
247 bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
248 #endif
250 if ( static_cast<sal_Int64>(pLocalTimeVal->Seconds) + bias > 0 )
252 pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
253 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
255 return true;
258 return false;
261 void sal_initGlobalTimer()
263 #ifdef __MACH__
264 clock_serv_t cclock;
266 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
267 clock_get_time(cclock, &startTime);
268 mach_port_deallocate(mach_task_self(), cclock);
269 #else /* ! (MACOSX || IOS) */
270 #if defined(USE_CLOCK_GETTIME)
271 clock_gettime(CLOCK_REALTIME, &startTime);
272 #else /* Ndef USE_CLOCK_GETTIME */
273 gettimeofday( &startTime, NULL );
274 #endif /* NDef USE_CLOCK_GETTIME */
275 #endif /* ! (MACOSX || IOS) */
278 sal_uInt32 SAL_CALL osl_getGlobalTimer()
280 sal_uInt32 nSeconds;
282 #ifdef __MACH__
283 clock_serv_t cclock;
284 mach_timespec_t currentTime;
286 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
287 clock_get_time(cclock, &currentTime);
288 mach_port_deallocate(mach_task_self(), cclock);
290 nSeconds = ( currentTime.tv_sec - startTime.tv_sec );
291 nSeconds = ( nSeconds * 1000 ) + static_cast<long>(( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
292 #else
293 osl_time_t currentTime;
295 #if defined(USE_CLOCK_GETTIME)
296 clock_gettime(CLOCK_REALTIME, &currentTime);
297 #else
298 gettimeofday( &currentTime, NULL );
299 #endif
301 nSeconds = static_cast<sal_uInt32>( currentTime.tv_sec - startTime.tv_sec );
302 #if defined(USE_CLOCK_GETTIME)
303 nSeconds = ( nSeconds * 1000 ) + static_cast<long>(( currentTime.tv_nsec - startTime.tv_nsec) / 1000000 );
304 #else
305 nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
306 #endif
307 #endif
308 return nSeconds;
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */