lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sal / osl / w32 / time.cxx
blobd1284b663faefc11f643c2b117802bf1fb5a50ab
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 SYSTEMTIME SystemTime;
33 FILETIME CurTime, OffTime;
34 __int64 Value;
36 typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME);
38 static HMODULE hModule = nullptr;
39 static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime = nullptr;
41 OSL_ASSERT(pTimeVal != nullptr);
43 if ( !hModule )
45 hModule = GetModuleHandleW( L"Kernel32.dll" );
46 if ( hModule )
47 pGetSystemTimePreciseAsFileTime = reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC>(
48 GetProcAddress(hModule, "GetSystemTimePreciseAsFileTime"));
51 // use ~1 microsecond resolution if available
52 if (pGetSystemTimePreciseAsFileTime)
53 pGetSystemTimePreciseAsFileTime(&CurTime);
54 else
56 GetSystemTime(&SystemTime);
57 SystemTimeToFileTime(&SystemTime, &CurTime);
60 SystemTime.wYear = 1970;
61 SystemTime.wMonth = 1;
62 SystemTime.wDayOfWeek = 0;
63 SystemTime.wDay = 1;
64 SystemTime.wHour = 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);
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;
134 DWORD Success;
135 sal_Int64 bias;
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;
151 return true;
155 return false;
158 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
160 TIME_ZONE_INFORMATION aTimeZoneInformation;
161 DWORD Success;
162 sal_Int64 bias;
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;
178 return true;
182 return false;
185 static struct _timeb startTime;
186 void sal_initGlobalTimer()
188 _ftime( &startTime );
191 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
193 struct _timeb currentTime;
194 sal_uInt32 nSeconds;
196 _ftime( &currentTime );
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: */