1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <osl/diagnose.h>
24 #include <sys/timeb.h>
26 extern sal_Bool
TimeValueToFileTime(const TimeValue
*cpTimeVal
, FILETIME
*pFTime
);
28 extern BOOL
FileTimeToTimeValue( const FILETIME
*cpFTime
, TimeValue
*pTimeVal
);
30 //--------------------------------------------------
32 //--------------------------------------------------
34 sal_Bool SAL_CALL
osl_getSystemTime(TimeValue
* pTimeVal
)
36 SYSTEMTIME SystemTime
;
37 FILETIME CurTime
, OffTime
;
40 OSL_ASSERT(pTimeVal
!= 0);
42 GetSystemTime(&SystemTime
);
43 SystemTimeToFileTime(&SystemTime
, &CurTime
);
45 SystemTime
.wYear
= 1970;
46 SystemTime
.wMonth
= 1;
47 SystemTime
.wDayOfWeek
= 0;
50 SystemTime
.wMinute
= 0;
51 SystemTime
.wSecond
= 0;
52 SystemTime
.wMilliseconds
= 0;
54 SystemTimeToFileTime(&SystemTime
, &OffTime
);
56 Value
= *((__int64
*)&CurTime
) - *((__int64
*)&OffTime
);
58 pTimeVal
->Seconds
= (unsigned long) (Value
/ 10000000L);
59 pTimeVal
->Nanosec
= (unsigned long)((Value
% 10000000L) * 100);
64 //--------------------------------------------------
65 // osl_getDateTimeFromTimeValue
66 //--------------------------------------------------
68 sal_Bool SAL_CALL
osl_getDateTimeFromTimeValue( const TimeValue
* pTimeVal
, oslDateTime
* pDateTime
)
71 SYSTEMTIME aSystemTime
;
73 if ( TimeValueToFileTime(pTimeVal
, &aFileTime
) )
75 if ( FileTimeToSystemTime( &aFileTime
, &aSystemTime
) )
77 pDateTime
->NanoSeconds
= pTimeVal
->Nanosec
;
79 pDateTime
->Seconds
= aSystemTime
.wSecond
;
80 pDateTime
->Minutes
= aSystemTime
.wMinute
;
81 pDateTime
->Hours
= aSystemTime
.wHour
;
82 pDateTime
->Day
= aSystemTime
.wDay
;
83 pDateTime
->DayOfWeek
= aSystemTime
.wDayOfWeek
;
84 pDateTime
->Month
= aSystemTime
.wMonth
;
85 pDateTime
->Year
= aSystemTime
.wYear
;
94 //--------------------------------------------------
95 // osl_getTimeValueFromDateTime
96 //--------------------------------------------------
98 sal_Bool SAL_CALL
osl_getTimeValueFromDateTime( const oslDateTime
* pDateTime
, TimeValue
* pTimeVal
)
101 SYSTEMTIME aSystemTime
;
103 aSystemTime
.wMilliseconds
= 0;
104 aSystemTime
.wSecond
= pDateTime
->Seconds
;
105 aSystemTime
.wMinute
= pDateTime
->Minutes
;
106 aSystemTime
.wHour
= pDateTime
->Hours
;
107 aSystemTime
.wDay
= pDateTime
->Day
;
108 aSystemTime
.wDayOfWeek
= pDateTime
->DayOfWeek
;
109 aSystemTime
.wMonth
= pDateTime
->Month
;
110 aSystemTime
.wYear
= pDateTime
->Year
;
112 if ( SystemTimeToFileTime( &aSystemTime
, &aFileTime
) )
114 if (FileTimeToTimeValue( &aFileTime
, pTimeVal
) )
116 pTimeVal
->Nanosec
= pDateTime
->NanoSeconds
;
125 //--------------------------------------------------
126 // osl_getLocalTimeFromSystemTime
127 //--------------------------------------------------
129 sal_Bool SAL_CALL
osl_getLocalTimeFromSystemTime( const TimeValue
* pSystemTimeVal
, TimeValue
* pLocalTimeVal
)
131 TIME_ZONE_INFORMATION aTimeZoneInformation
;
135 // get timezone information
136 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
138 bias
=aTimeZoneInformation
.Bias
;
140 // add bias for daylight saving time
141 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
142 bias
+=aTimeZoneInformation
.DaylightBias
;
144 if ( (sal_Int64
) pSystemTimeVal
->Seconds
> ( bias
* 60 ) )
146 pLocalTimeVal
->Seconds
= (sal_uInt32
) (pSystemTimeVal
->Seconds
- ( bias
* 60) );
147 pLocalTimeVal
->Nanosec
= pSystemTimeVal
->Nanosec
;
156 //--------------------------------------------------
157 // osl_getSystemTimeFromLocalTime
158 //--------------------------------------------------
160 sal_Bool SAL_CALL
osl_getSystemTimeFromLocalTime( const TimeValue
* pLocalTimeVal
, TimeValue
* pSystemTimeVal
)
162 TIME_ZONE_INFORMATION aTimeZoneInformation
;
166 // get timezone information
167 if ( ( Success
=GetTimeZoneInformation( &aTimeZoneInformation
) ) != TIME_ZONE_ID_INVALID
)
169 bias
=aTimeZoneInformation
.Bias
;
171 // add bias for daylight saving time
172 if ( Success
== TIME_ZONE_ID_DAYLIGHT
)
173 bias
+=aTimeZoneInformation
.DaylightBias
;
175 if ( (sal_Int64
) pLocalTimeVal
->Seconds
+ ( bias
* 60 ) > 0 )
177 pSystemTimeVal
->Seconds
= (sal_uInt32
) ( pLocalTimeVal
->Seconds
+ ( bias
* 60) );
178 pSystemTimeVal
->Nanosec
= pLocalTimeVal
->Nanosec
;
187 static struct _timeb startTime
;
188 void sal_initGlobalTimer()
190 _ftime( &startTime
);
193 sal_uInt32 SAL_CALL
osl_getGlobalTimer(void)
195 struct _timeb currentTime
;
198 _ftime( ¤tTime
);
200 nSeconds
= (sal_uInt32
)( currentTime
.time
- startTime
.time
);
202 return ( nSeconds
* 1000 ) + (long)( currentTime
.millitm
- startTime
.millitm
);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */