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