update dev300-m57
[ooovba.git] / sal / osl / w32 / time.c
blobb6626fdfbf17f0e6589bc172755836a97aaab3fa
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: time.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "system.h"
33 #include <osl/diagnose.h>
34 #include <osl/time.h>
35 #include <sys/timeb.h>
37 extern sal_Bool TimeValueToFileTime(const TimeValue *cpTimeVal, FILETIME *pFTime);
39 extern BOOL FileTimeToTimeValue( const FILETIME *cpFTime, TimeValue *pTimeVal );
41 //--------------------------------------------------
42 // osl_getSystemTime
43 //--------------------------------------------------
45 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
47 SYSTEMTIME SystemTime;
48 FILETIME CurTime, OffTime;
49 __int64 Value;
51 OSL_ASSERT(pTimeVal != 0);
53 GetSystemTime(&SystemTime);
54 SystemTimeToFileTime(&SystemTime, &CurTime);
56 SystemTime.wYear = 1970;
57 SystemTime.wMonth = 1;
58 SystemTime.wDayOfWeek = 0;
59 SystemTime.wDay = 1;
60 SystemTime.wHour = 0;
61 SystemTime.wMinute = 0;
62 SystemTime.wSecond = 0;
63 SystemTime.wMilliseconds = 0;
65 SystemTimeToFileTime(&SystemTime, &OffTime);
67 Value = *((__int64 *)&CurTime) - *((__int64 *)&OffTime);
69 pTimeVal->Seconds = (unsigned long) (Value / 10000000L);
70 pTimeVal->Nanosec = (unsigned long)((Value % 10000000L) * 100);
72 return (sal_True);
75 //--------------------------------------------------
76 // osl_getDateTimeFromTimeValue
77 //--------------------------------------------------
79 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( 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 sal_True;
102 return sal_False;
105 //--------------------------------------------------
106 // osl_getTimeValueFromDateTime
107 //--------------------------------------------------
109 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
111 FILETIME aFileTime;
112 SYSTEMTIME aSystemTime;
114 aSystemTime.wMilliseconds = 0;
115 aSystemTime.wSecond = pDateTime->Seconds;
116 aSystemTime.wMinute = pDateTime->Minutes;
117 aSystemTime.wHour = pDateTime->Hours;
118 aSystemTime.wDay = pDateTime->Day;
119 aSystemTime.wDayOfWeek = pDateTime->DayOfWeek;
120 aSystemTime.wMonth = pDateTime->Month;
121 aSystemTime.wYear = pDateTime->Year;
123 if ( SystemTimeToFileTime( &aSystemTime, &aFileTime ) )
125 if (FileTimeToTimeValue( &aFileTime, pTimeVal ) )
127 pTimeVal->Nanosec = pDateTime->NanoSeconds;
128 return sal_True;
132 return sal_False;
136 //--------------------------------------------------
137 // osl_getLocalTimeFromSystemTime
138 //--------------------------------------------------
140 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
142 TIME_ZONE_INFORMATION aTimeZoneInformation;
143 DWORD Success;
144 sal_Int64 bias;
146 // get timezone information
147 if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
149 bias=aTimeZoneInformation.Bias;
151 // add bias for daylight saving time
152 if ( Success== TIME_ZONE_ID_DAYLIGHT )
153 bias+=aTimeZoneInformation.DaylightBias;
155 if ( (sal_Int64) pSystemTimeVal->Seconds > ( bias * 60 ) )
157 pLocalTimeVal->Seconds = (sal_uInt32) (pSystemTimeVal->Seconds - ( bias * 60) );
158 pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
160 return sal_True;
164 return sal_False;
167 //--------------------------------------------------
168 // osl_getSystemTimeFromLocalTime
169 //--------------------------------------------------
171 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
173 TIME_ZONE_INFORMATION aTimeZoneInformation;
174 DWORD Success;
175 sal_Int64 bias;
177 // get timezone information
178 if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
180 bias=aTimeZoneInformation.Bias;
182 // add bias for daylight saving time
183 if ( Success== TIME_ZONE_ID_DAYLIGHT )
184 bias+=aTimeZoneInformation.DaylightBias;
186 if ( (sal_Int64) pLocalTimeVal->Seconds + ( bias * 60 ) > 0 )
188 pSystemTimeVal->Seconds = (sal_uInt32) ( pLocalTimeVal->Seconds + ( bias * 60) );
189 pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
191 return sal_True;
195 return sal_False;
199 static struct _timeb startTime;
200 static sal_Bool bGlobalTimer = sal_False;
202 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
204 struct _timeb currentTime;
205 sal_uInt32 nSeconds;
207 if ( bGlobalTimer == sal_False )
209 _ftime( &startTime );
210 bGlobalTimer=sal_True;
213 _ftime( &currentTime );
215 nSeconds = (sal_uInt32)( currentTime.time - startTime.time );
217 return ( nSeconds * 1000 ) + (long)( currentTime.millitm - startTime.millitm );