xref: /trunk/main/sal/osl/w32/time.c (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb) !
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "system.h"
29 
30 #include <osl/diagnose.h>
31 #include <osl/time.h>
32 #include <sys/timeb.h>
33 
34 extern sal_Bool TimeValueToFileTime(const TimeValue *cpTimeVal, FILETIME *pFTime);
35 
36 extern BOOL FileTimeToTimeValue( const FILETIME *cpFTime, TimeValue *pTimeVal );
37 
38 //--------------------------------------------------
39 // osl_getSystemTime
40 //--------------------------------------------------
41 
42 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
43 {
44     SYSTEMTIME SystemTime;
45     FILETIME   CurTime, OffTime;
46     __int64    Value;
47 
48     OSL_ASSERT(pTimeVal != 0);
49 
50     GetSystemTime(&SystemTime);
51     SystemTimeToFileTime(&SystemTime, &CurTime);
52 
53     SystemTime.wYear         = 1970;
54     SystemTime.wMonth        = 1;
55     SystemTime.wDayOfWeek    = 0;
56     SystemTime.wDay          = 1;
57     SystemTime.wHour         = 0;
58     SystemTime.wMinute       = 0;
59     SystemTime.wSecond       = 0;
60     SystemTime.wMilliseconds = 0;
61 
62     SystemTimeToFileTime(&SystemTime, &OffTime);
63 
64     Value = *((__int64 *)&CurTime) - *((__int64 *)&OffTime);
65 
66     pTimeVal->Seconds  = (unsigned long) (Value / 10000000L);
67     pTimeVal->Nanosec  = (unsigned long)((Value % 10000000L) * 100);
68 
69     return (sal_True);
70 }
71 
72 //--------------------------------------------------
73 // osl_getDateTimeFromTimeValue
74 //--------------------------------------------------
75 
76 sal_Bool SAL_CALL osl_getDateTimeFromTimeValue( TimeValue* pTimeVal, oslDateTime* pDateTime )
77 {
78     FILETIME    aFileTime;
79     SYSTEMTIME  aSystemTime;
80 
81     if ( TimeValueToFileTime(pTimeVal, &aFileTime) )
82     {
83         if ( FileTimeToSystemTime( &aFileTime, &aSystemTime ) )
84         {
85             pDateTime->NanoSeconds  =   pTimeVal->Nanosec;
86 
87             pDateTime->Seconds      =   aSystemTime.wSecond;
88             pDateTime->Minutes      =   aSystemTime.wMinute;
89             pDateTime->Hours        =   aSystemTime.wHour;
90             pDateTime->Day          =   aSystemTime.wDay;
91             pDateTime->DayOfWeek    =   aSystemTime.wDayOfWeek;
92             pDateTime->Month        =   aSystemTime.wMonth;
93             pDateTime->Year         =   aSystemTime.wYear;
94 
95             return sal_True;
96         }
97     }
98 
99     return sal_False;
100 }
101 
102 //--------------------------------------------------
103 // osl_getTimeValueFromDateTime
104 //--------------------------------------------------
105 
106 sal_Bool SAL_CALL osl_getTimeValueFromDateTime( oslDateTime* pDateTime, TimeValue* pTimeVal )
107 {
108     FILETIME    aFileTime;
109     SYSTEMTIME  aSystemTime;
110 
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;
119 
120     if ( SystemTimeToFileTime( &aSystemTime, &aFileTime ) )
121     {
122         if (FileTimeToTimeValue( &aFileTime, pTimeVal  ) )
123         {
124             pTimeVal->Nanosec = pDateTime->NanoSeconds;
125             return sal_True;
126         }
127     }
128 
129     return sal_False;
130 }
131 
132 
133 //--------------------------------------------------
134 // osl_getLocalTimeFromSystemTime
135 //--------------------------------------------------
136 
137 sal_Bool SAL_CALL osl_getLocalTimeFromSystemTime( TimeValue* pSystemTimeVal, TimeValue* pLocalTimeVal )
138 {
139     TIME_ZONE_INFORMATION aTimeZoneInformation;
140     DWORD Success;
141     sal_Int64   bias;
142 
143     // get timezone information
144     if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
145     {
146         bias=aTimeZoneInformation.Bias;
147 
148         // add bias for daylight saving time
149         if ( Success== TIME_ZONE_ID_DAYLIGHT )
150             bias+=aTimeZoneInformation.DaylightBias;
151 
152         if ( (sal_Int64) pSystemTimeVal->Seconds > ( bias * 60 ) )
153         {
154             pLocalTimeVal->Seconds = (sal_uInt32) (pSystemTimeVal->Seconds - ( bias * 60) );
155             pLocalTimeVal->Nanosec = pSystemTimeVal->Nanosec;
156 
157             return sal_True;
158         }
159     }
160 
161     return sal_False;
162 }
163 
164 //--------------------------------------------------
165 // osl_getSystemTimeFromLocalTime
166 //--------------------------------------------------
167 
168 sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( TimeValue* pLocalTimeVal, TimeValue* pSystemTimeVal )
169 {
170     TIME_ZONE_INFORMATION aTimeZoneInformation;
171     DWORD Success;
172     sal_Int64   bias;
173 
174     // get timezone information
175     if ( ( Success=GetTimeZoneInformation( &aTimeZoneInformation ) ) != TIME_ZONE_ID_INVALID)
176     {
177         bias=aTimeZoneInformation.Bias;
178 
179         // add bias for daylight saving time
180         if ( Success== TIME_ZONE_ID_DAYLIGHT )
181             bias+=aTimeZoneInformation.DaylightBias;
182 
183         if ( (sal_Int64) pLocalTimeVal->Seconds + ( bias * 60 ) > 0 )
184         {
185             pSystemTimeVal->Seconds = (sal_uInt32) ( pLocalTimeVal->Seconds + ( bias * 60) );
186             pSystemTimeVal->Nanosec = pLocalTimeVal->Nanosec;
187 
188             return sal_True;
189         }
190     }
191 
192     return sal_False;
193 }
194 
195 
196 static struct _timeb startTime;
197 static sal_Bool bGlobalTimer = sal_False;
198 
199 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
200 {
201   struct _timeb currentTime;
202   sal_uInt32 nSeconds;
203 
204   if ( bGlobalTimer == sal_False )
205   {
206       _ftime( &startTime );
207       bGlobalTimer=sal_True;
208   }
209 
210   _ftime( &currentTime );
211 
212   nSeconds = (sal_uInt32)( currentTime.time - startTime.time );
213 
214   return ( nSeconds * 1000 ) + (long)( currentTime.millitm - startTime.millitm );
215 }
216 
217