Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / w32 / time.cxx
blob3f11746fedad7f220a47e7d8bfb8e67e5315f222
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include "system.h"
23 #include "filetime.hxx"
24 #include "time.hxx"
26 #include <osl/diagnose.h>
27 #include <osl/time.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"));
43 }();
45 // use ~1 microsecond resolution if available
46 if (pGetSystemTimePreciseAsFileTime)
47 pGetSystemTimePreciseAsFileTime(reinterpret_cast<LPFILETIME>(&CurTime));
48 else
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;
60 SystemTime.wDay = 1;
61 SystemTime.wHour = 0;
62 SystemTime.wMinute = 0;
63 SystemTime.wSecond = 0;
64 SystemTime.wMilliseconds = 0;
66 unsigned __int64 ft;
67 SystemTimeToFileTime(&SystemTime, reinterpret_cast<LPFILETIME>(&ft));
68 return ft;
69 }();
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);
76 return true;
79 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
81 FILETIME aFileTime;
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;
98 return true;
102 return false;
105 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
107 FILETIME aFileTime;
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;
124 return true;
128 return false;
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)
138 return false;
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;
151 return true;
153 return false;
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 )
163 return false;
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;
176 return true;
179 return false;
182 static struct _timeb startTime;
183 void sal_initGlobalTimer()
185 _ftime( &startTime );
188 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
190 struct _timeb currentTime;
191 sal_uInt32 nSeconds;
193 _ftime( &currentTime );
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: */