fix toolbar import
[ooovba.git] / tools / source / datetime / ttime.cxx
blobebb1d3862c48e330a9e50c41e43553d0ffaa59a4
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: ttime.cxx,v $
10 * $Revision: 1.16 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 #define _TOOLS_TIME_CXX
36 #if defined( OS2 )
37 #define INCL_DOSMISC
38 #define INCL_DOSDATETIME
39 #include <svpm.h>
40 #elif defined( WNT )
41 #ifdef _MSC_VER
42 #pragma warning (push,1)
43 #endif
44 #include <tools/svwin.h>
45 #ifdef _MSC_VER
46 #pragma warning (pop)
47 #endif
48 #elif defined UNX
49 #include <unistd.h>
50 #include <limits.h>
51 #include <math.h>
52 #include <sys/time.h>
53 #endif
55 #include <time.h>
56 #include <tools/time.hxx>
58 #if defined(SOLARIS) && defined(__GNUC__)
59 extern long altzone;
60 #endif
62 // =======================================================================
64 static sal_Int32 TimeToSec100( const Time& rTime )
66 short nSign = (rTime.GetTime() >= 0) ? +1 : -1;
67 sal_Int32 nHour = rTime.GetHour();
68 sal_Int32 nMin = rTime.GetMin();
69 sal_Int32 nSec = rTime.GetSec();
70 sal_Int32 n100Sec = rTime.Get100Sec();
72 // Wegen Interal Compiler Error bei MSC, etwas komplizierter
73 // return (n100Sec + (nSec*100) + (nMin*60*100) + (nHour*60*60*100) * nSign);
75 sal_Int32 nRet = n100Sec;
76 nRet += nSec*100;
77 nRet += nMin*60*100;
78 nRet += nHour*60*60*100;
80 return (nRet * nSign);
83 // -----------------------------------------------------------------------
85 static Time Sec100ToTime( sal_Int32 nSec100 )
87 short nSign;
88 if ( nSec100 < 0 )
90 nSec100 *= -1;
91 nSign = -1;
93 else
94 nSign = 1;
96 Time aTime( 0, 0, 0, nSec100 );
97 aTime.SetTime( aTime.GetTime() * nSign );
98 return aTime;
101 // =======================================================================
103 Time::Time()
105 #if defined( OS2 )
106 DATETIME aDateTime;
107 DosGetDateTime( &aDateTime );
109 // Zeit zusammenbauen
110 nTime = (((sal_Int32)aDateTime.hours)*1000000) +
111 (((sal_Int32)aDateTime.minutes)*10000) +
112 (((sal_Int32)aDateTime.seconds)*100) +
113 ((sal_Int32)aDateTime.hundredths);
114 #elif defined( WNT )
115 SYSTEMTIME aDateTime;
116 GetLocalTime( &aDateTime );
118 // Zeit zusammenbauen
119 nTime = (((sal_Int32)aDateTime.wHour)*1000000) +
120 (((sal_Int32)aDateTime.wMinute)*10000) +
121 (((sal_Int32)aDateTime.wSecond)*100) +
122 ((sal_Int32)aDateTime.wMilliseconds/10);
123 #else
124 time_t nTmpTime;
125 struct tm aTime;
127 // Zeit ermitteln
128 nTmpTime = time( 0 );
130 // Zeit zusammenbauen
131 if ( localtime_r( &nTmpTime, &aTime ) )
133 nTime = (((sal_Int32)aTime.tm_hour)*1000000) +
134 (((sal_Int32)aTime.tm_min)*10000) +
135 (((sal_Int32)aTime.tm_sec)*100);
137 else
138 nTime = 0;
139 #endif
142 // -----------------------------------------------------------------------
144 Time::Time( const Time& rTime )
146 nTime = rTime.nTime;
149 // -----------------------------------------------------------------------
151 Time::Time( ULONG nHour, ULONG nMin, ULONG nSec, ULONG n100Sec )
153 // Zeit normalisieren
154 nSec += n100Sec / 100;
155 n100Sec = n100Sec % 100;
156 nMin += nSec / 60;
157 nSec = nSec % 60;
158 nHour += nMin / 60;
159 nMin = nMin % 60;
161 // Zeit zusammenbauen
162 nTime = (sal_Int32)(n100Sec + (nSec*100) + (nMin*10000) + (nHour*1000000));
165 // -----------------------------------------------------------------------
167 void Time::SetHour( USHORT nNewHour )
169 short nSign = (nTime >= 0) ? +1 : -1;
170 sal_Int32 nMin = GetMin();
171 sal_Int32 nSec = GetSec();
172 sal_Int32 n100Sec = Get100Sec();
174 nTime = (n100Sec + (nSec*100) + (nMin*10000) +
175 (((sal_Int32)nNewHour)*1000000)) * nSign;
178 // -----------------------------------------------------------------------
180 void Time::SetMin( USHORT nNewMin )
182 short nSign = (nTime >= 0) ? +1 : -1;
183 sal_Int32 nHour = GetHour();
184 sal_Int32 nSec = GetSec();
185 sal_Int32 n100Sec = Get100Sec();
187 // kein Ueberlauf
188 nNewMin = nNewMin % 60;
190 nTime = (n100Sec + (nSec*100) + (((sal_Int32)nNewMin)*10000) +
191 (nHour*1000000)) * nSign;
194 // -----------------------------------------------------------------------
196 void Time::SetSec( USHORT nNewSec )
198 short nSign = (nTime >= 0) ? +1 : -1;
199 sal_Int32 nHour = GetHour();
200 sal_Int32 nMin = GetMin();
201 sal_Int32 n100Sec = Get100Sec();
203 // kein Ueberlauf
204 nNewSec = nNewSec % 60;
206 nTime = (n100Sec + (((sal_Int32)nNewSec)*100) + (nMin*10000) +
207 (nHour*1000000)) * nSign;
210 // -----------------------------------------------------------------------
212 void Time::Set100Sec( USHORT nNew100Sec )
214 short nSign = (nTime >= 0) ? +1 : -1;
215 sal_Int32 nHour = GetHour();
216 sal_Int32 nMin = GetMin();
217 sal_Int32 nSec = GetSec();
219 // kein Ueberlauf
220 nNew100Sec = nNew100Sec % 100;
222 nTime = (((sal_Int32)nNew100Sec) + (nSec*100) + (nMin*10000) +
223 (nHour*1000000)) * nSign;
226 // -----------------------------------------------------------------------
228 sal_Int32 Time::GetMSFromTime() const
230 short nSign = (nTime >= 0) ? +1 : -1;
231 sal_Int32 nHour = GetHour();
232 sal_Int32 nMin = GetMin();
233 sal_Int32 nSec = GetSec();
234 sal_Int32 n100Sec = Get100Sec();
236 return (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);
239 // -----------------------------------------------------------------------
241 void Time::MakeTimeFromMS( sal_Int32 nMS )
243 short nSign;
244 if ( nMS < 0 )
246 nMS *= -1;
247 nSign = -1;
249 else
250 nSign = 1;
252 Time aTime( 0, 0, 0, nMS/10 );
253 SetTime( aTime.GetTime() * nSign );
256 // -----------------------------------------------------------------------
258 double Time::GetTimeInDays() const
260 short nSign = (nTime >= 0) ? +1 : -1;
261 double nHour = GetHour();
262 double nMin = GetMin();
263 double nSec = GetSec();
264 double n100Sec = Get100Sec();
266 return (nHour+(nMin/60)+(nSec/(60*60))+(n100Sec/(60*60*100))) / 24 * nSign;
269 // -----------------------------------------------------------------------
271 Time& Time::operator =( const Time& rTime )
273 nTime = rTime.nTime;
274 return *this;
277 // -----------------------------------------------------------------------
279 Time& Time::operator +=( const Time& rTime )
281 nTime = Sec100ToTime( TimeToSec100( *this ) +
282 TimeToSec100( rTime ) ).GetTime();
283 return *this;
286 // -----------------------------------------------------------------------
288 Time& Time::operator -=( const Time& rTime )
290 nTime = Sec100ToTime( TimeToSec100( *this ) -
291 TimeToSec100( rTime ) ).GetTime();
292 return *this;
295 // -----------------------------------------------------------------------
297 Time operator +( const Time& rTime1, const Time& rTime2 )
299 return Sec100ToTime( TimeToSec100( rTime1 ) +
300 TimeToSec100( rTime2 ) );
303 // -----------------------------------------------------------------------
305 Time operator -( const Time& rTime1, const Time& rTime2 )
307 return Sec100ToTime( TimeToSec100( rTime1 ) -
308 TimeToSec100( rTime2 ) );
311 // -----------------------------------------------------------------------
313 BOOL Time::IsEqualIgnore100Sec( const Time& rTime ) const
315 sal_Int32 n1 = (nTime < 0 ? -Get100Sec() : Get100Sec() );
316 sal_Int32 n2 = (rTime.nTime < 0 ? -rTime.Get100Sec() : rTime.Get100Sec() );
317 return (nTime - n1) == (rTime.nTime - n2);
320 // -----------------------------------------------------------------------
322 Time Time::GetUTCOffset()
324 #if defined( OS2 )
325 #undef timezone
326 DATETIME aDateTime;
327 DosGetDateTime( &aDateTime );
329 // Zeit zusammenbauen
330 if ( aDateTime.timezone != -1 )
332 short nTempTime = (short)Abs( aDateTime.timezone );
333 Time aTime( 0, (USHORT)nTempTime );
334 if ( aDateTime.timezone > 0 )
335 aTime = -aTime;
336 return aTime;
338 else
339 return Time( 0 );
340 #elif defined( WNT )
341 TIME_ZONE_INFORMATION aTimeZone;
342 aTimeZone.Bias = 0;
343 DWORD nTimeZoneRet = GetTimeZoneInformation( &aTimeZone );
344 sal_Int32 nTempTime = aTimeZone.Bias;
345 if ( nTimeZoneRet == TIME_ZONE_ID_STANDARD )
346 nTempTime += aTimeZone.StandardBias;
347 else if ( nTimeZoneRet == TIME_ZONE_ID_DAYLIGHT )
348 nTempTime += aTimeZone.DaylightBias;
349 Time aTime( 0, (USHORT)Abs( nTempTime ) );
350 if ( nTempTime > 0 )
351 aTime = -aTime;
352 return aTime;
353 #else
354 static ULONG nCacheTicks = 0;
355 static sal_Int32 nCacheSecOffset = -1;
356 ULONG nTicks = Time::GetSystemTicks();
357 time_t nTime;
358 tm aTM;
359 sal_Int32 nLocalTime;
360 sal_Int32 nUTC;
361 short nTempTime;
363 // Evt. Wert neu ermitteln
364 if ( (nCacheSecOffset == -1) ||
365 ((nTicks - nCacheTicks) > 360000) ||
366 ( nTicks < nCacheTicks ) // handle overflow
369 nTime = time( 0 );
370 localtime_r( &nTime, &aTM );
371 nLocalTime = mktime( &aTM );
372 #if defined( SOLARIS )
373 // Solaris gmtime_r() seems not to handle daylight saving time
374 // flags correctly
375 nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone );
376 #elif defined( LINUX )
377 // Linux mktime() seems not to handle tm_isdst correctly
378 nUTC = nLocalTime - aTM.tm_gmtoff;
379 #else
380 gmtime_r( &nTime, &aTM );
381 nUTC = mktime( &aTM );
382 #endif
383 nCacheTicks = nTicks;
384 nCacheSecOffset = (nLocalTime-nUTC) / 60;
387 nTempTime = (short)Abs( nCacheSecOffset );
388 Time aTime( 0, (USHORT)nTempTime );
389 if ( nCacheSecOffset < 0 )
390 aTime = -aTime;
391 return aTime;
392 #endif
396 // -----------------------------------------------------------------------
398 ULONG Time::GetSystemTicks()
400 #if defined WNT
401 return (ULONG)GetTickCount();
402 #elif defined( OS2 )
403 ULONG nClock;
404 DosQuerySysInfo( QSV_MS_COUNT, QSV_MS_COUNT, &nClock, sizeof( nClock ) );
405 return (ULONG)nClock;
406 #else
407 timeval tv;
408 gettimeofday (&tv, 0);
410 double fTicks = tv.tv_sec;
411 fTicks *= 1000;
412 fTicks += ((tv.tv_usec + 500) / 1000);
414 fTicks = fmod (fTicks, double(ULONG_MAX));
415 return ULONG(fTicks);
416 #endif
419 // -----------------------------------------------------------------------
421 ULONG Time::GetProcessTicks()
423 #if defined WNT
424 return (ULONG)GetTickCount();
425 #elif defined( OS2 )
426 ULONG nClock;
427 DosQuerySysInfo( QSV_MS_COUNT, QSV_MS_COUNT, &nClock, sizeof( nClock ) );
428 return (ULONG)nClock;
429 #else
430 static ULONG nImplTicksPerSecond = 0;
431 static double dImplTicksPerSecond;
432 static double dImplTicksULONGMAX;
433 ULONG nTicks = (ULONG)clock();
435 if ( !nImplTicksPerSecond )
437 nImplTicksPerSecond = CLOCKS_PER_SEC;
438 dImplTicksPerSecond = nImplTicksPerSecond;
439 dImplTicksULONGMAX = (double)(ULONG)ULONG_MAX;
442 double fTicks = nTicks;
443 fTicks *= 1000;
444 fTicks /= dImplTicksPerSecond;
445 fTicks = fmod (fTicks, dImplTicksULONGMAX);
446 return (ULONG)fTicks;
447 #endif