Line data Source code
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 :
21 : #include "system.h"
22 :
23 : #include <osl/diagnose.h>
24 : #include <osl/time.h>
25 :
26 : /* FIXME: detection should be done in configure script */
27 : #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
28 : defined(LINUX) || defined(OPENBSD) || defined(DRAGONFLY)
29 : #define STRUCT_TM_HAS_GMTOFF 1
30 :
31 : #elif defined(SOLARIS)
32 : #define HAS_ALTZONE 1
33 : #endif
34 :
35 : /*--------------------------------------------------
36 : * osl_getSystemTime
37 : *-------------------------------------------------*/
38 :
39 4907 : sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
40 : {
41 : struct timeval tp;
42 :
43 : /* FIXME: use higher resolution */
44 4907 : gettimeofday(&tp, NULL);
45 :
46 4907 : tv->Seconds = tp.tv_sec;
47 4907 : tv->Nanosec = tp.tv_usec * 1000;
48 :
49 4907 : return (sal_True);
50 : }
51 :
52 :
53 : /*--------------------------------------------------
54 : * osl_getDateTimeFromTimeValue
55 : *-------------------------------------------------*/
56 :
57 1776 : sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
58 : {
59 : struct tm *pSystemTime;
60 : struct tm tmBuf;
61 : time_t atime;
62 :
63 1776 : atime = (time_t)pTimeVal->Seconds;
64 :
65 : /* Convert time from type time_t to struct tm */
66 1776 : pSystemTime = gmtime_r( &atime, &tmBuf );
67 :
68 :
69 : /* Convert struct tm to struct oslDateTime */
70 1776 : if ( pSystemTime != NULL )
71 : {
72 1776 : pDateTime->NanoSeconds = pTimeVal->Nanosec;
73 1776 : pDateTime->Seconds = pSystemTime->tm_sec;
74 1776 : pDateTime->Minutes = pSystemTime->tm_min;
75 1776 : pDateTime->Hours = pSystemTime->tm_hour;
76 1776 : pDateTime->Day = pSystemTime->tm_mday;
77 1776 : pDateTime->DayOfWeek = pSystemTime->tm_wday;
78 1776 : pDateTime->Month = pSystemTime->tm_mon + 1;
79 1776 : pDateTime->Year = pSystemTime->tm_year + 1900;
80 :
81 1776 : return sal_True;
82 : }
83 :
84 0 : return sal_False;
85 : }
86 :
87 : /*--------------------------------------------------
88 : * osl_getTimeValueFromDateTime
89 : *--------------------------------------------------*/
90 :
91 0 : sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
92 : {
93 : struct tm aTime;
94 : time_t nSeconds;
95 :
96 : /* Convert struct oslDateTime to struct tm */
97 0 : aTime.tm_sec = pDateTime->Seconds;
98 0 : aTime.tm_min = pDateTime->Minutes;
99 0 : aTime.tm_hour = pDateTime->Hours;
100 0 : aTime.tm_mday = pDateTime->Day;
101 0 : aTime.tm_wday = pDateTime->DayOfWeek;
102 :
103 0 : if ( pDateTime->Month > 0 )
104 0 : aTime.tm_mon = pDateTime->Month - 1;
105 : else
106 0 : return sal_False;
107 :
108 0 : if ( pDateTime->Year >= 1900 )
109 0 : aTime.tm_year = pDateTime->Year - 1900;
110 : else
111 0 : return sal_False;
112 :
113 0 : aTime.tm_isdst = -1;
114 0 : aTime.tm_wday = 0;
115 0 : aTime.tm_yday = 0;
116 :
117 : /* Convert time to calendar value */
118 0 : nSeconds = mktime( &aTime );
119 :
120 : /*
121 : * mktime expects the struct tm to be in local timezone, so we have to adjust
122 : * the returned value to be timezone neutral.
123 : */
124 :
125 0 : if ( nSeconds != (time_t) -1 )
126 : {
127 : time_t bias;
128 :
129 : /* timezone corrections */
130 0 : tzset();
131 :
132 : #if defined(STRUCT_TM_HAS_GMTOFF)
133 : /* members of struct tm are corrected by mktime */
134 0 : bias = 0 - aTime.tm_gmtoff;
135 :
136 : #elif defined(HAS_ALTZONE)
137 : /* check if daylight saving time is in effect */
138 : bias = aTime.tm_isdst > 0 ? altzone : timezone;
139 : #else
140 : /* exspect daylight saving time to be one hour */
141 : bias = aTime.tm_isdst > 0 ? timezone - 3600 : timezone;
142 : #endif
143 :
144 0 : pTimeVal->Seconds = nSeconds;
145 0 : pTimeVal->Nanosec = pDateTime->NanoSeconds;
146 :
147 0 : if ( nSeconds > bias )
148 0 : pTimeVal->Seconds -= bias;
149 :
150 0 : return sal_True;
151 : }
152 :
153 0 : return sal_False;
154 : }
155 :
156 :
157 : /*--------------------------------------------------
158 : * osl_getLocalTimeFromSystemTime
159 : *--------------------------------------------------*/
160 :
161 1476 : sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
162 : {
163 : struct tm *pLocalTime;
164 : struct tm tmBuf;
165 : time_t bias;
166 : time_t atime;
167 :
168 1476 : atime = (time_t) pSystemTimeVal->Seconds;
169 1476 : pLocalTime = localtime_r( &atime, &tmBuf );
170 :
171 : #if defined(STRUCT_TM_HAS_GMTOFF)
172 : /* members of struct tm are corrected by mktime */
173 1476 : bias = 0 - pLocalTime->tm_gmtoff;
174 :
175 : #elif defined(HAS_ALTZONE)
176 : /* check if daylight saving time is in effect */
177 : bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
178 : #else
179 : /* exspect daylight saving time to be one hour */
180 : bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
181 : #endif
182 :
183 1476 : if ( (sal_Int64) pSystemTimeVal->Seconds > bias )
184 : {
185 1476 : pLocalTimeVal->Seconds = pSystemTimeVal->Seconds - bias;
186 1476 : pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
187 :
188 1476 : return sal_True;
189 : }
190 :
191 0 : return sal_False;
192 : }
193 :
194 : /*--------------------------------------------------
195 : * osl_getSystemTimeFromLocalTime
196 : *--------------------------------------------------*/
197 :
198 0 : sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
199 : {
200 : struct tm *pLocalTime;
201 : struct tm tmBuf;
202 : time_t bias;
203 : time_t atime;
204 :
205 0 : atime = (time_t) pLocalTimeVal->Seconds;
206 :
207 : /* Convert atime, which is a local time, to it's GMT equivalent. Then, get
208 : * the timezone offset for the local time for the GMT equivalent time. Note
209 : * that we cannot directly use local time to determine the timezone offset
210 : * because GMT is the only reliable time that we can determine timezone
211 : * offset from.
212 : */
213 :
214 0 : atime = mktime( gmtime_r( &atime, &tmBuf ) );
215 0 : pLocalTime = localtime_r( &atime, &tmBuf );
216 :
217 : #if defined(STRUCT_TM_HAS_GMTOFF)
218 : /* members of struct tm are corrected by mktime */
219 0 : bias = 0 - pLocalTime->tm_gmtoff;
220 :
221 : #elif defined(HAS_ALTZONE)
222 : /* check if daylight saving time is in effect */
223 : bias = pLocalTime->tm_isdst > 0 ? altzone : timezone;
224 : #else
225 : /* exspect daylight saving time to be one hour */
226 : bias = pLocalTime->tm_isdst > 0 ? timezone - 3600 : timezone;
227 : #endif
228 :
229 0 : if ( (sal_Int64) pLocalTimeVal->Seconds + bias > 0 )
230 : {
231 0 : pSystemTimeVal->Seconds = pLocalTimeVal->Seconds + bias;
232 0 : pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
233 :
234 0 : return sal_True;
235 : }
236 :
237 0 : return sal_False;
238 : }
239 :
240 :
241 :
242 : static struct timeval startTime;
243 : static sal_Bool bGlobalTimer = sal_False;
244 :
245 2025 : sal_uInt32 SAL_CALL osl_getGlobalTimer()
246 : {
247 : struct timeval currentTime;
248 : sal_uInt32 nSeconds;
249 :
250 : // FIXME: not thread safe !!
251 2025 : if ( bGlobalTimer == sal_False )
252 : {
253 1277 : gettimeofday( &startTime, NULL );
254 1277 : bGlobalTimer=sal_True;
255 : }
256 :
257 2025 : gettimeofday( ¤tTime, NULL );
258 :
259 2025 : nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
260 :
261 2025 : return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - startTime.tv_usec) / 1000 );
262 : }
263 :
264 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|