Bump for 3.6-28
[LibreOffice.git] / sal / osl / w32 / time.c
bloba60028b18e92fdb5bf23bcbe2b9ab6b1e5e348a4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "system.h"
31 #include <osl/diagnose.h>
32 #include <osl/time.h>
33 #include <sys/timeb.h>
35 extern sal_Bool TimeValueToFileTime(const TimeValue *cpTimeVal, FILETIME *pFTime);
37 extern BOOL FileTimeToTimeValue( const FILETIME *cpFTime, TimeValue *pTimeVal );
39 //--------------------------------------------------
40 // osl_getSystemTime
41 //--------------------------------------------------
43 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
45 SYSTEMTIME SystemTime;
46 FILETIME CurTime, OffTime;
47 __int64 Value;
49 OSL_ASSERT(pTimeVal != 0);
51 GetSystemTime(&SystemTime);
52 SystemTimeToFileTime(&SystemTime, &CurTime);
54 SystemTime.wYear = 1970;
55 SystemTime.wMonth = 1;
56 SystemTime.wDayOfWeek = 0;
57 SystemTime.wDay = 1;
58 SystemTime.wHour = 0;
59 SystemTime.wMinute = 0;
60 SystemTime.wSecond = 0;
61 SystemTime.wMilliseconds = 0;
63 SystemTimeToFileTime(&SystemTime, &OffTime);
65 Value = *((__int64 *)&CurTime) - *((__int64 *)&OffTime);
67 pTimeVal->Seconds = (unsigned long) (Value / 10000000L);
68 pTimeVal->Nanosec = (unsigned long)((Value % 10000000L) * 100);
70 return (sal_True);
73 //--------------------------------------------------
74 // osl_getDateTimeFromTimeValue
75 //--------------------------------------------------
77 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
79 FILETIME aFileTime;
80 SYSTEMTIME aSystemTime;
82 if ( TimeValueToFileTime(pTimeVal, &aFileTime) )
84 if ( FileTimeToSystemTime( &aFileTime, &aSystemTime ) )
86 pDateTime->NanoSeconds = pTimeVal->Nanosec;
88 pDateTime->Seconds = aSystemTime.wSecond;
89 pDateTime->Minutes = aSystemTime.wMinute;
90 pDateTime->Hours = aSystemTime.wHour;
91 pDateTime->Day = aSystemTime.wDay;
92 pDateTime->DayOfWeek = aSystemTime.wDayOfWeek;
93 pDateTime->Month = aSystemTime.wMonth;
94 pDateTime->Year = aSystemTime.wYear;
96 return sal_True;
100 return sal_False;
103 //--------------------------------------------------
104 // osl_getTimeValueFromDateTime
105 //--------------------------------------------------
107 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
109 FILETIME aFileTime;
110 SYSTEMTIME aSystemTime;
112 aSystemTime.wMilliseconds = 0;
113 aSystemTime.wSecond = pDateTime->Seconds;
114 aSystemTime.wMinute = pDateTime->Minutes;
115 aSystemTime.wHour = pDateTime->Hours;
116 aSystemTime.wDay = pDateTime->Day;
117 aSystemTime.wDayOfWeek = pDateTime->DayOfWeek;
118 aSystemTime.wMonth = pDateTime->Month;
119 aSystemTime.wYear = pDateTime->Year;
121 if ( SystemTimeToFileTime( &aSystemTime, &aFileTime ) )
123 if (FileTimeToTimeValue( &aFileTime, pTimeVal ) )
125 pTimeVal->Nanosec = pDateTime->NanoSeconds;
126 return sal_True;
130 return sal_False;
134 //--------------------------------------------------
135 // osl_getLocalTimeFromSystemTime
136 //--------------------------------------------------
138 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
140 TIME_ZONE_INFORMATION aTimeZoneInformation;
141 DWORD Success;
142 sal_Int64 bias;
144 // get timezone information
145 if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
147 bias=aTimeZoneInformation.Bias;
149 // add bias for daylight saving time
150 if ( Success== TIME_ZONE_ID_DAYLIGHT )
151 bias+=aTimeZoneInformation.DaylightBias;
153 if ( (sal_Int64) pSystemTimeVal->Seconds > ( bias * 60 ) )
155 pLocalTimeVal->Seconds = (sal_uInt32) (pSystemTimeVal->Seconds - ( bias * 60) );
156 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
158 return sal_True;
162 return sal_False;
165 //--------------------------------------------------
166 // osl_getSystemTimeFromLocalTime
167 //--------------------------------------------------
169 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
171 TIME_ZONE_INFORMATION aTimeZoneInformation;
172 DWORD Success;
173 sal_Int64 bias;
175 // get timezone information
176 if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
178 bias=aTimeZoneInformation.Bias;
180 // add bias for daylight saving time
181 if ( Success== TIME_ZONE_ID_DAYLIGHT )
182 bias+=aTimeZoneInformation.DaylightBias;
184 if ( (sal_Int64) pLocalTimeVal->Seconds + ( bias * 60 ) > 0 )
186 pSystemTimeVal->Seconds = (sal_uInt32) ( pLocalTimeVal->Seconds + ( bias * 60) );
187 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
189 return sal_True;
193 return sal_False;
197 static struct _timeb startTime;
198 static sal_Bool bGlobalTimer = sal_False;
200 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
202 struct _timeb currentTime;
203 sal_uInt32 nSeconds;
205 if ( bGlobalTimer == sal_False )
207 _ftime( &startTime );
208 bGlobalTimer=sal_True;
211 _ftime( &currentTime );
213 nSeconds = (sal_uInt32)( currentTime.time - startTime.time );
215 return ( nSeconds * 1000 ) + (long)( currentTime.millitm - startTime.millitm );
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */