1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include "filetime.hxx"
26 #include <osl/diagnose.h>
28 #include <sys/timeb.h>
30 sal_Bool SAL_CALL
osl_getSystemTime(TimeValue
* pTimeVal
)
32 SYSTEMTIME SystemTime
;
33 FILETIME CurTime
, OffTime
;
36 typedef VOID (WINAPI
*GetSystemTimePreciseAsFileTime_PROC
)(LPFILETIME
);
38 OSL_ASSERT(pTimeVal
!= nullptr);
40 static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime
= [&]()
42 HMODULE hModule
= GetModuleHandleW( L
"Kernel32.dll" );
43 return reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC
>(
44 GetProcAddress(hModule
, "GetSystemTimePreciseAsFileTime"));
47 // use ~1 microsecond resolution if available
48 if (pGetSystemTimePreciseAsFileTime
)
49 pGetSystemTimePreciseAsFileTime(&CurTime
);
52 GetSystemTime(&SystemTime
);
53 SystemTimeToFileTime(&SystemTime
, &CurTime
);
56 SystemTime
.wYear
= 1970;
57 SystemTime
.wMonth
= 1;
58 SystemTime
.wDayOfWeek
= 0;
61 SystemTime
.wMinute
= 0;
62 SystemTime
.wSecond
= 0;
63 SystemTime
.wMilliseconds
= 0;
65 SystemTimeToFileTime(&SystemTime
, &OffTime
);
67 Value
= *reinterpret_cast<__int64
*>(&CurTime
) - *reinterpret_cast<__int64
*>(&OffTime
);
69 pTimeVal
->Seconds
= static_cast<unsigned long>(Value
/ 10000000L);
70 pTimeVal
->Nanosec
= static_cast<unsigned long>((Value
% 10000000L) * 100);
75 sal_Bool SAL_CALL
osl_getDateTimeFromTimeValue( const TimeValue
* pTimeVal
, oslDateTime
* pDateTime
)
78 SYSTEMTIME aSystemTime
;
80 if ( TimeValueToFileTime(pTimeVal
, &aFileTime
) )
82 if ( FileTimeToSystemTime( &aFileTime
, &aSystemTime
) )
84 pDateTime
->NanoSeconds
= pTimeVal
->Nanosec
;
86 pDateTime
->Seconds
= aSystemTime
.wSecond
;
87 pDateTime
->Minutes
= aSystemTime
.wMinute
;
88 pDateTime
->Hours
= aSystemTime
.wHour
;
89 pDateTime
->Day
= aSystemTime
.wDay
;
90 pDateTime
->DayOfWeek
= aSystemTime
.wDayOfWeek
;
91 pDateTime
->Month
= aSystemTime
.wMonth
;
92 pDateTime
->Year
= aSystemTime
.wYear
;
101 sal_Bool SAL_CALL
osl_getTimeValueFromDateTime( const oslDateTime
* pDateTime
, TimeValue
* pTimeVal
)
104 SYSTEMTIME aSystemTime
;
106 aSystemTime
.wMilliseconds
= 0;
107 aSystemTime
.wSecond
= pDateTime
->Seconds
;
108 aSystemTime
.wMinute
= pDateTime
->Minutes
;
109 aSystemTime
.wHour
= pDateTime
->Hours
;
110 aSystemTime
.wDay
= pDateTime
->Day
;
111 aSystemTime
.wDayOfWeek
= pDateTime
->DayOfWeek
;
112 aSystemTime
.wMonth
= pDateTime
->Month
;
113 aSystemTime
.wYear
= pDateTime
->Year
;
115 if ( SystemTimeToFileTime( &aSystemTime
, &aFileTime
) )
117 if (FileTimeToTimeValue( &aFileTime
, pTimeVal
) )
119 pTimeVal
->Nanosec
= pDateTime
->NanoSeconds
;
127 sal_Bool SAL_CALL
osl_getLocalTimeFromSystemTime( const TimeValue
* pSystemTimeVal
, TimeValue
* pLocalTimeVal
)
129 TIME_ZONE_INFORMATION aTimeZoneInformation
;
133 // get timezone information
134 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
136 bias
=aTimeZoneInformation
.Bias
;
138 // add bias for daylight saving time
139 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
140 bias
+=aTimeZoneInformation
.DaylightBias
;
142 if ( static_cast<sal_Int64
>(pSystemTimeVal
->Seconds
) > ( bias
* 60 ) )
144 pLocalTimeVal
->Seconds
= static_cast<sal_uInt32
>(pSystemTimeVal
->Seconds
- ( bias
* 60) );
145 pLocalTimeVal
->Nanosec
= pSystemTimeVal
->Nanosec
;
154 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( const TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
156 TIME_ZONE_INFORMATION aTimeZoneInformation
;
160 // get timezone information
161 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
163 bias
=aTimeZoneInformation
.Bias
;
165 // add bias for daylight saving time
166 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
167 bias
+=aTimeZoneInformation
.DaylightBias
;
169 if ( static_cast<sal_Int64
>(pLocalTimeVal
->Seconds
) + ( bias
* 60 ) > 0 )
171 pSystemTimeVal
->Seconds
= static_cast<sal_uInt32
>( pLocalTimeVal
->Seconds
+ ( bias
* 60) );
172 pSystemTimeVal
->Nanosec
= pLocalTimeVal
->Nanosec
;
181 static struct _timeb startTime
;
182 void sal_initGlobalTimer()
184 _ftime( &startTime
);
187 sal_uInt32 SAL_CALL
osl_getGlobalTimer(void)
189 struct _timeb currentTime
;
192 _ftime( ¤tTime
);
194 nSeconds
= static_cast<sal_uInt32
>( currentTime
.time
- startTime
.time
);
196 return ( nSeconds
* 1000 ) + static_cast<long>( currentTime
.millitm
- startTime
.millitm
);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */