xref: /aoo41x/main/sal/osl/w32/time.c (revision 647f063d)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647f063dSAndrew Rist  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19*647f063dSAndrew Rist  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/time.h>
28cdf0e10cSrcweir #include <sys/timeb.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir extern sal_Bool TimeValueToFileTime(const TimeValue *cpTimeVal, FILETIME *pFTime);
31cdf0e10cSrcweir 
32cdf0e10cSrcweir extern BOOL FileTimeToTimeValue( const FILETIME *cpFTime, TimeValue *pTimeVal );
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //--------------------------------------------------
35cdf0e10cSrcweir // osl_getSystemTime
36cdf0e10cSrcweir //--------------------------------------------------
37cdf0e10cSrcweir 
osl_getSystemTime(TimeValue * pTimeVal)38cdf0e10cSrcweir sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	SYSTEMTIME SystemTime;
41cdf0e10cSrcweir 	FILETIME   CurTime, OffTime;
42cdf0e10cSrcweir 	__int64	   Value;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	OSL_ASSERT(pTimeVal != 0);
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	GetSystemTime(&SystemTime);
47cdf0e10cSrcweir 	SystemTimeToFileTime(&SystemTime, &CurTime);
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	SystemTime.wYear         = 1970;
50cdf0e10cSrcweir 	SystemTime.wMonth        = 1;
51cdf0e10cSrcweir     SystemTime.wDayOfWeek    = 0;
52cdf0e10cSrcweir     SystemTime.wDay          = 1;
53cdf0e10cSrcweir     SystemTime.wHour         = 0;
54cdf0e10cSrcweir     SystemTime.wMinute       = 0;
55cdf0e10cSrcweir     SystemTime.wSecond       = 0;
56cdf0e10cSrcweir     SystemTime.wMilliseconds = 0;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	SystemTimeToFileTime(&SystemTime, &OffTime);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	Value = *((__int64 *)&CurTime) - *((__int64 *)&OffTime);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	pTimeVal->Seconds  = (unsigned long) (Value / 10000000L);
63cdf0e10cSrcweir 	pTimeVal->Nanosec  = (unsigned long)((Value % 10000000L) * 100);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	return (sal_True);
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //--------------------------------------------------
69cdf0e10cSrcweir // osl_getDateTimeFromTimeValue
70cdf0e10cSrcweir //--------------------------------------------------
71cdf0e10cSrcweir 
osl_getDateTimeFromTimeValue(TimeValue * pTimeVal,oslDateTime * pDateTime)72cdf0e10cSrcweir sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	FILETIME	aFileTime;
75cdf0e10cSrcweir 	SYSTEMTIME	aSystemTime;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	if ( TimeValueToFileTime(pTimeVal, &aFileTime) )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		if ( FileTimeToSystemTime( &aFileTime, &aSystemTime ) )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			pDateTime->NanoSeconds	=	pTimeVal->Nanosec;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 			pDateTime->Seconds		=	aSystemTime.wSecond;
84cdf0e10cSrcweir 			pDateTime->Minutes		=	aSystemTime.wMinute;
85cdf0e10cSrcweir 			pDateTime->Hours		=	aSystemTime.wHour;
86cdf0e10cSrcweir 			pDateTime->Day			=	aSystemTime.wDay;
87cdf0e10cSrcweir 			pDateTime->DayOfWeek	=	aSystemTime.wDayOfWeek;
88cdf0e10cSrcweir 			pDateTime->Month		=	aSystemTime.wMonth;
89cdf0e10cSrcweir 			pDateTime->Year			=	aSystemTime.wYear;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			return sal_True;
92cdf0e10cSrcweir 		}
93cdf0e10cSrcweir 	}
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	return sal_False;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //--------------------------------------------------
99cdf0e10cSrcweir // osl_getTimeValueFromDateTime
100cdf0e10cSrcweir //--------------------------------------------------
101cdf0e10cSrcweir 
osl_getTimeValueFromDateTime(oslDateTime * pDateTime,TimeValue * pTimeVal)102cdf0e10cSrcweir sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	FILETIME	aFileTime;
105cdf0e10cSrcweir 	SYSTEMTIME	aSystemTime;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	aSystemTime.wMilliseconds	=	0;
108cdf0e10cSrcweir 	aSystemTime.wSecond			=	pDateTime->Seconds;
109cdf0e10cSrcweir 	aSystemTime.wMinute			=	pDateTime->Minutes;
110cdf0e10cSrcweir 	aSystemTime.wHour			=	pDateTime->Hours;
111cdf0e10cSrcweir 	aSystemTime.wDay			=	pDateTime->Day;
112cdf0e10cSrcweir 	aSystemTime.wDayOfWeek		=	pDateTime->DayOfWeek;
113cdf0e10cSrcweir 	aSystemTime.wMonth			=	pDateTime->Month;
114cdf0e10cSrcweir 	aSystemTime.wYear			=	pDateTime->Year;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	if ( SystemTimeToFileTime( &aSystemTime, &aFileTime ) )
117cdf0e10cSrcweir 	{
118cdf0e10cSrcweir 		if (FileTimeToTimeValue( &aFileTime, pTimeVal  ) )
119cdf0e10cSrcweir 		{
120cdf0e10cSrcweir 			pTimeVal->Nanosec = pDateTime->NanoSeconds;
121cdf0e10cSrcweir 			return sal_True;
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 	}
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	return sal_False;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
129cdf0e10cSrcweir //--------------------------------------------------
130cdf0e10cSrcweir // osl_getLocalTimeFromSystemTime
131cdf0e10cSrcweir //--------------------------------------------------
132cdf0e10cSrcweir 
osl_getLocalTimeFromSystemTime(TimeValue * pSystemTimeVal,TimeValue * pLocalTimeVal)133cdf0e10cSrcweir sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir 	TIME_ZONE_INFORMATION aTimeZoneInformation;
136cdf0e10cSrcweir 	DWORD Success;
137cdf0e10cSrcweir 	sal_Int64	bias;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	// get timezone information
140cdf0e10cSrcweir 	if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
141cdf0e10cSrcweir 	{
142cdf0e10cSrcweir 		bias=aTimeZoneInformation.Bias;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		// add bias for daylight saving time
145cdf0e10cSrcweir 		if ( Success== TIME_ZONE_ID_DAYLIGHT )
146cdf0e10cSrcweir 			bias+=aTimeZoneInformation.DaylightBias;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		if ( (sal_Int64) pSystemTimeVal->Seconds > ( bias * 60 ) )
149cdf0e10cSrcweir 		{
150cdf0e10cSrcweir 			pLocalTimeVal->Seconds = (sal_uInt32) (pSystemTimeVal->Seconds - ( bias * 60) );
151cdf0e10cSrcweir 			pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 			return sal_True;
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	return sal_False;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir //--------------------------------------------------
161cdf0e10cSrcweir // osl_getSystemTimeFromLocalTime
162cdf0e10cSrcweir //--------------------------------------------------
163cdf0e10cSrcweir 
osl_getSystemTimeFromLocalTime(TimeValue * pLocalTimeVal,TimeValue * pSystemTimeVal)164cdf0e10cSrcweir sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	TIME_ZONE_INFORMATION aTimeZoneInformation;
167cdf0e10cSrcweir 	DWORD Success;
168cdf0e10cSrcweir 	sal_Int64	bias;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	// get timezone information
171cdf0e10cSrcweir 	if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		bias=aTimeZoneInformation.Bias;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 		// add bias for daylight saving time
176cdf0e10cSrcweir 		if ( Success== TIME_ZONE_ID_DAYLIGHT )
177cdf0e10cSrcweir 			bias+=aTimeZoneInformation.DaylightBias;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 		if ( (sal_Int64) pLocalTimeVal->Seconds + ( bias * 60 ) > 0 )
180cdf0e10cSrcweir 		{
181cdf0e10cSrcweir 			pSystemTimeVal->Seconds = (sal_uInt32) ( pLocalTimeVal->Seconds + ( bias * 60) );
182cdf0e10cSrcweir 			pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			return sal_True;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 	}
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	return sal_False;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
192cdf0e10cSrcweir static struct _timeb startTime;
193cdf0e10cSrcweir static sal_Bool bGlobalTimer = sal_False;
194cdf0e10cSrcweir 
osl_getGlobalTimer(void)195cdf0e10cSrcweir sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir   struct _timeb currentTime;
198cdf0e10cSrcweir   sal_uInt32 nSeconds;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir   if ( bGlobalTimer == sal_False )
201cdf0e10cSrcweir   {
202cdf0e10cSrcweir       _ftime( &startTime );
203cdf0e10cSrcweir       bGlobalTimer=sal_True;
204cdf0e10cSrcweir   }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir   _ftime( &currentTime );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir   nSeconds = (sal_uInt32)( currentTime.time - startTime.time );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir   return ( nSeconds * 1000 ) + (long)( currentTime.millitm - startTime.millitm );
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213