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 static HMODULE hModule
= nullptr;
39 static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime
= nullptr;
41 OSL_ASSERT(pTimeVal
!= nullptr);
45 hModule
= GetModuleHandleW( L
"Kernel32.dll" );
47 pGetSystemTimePreciseAsFileTime
= reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC
>(
48 GetProcAddress(hModule
, "GetSystemTimePreciseAsFileTime"));
51 // use ~1 microsecond resolution if available
52 if (pGetSystemTimePreciseAsFileTime
)
53 pGetSystemTimePreciseAsFileTime(&CurTime
);
56 GetSystemTime(&SystemTime
);
57 SystemTimeToFileTime(&SystemTime
, &CurTime
);
60 SystemTime
.wYear
= 1970;
61 SystemTime
.wMonth
= 1;
62 SystemTime
.wDayOfWeek
= 0;
65 SystemTime
.wMinute
= 0;
66 SystemTime
.wSecond
= 0;
67 SystemTime
.wMilliseconds
= 0;
69 SystemTimeToFileTime(&SystemTime
, &OffTime
);
71 Value
= *reinterpret_cast<__int64
*>(&CurTime
) - *reinterpret_cast<__int64
*>(&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
;
137 // get timezone information
138 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
140 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
;
158 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( const TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
160 TIME_ZONE_INFORMATION aTimeZoneInformation
;
164 // get timezone information
165 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
167 bias
=aTimeZoneInformation
.Bias
;
169 // add bias for daylight saving time
170 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
171 bias
+=aTimeZoneInformation
.DaylightBias
;
173 if ( static_cast<sal_Int64
>(pLocalTimeVal
->Seconds
) + ( bias
* 60 ) > 0 )
175 pSystemTimeVal
->Seconds
= static_cast<sal_uInt32
>( pLocalTimeVal
->Seconds
+ ( bias
* 60) );
176 pSystemTimeVal
->Nanosec
= pLocalTimeVal
->Nanosec
;
185 static struct _timeb startTime
;
186 void sal_initGlobalTimer()
188 _ftime( &startTime
);
191 sal_uInt32 SAL_CALL
osl_getGlobalTimer(void)
193 struct _timeb currentTime
;
196 _ftime( ¤tTime
);
198 nSeconds
= static_cast<sal_uInt32
>( currentTime
.time
- startTime
.time
);
200 return ( nSeconds
* 1000 ) + static_cast<long>( currentTime
.millitm
- startTime
.millitm
);
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */