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 unsigned __int64 CurTime
;
34 typedef VOID (WINAPI
*GetSystemTimePreciseAsFileTime_PROC
)(LPFILETIME
);
36 OSL_ASSERT(pTimeVal
!= nullptr);
38 static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime
= []()
40 HMODULE hModule
= GetModuleHandleW( L
"Kernel32.dll" );
41 return reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC
>(
42 GetProcAddress(hModule
, "GetSystemTimePreciseAsFileTime"));
45 // use ~1 microsecond resolution if available
46 if (pGetSystemTimePreciseAsFileTime
)
47 pGetSystemTimePreciseAsFileTime(reinterpret_cast<LPFILETIME
>(&CurTime
));
50 SYSTEMTIME SystemTime
;
51 GetSystemTime(&SystemTime
);
52 SystemTimeToFileTime(&SystemTime
, reinterpret_cast<LPFILETIME
>(&CurTime
));
55 static const unsigned __int64 OffTime
= [] {
56 SYSTEMTIME SystemTime
;
57 SystemTime
.wYear
= 1970;
58 SystemTime
.wMonth
= 1;
59 SystemTime
.wDayOfWeek
= 0;
62 SystemTime
.wMinute
= 0;
63 SystemTime
.wSecond
= 0;
64 SystemTime
.wMilliseconds
= 0;
67 SystemTimeToFileTime(&SystemTime
, reinterpret_cast<LPFILETIME
>(&ft
));
71 const unsigned __int64 Value
= CurTime
- OffTime
;
73 pTimeVal
->Seconds
= static_cast<unsigned long>(Value
/ 10000000L);
74 pTimeVal
->Nanosec
= static_cast<unsigned long>((Value
% 10000000L) * 100);
79 sal_Bool SAL_CALL
osl_getDateTimeFromTimeValue( const TimeValue
* pTimeVal
, oslDateTime
* pDateTime
)
82 SYSTEMTIME aSystemTime
;
84 if ( TimeValueToFileTime(pTimeVal
, &aFileTime
) )
86 if ( FileTimeToSystemTime( &aFileTime
, &aSystemTime
) )
88 pDateTime
->NanoSeconds
= pTimeVal
->Nanosec
;
90 pDateTime
->Seconds
= aSystemTime
.wSecond
;
91 pDateTime
->Minutes
= aSystemTime
.wMinute
;
92 pDateTime
->Hours
= aSystemTime
.wHour
;
93 pDateTime
->Day
= aSystemTime
.wDay
;
94 pDateTime
->DayOfWeek
= aSystemTime
.wDayOfWeek
;
95 pDateTime
->Month
= aSystemTime
.wMonth
;
96 pDateTime
->Year
= aSystemTime
.wYear
;
105 sal_Bool SAL_CALL
osl_getTimeValueFromDateTime( const oslDateTime
* pDateTime
, TimeValue
* pTimeVal
)
108 SYSTEMTIME aSystemTime
;
110 aSystemTime
.wMilliseconds
= 0;
111 aSystemTime
.wSecond
= pDateTime
->Seconds
;
112 aSystemTime
.wMinute
= pDateTime
->Minutes
;
113 aSystemTime
.wHour
= pDateTime
->Hours
;
114 aSystemTime
.wDay
= pDateTime
->Day
;
115 aSystemTime
.wDayOfWeek
= pDateTime
->DayOfWeek
;
116 aSystemTime
.wMonth
= pDateTime
->Month
;
117 aSystemTime
.wYear
= pDateTime
->Year
;
119 if ( SystemTimeToFileTime( &aSystemTime
, &aFileTime
) )
121 if (FileTimeToTimeValue( &aFileTime
, pTimeVal
) )
123 pTimeVal
->Nanosec
= pDateTime
->NanoSeconds
;
131 sal_Bool SAL_CALL
osl_getLocalTimeFromSystemTime( const TimeValue
* pSystemTimeVal
, TimeValue
* pLocalTimeVal
)
133 TIME_ZONE_INFORMATION aTimeZoneInformation
;
135 // get timezone information
136 DWORD Success
= GetTimeZoneInformation( &aTimeZoneInformation
);
137 if (Success
== TIME_ZONE_ID_INVALID
)
140 sal_Int64 bias
= aTimeZoneInformation
.Bias
;
142 // add bias for daylight saving time
143 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
144 bias
+=aTimeZoneInformation
.DaylightBias
;
146 if ( static_cast<sal_Int64
>(pSystemTimeVal
->Seconds
) > ( bias
* 60 ) )
148 pLocalTimeVal
->Seconds
= static_cast<sal_uInt32
>(pSystemTimeVal
->Seconds
- ( bias
* 60) );
149 pLocalTimeVal
->Nanosec
= pSystemTimeVal
->Nanosec
;
156 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( const TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
158 TIME_ZONE_INFORMATION aTimeZoneInformation
;
160 // get timezone information
161 DWORD Success
= GetTimeZoneInformation( &aTimeZoneInformation
);
162 if ( Success
== TIME_ZONE_ID_INVALID
)
165 sal_Int64 bias
= aTimeZoneInformation
.Bias
;
167 // add bias for daylight saving time
168 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
169 bias
+=aTimeZoneInformation
.DaylightBias
;
171 if ( static_cast<sal_Int64
>(pLocalTimeVal
->Seconds
) + ( bias
* 60 ) > 0 )
173 pSystemTimeVal
->Seconds
= static_cast<sal_uInt32
>( pLocalTimeVal
->Seconds
+ ( bias
* 60) );
174 pSystemTimeVal
->Nanosec
= pLocalTimeVal
->Nanosec
;
182 static struct _timeb startTime
;
183 void sal_initGlobalTimer()
185 _ftime( &startTime
);
188 sal_uInt32 SAL_CALL
osl_getGlobalTimer(void)
190 struct _timeb currentTime
;
193 _ftime( ¤tTime
);
195 nSeconds
= static_cast<sal_uInt32
>( currentTime
.time
- startTime
.time
);
197 return ( nSeconds
* 1000 ) + static_cast<long>( currentTime
.millitm
- startTime
.millitm
);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */