tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / sal / osl / w32 / time.cxx
blobe8b2059f70d4c2b20ce1dd1a974649433bcd048f
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 "assert.h"
22 #include "system.h"
24 #include "filetime.hxx"
25 #include "time.hxx"
27 #include <osl/diagnose.h>
28 #include <osl/time.h>
29 #include <sys/timeb.h>
31 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
33 unsigned __int64 CurTime;
35 typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME);
37 assert(pTimeVal != nullptr);
39 static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime = []()
41 HMODULE hModule = GetModuleHandleW( L"Kernel32.dll" );
42 return reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC>(
43 GetProcAddress(hModule, "GetSystemTimePreciseAsFileTime"));
44 }();
46 // use ~1 microsecond resolution if available
47 if (pGetSystemTimePreciseAsFileTime)
48 pGetSystemTimePreciseAsFileTime(reinterpret_cast<LPFILETIME>(&CurTime));
49 else
51 SYSTEMTIME SystemTime;
52 GetSystemTime(&SystemTime);
53 SystemTimeToFileTime(&SystemTime, reinterpret_cast<LPFILETIME>(&CurTime));
56 static const unsigned __int64 OffTime = [] {
57 SYSTEMTIME SystemTime;
58 SystemTime.wYear = 1970;
59 SystemTime.wMonth = 1;
60 SystemTime.wDayOfWeek = 0;
61 SystemTime.wDay = 1;
62 SystemTime.wHour = 0;
63 SystemTime.wMinute = 0;
64 SystemTime.wSecond = 0;
65 SystemTime.wMilliseconds = 0;
67 unsigned __int64 ft;
68 SystemTimeToFileTime(&SystemTime, reinterpret_cast<LPFILETIME>(&ft));
69 return ft;
70 }();
72 const unsigned __int64 Value = CurTime - OffTime;
74 pTimeVal->Seconds = static_cast<unsigned long>(Value / 10000000L);
75 pTimeVal->Nanosec = static_cast<unsigned long>((Value % 10000000L) * 100);
77 return true;
80 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( const TimeValue* pTimeVal, oslDateTime* pDateTime )
82 FILETIME aFileTime;
83 SYSTEMTIME aSystemTime;
85 if ( TimeValueToFileTime(pTimeVal, &aFileTime) )
87 if ( FileTimeToSystemTime( &aFileTime, &aSystemTime ) )
89 pDateTime->NanoSeconds = pTimeVal->Nanosec;
91 pDateTime->Seconds = aSystemTime.wSecond;
92 pDateTime->Minutes = aSystemTime.wMinute;
93 pDateTime->Hours = aSystemTime.wHour;
94 pDateTime->Day = aSystemTime.wDay;
95 pDateTime->DayOfWeek = aSystemTime.wDayOfWeek;
96 pDateTime->Month = aSystemTime.wMonth;
97 pDateTime->Year = aSystemTime.wYear;
99 return true;
103 return false;
106 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( const oslDateTime* pDateTime, TimeValue* pTimeVal )
108 FILETIME aFileTime;
109 SYSTEMTIME aSystemTime;
111 aSystemTime.wMilliseconds = 0;
112 aSystemTime.wSecond = pDateTime->Seconds;
113 aSystemTime.wMinute = pDateTime->Minutes;
114 aSystemTime.wHour = pDateTime->Hours;
115 aSystemTime.wDay = pDateTime->Day;
116 aSystemTime.wDayOfWeek = pDateTime->DayOfWeek;
117 aSystemTime.wMonth = pDateTime->Month;
118 aSystemTime.wYear = pDateTime->Year;
120 if ( SystemTimeToFileTime( &aSystemTime, &aFileTime ) )
122 if (FileTimeToTimeValue( &aFileTime, pTimeVal ) )
124 pTimeVal->Nanosec = pDateTime->NanoSeconds;
125 return true;
129 return false;
132 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( const TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
134 TIME_ZONE_INFORMATION aTimeZoneInformation;
136 // get timezone information
137 DWORD Success = GetTimeZoneInformation( &aTimeZoneInformation );
138 if (Success == TIME_ZONE_ID_INVALID)
139 return false;
141 sal_Int64 bias = aTimeZoneInformation.Bias;
143 // add bias for daylight saving time
144 if ( Success == TIME_ZONE_ID_DAYLIGHT )
145 bias+=aTimeZoneInformation.DaylightBias;
147 if ( static_cast<sal_Int64>(pSystemTimeVal->Seconds) > ( bias * 60 ) )
149 pLocalTimeVal->Seconds = static_cast<sal_uInt32>(pSystemTimeVal->Seconds - ( bias * 60) );
150 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
152 return true;
154 return false;
157 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( const TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
159 TIME_ZONE_INFORMATION aTimeZoneInformation;
161 // get timezone information
162 DWORD Success = GetTimeZoneInformation( &aTimeZoneInformation );
163 if ( Success == TIME_ZONE_ID_INVALID )
164 return false;
166 sal_Int64 bias = aTimeZoneInformation.Bias;
168 // add bias for daylight saving time
169 if ( Success == TIME_ZONE_ID_DAYLIGHT )
170 bias+=aTimeZoneInformation.DaylightBias;
172 if ( static_cast<sal_Int64>(pLocalTimeVal->Seconds) + ( bias * 60 ) > 0 )
174 pSystemTimeVal->Seconds = static_cast<sal_uInt32>( pLocalTimeVal->Seconds + ( bias * 60) );
175 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
177 return true;
180 return false;
183 static struct _timeb startTime;
184 void sal_initGlobalTimer()
186 _ftime( &startTime );
189 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
191 struct _timeb currentTime;
192 sal_uInt32 nSeconds;
194 _ftime( &currentTime );
196 nSeconds = static_cast<sal_uInt32>( currentTime.time - startTime.time );
198 return ( nSeconds * 1000 ) + static_cast<long>( currentTime.millitm - startTime.millitm );
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */