1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
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 ************************************************************************/
33 #include <osl/diagnose.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 //--------------------------------------------------
43 //--------------------------------------------------
45 sal_Bool SAL_CALL
osl_getSystemTime(TimeValue
* pTimeVal
)
47 SYSTEMTIME SystemTime
;
48 FILETIME CurTime
, OffTime
;
51 OSL_ASSERT(pTimeVal
!= 0);
53 GetSystemTime(&SystemTime
);
54 SystemTimeToFileTime(&SystemTime
, &CurTime
);
56 SystemTime
.wYear
= 1970;
57 SystemTime
.wMonth
= 1;
58 SystemTime
.wDayOfWeek
= 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);
75 //--------------------------------------------------
76 // osl_getDateTimeFromTimeValue
77 //--------------------------------------------------
79 sal_Bool SAL_CALL
osl_getDateTimeFromTimeValue( TimeValue
* pTimeVal
, oslDateTime
* pDateTime
)
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
;
105 //--------------------------------------------------
106 // osl_getTimeValueFromDateTime
107 //--------------------------------------------------
109 sal_Bool SAL_CALL
osl_getTimeValueFromDateTime( oslDateTime
* pDateTime
, TimeValue
* pTimeVal
)
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
;
136 //--------------------------------------------------
137 // osl_getLocalTimeFromSystemTime
138 //--------------------------------------------------
140 sal_Bool SAL_CALL
osl_getLocalTimeFromSystemTime( TimeValue
* pSystemTimeVal
, TimeValue
* pLocalTimeVal
)
142 TIME_ZONE_INFORMATION aTimeZoneInformation
;
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
;
167 //--------------------------------------------------
168 // osl_getSystemTimeFromLocalTime
169 //--------------------------------------------------
171 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
173 TIME_ZONE_INFORMATION aTimeZoneInformation
;
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
;
199 static struct _timeb startTime
;
200 static sal_Bool bGlobalTimer
= sal_False
;
202 sal_uInt32 SAL_CALL
osl_getGlobalTimer(void)
204 struct _timeb currentTime
;
207 if ( bGlobalTimer
== sal_False
)
209 _ftime( &startTime
);
210 bGlobalTimer
=sal_True
;
213 _ftime( ¤tTime
);
215 nSeconds
= (sal_uInt32
)( currentTime
.time
- startTime
.time
);
217 return ( nSeconds
* 1000 ) + (long)( currentTime
.millitm
- startTime
.millitm
);