2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(win32
)
17 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
18 #define SETTIME_MAX_ADJUST 120
20 /* TIME_GetBias: helper function calculates delta local time from UTC */
21 static int TIME_GetBias( time_t utc
, int *pdaylight
)
23 struct tm
*ptm
= localtime(&utc
);
24 *pdaylight
= ptm
->tm_isdst
; /* daylight for local timezone */
26 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
27 return (int)(utc
-mktime(ptm
));
31 /***********************************************************************
32 * GetLocalTime (KERNEL32.228)
34 VOID WINAPI
GetLocalTime(LPSYSTEMTIME systime
)
40 gettimeofday(&tv
, NULL
);
41 local_time
= tv
.tv_sec
;
42 local_tm
= localtime(&local_time
);
44 systime
->wYear
= local_tm
->tm_year
+ 1900;
45 systime
->wMonth
= local_tm
->tm_mon
+ 1;
46 systime
->wDayOfWeek
= local_tm
->tm_wday
;
47 systime
->wDay
= local_tm
->tm_mday
;
48 systime
->wHour
= local_tm
->tm_hour
;
49 systime
->wMinute
= local_tm
->tm_min
;
50 systime
->wSecond
= local_tm
->tm_sec
;
51 systime
->wMilliseconds
= (tv
.tv_usec
/ 1000) % 1000;
55 /***********************************************************************
56 * SetLocalTime (KERNEL32.655)
58 * FIXME: correct ? Is the timezone param of settimeofday() needed ?
59 * I don't have any docu about SetLocal/SystemTime(), argl...
61 BOOL WINAPI
SetLocalTime(const SYSTEMTIME
*systime
)
66 time_t oldsec
=time(NULL
);
69 /* get the number of seconds */
70 t
.tm_sec
= systime
->wSecond
;
71 t
.tm_min
= systime
->wMinute
;
72 t
.tm_hour
= systime
->wHour
;
73 t
.tm_mday
= systime
->wDay
;
74 t
.tm_mon
= systime
->wMonth
- 1;
75 t
.tm_year
= systime
->wYear
- 1900;
79 /* set the new time */
81 tv
.tv_usec
= systime
->wMilliseconds
* 1000;
83 /* error and sanity check*/
84 if( sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
){
86 SetLastError(ERROR_INVALID_PARAMETER
);
88 err
=settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
91 SetLastError(ERROR_PRIVILEGE_NOT_HELD
);
93 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
94 systime
->wYear
, systime
->wMonth
, systime
->wDay
, systime
->wHour
,
95 systime
->wMinute
, systime
->wSecond
,
96 sec
-oldsec
, err
== -1 ? "No Permission" :
97 sec
==(time_t)-1 ? "" : "is too large." );
102 /***********************************************************************
103 * GetSystemTimeAdjustment (KERNEL32.407)
106 DWORD WINAPI
GetSystemTimeAdjustment( LPDWORD lpTimeAdjustment
,
107 LPDWORD lpTimeIncrement
,
108 LPBOOL lpTimeAdjustmentDisabled
)
110 *lpTimeAdjustment
= 0;
111 *lpTimeIncrement
= 0;
112 *lpTimeAdjustmentDisabled
= TRUE
;
117 /***********************************************************************
118 * GetSystemTime (KERNEL32.285)
120 VOID WINAPI
GetSystemTime(LPSYSTEMTIME systime
)
126 gettimeofday(&tv
, NULL
);
127 local_time
= tv
.tv_sec
;
128 local_tm
= gmtime(&local_time
);
130 systime
->wYear
= local_tm
->tm_year
+ 1900;
131 systime
->wMonth
= local_tm
->tm_mon
+ 1;
132 systime
->wDayOfWeek
= local_tm
->tm_wday
;
133 systime
->wDay
= local_tm
->tm_mday
;
134 systime
->wHour
= local_tm
->tm_hour
;
135 systime
->wMinute
= local_tm
->tm_min
;
136 systime
->wSecond
= local_tm
->tm_sec
;
137 systime
->wMilliseconds
= (tv
.tv_usec
/ 1000) % 1000;
141 /***********************************************************************
142 * SetSystemTime (KERNEL32.507)
144 BOOL WINAPI
SetSystemTime(const SYSTEMTIME
*systime
)
153 /* call gettimeofday to get the current timezone */
154 gettimeofday(&tv
, &tz
);
156 /* get delta local time from utc */
157 bias
=TIME_GetBias(oldsec
,&dst
);
160 /* get the number of seconds */
161 t
.tm_sec
= systime
->wSecond
;
162 t
.tm_min
= systime
->wMinute
;
163 t
.tm_hour
= systime
->wHour
;
164 t
.tm_mday
= systime
->wDay
;
165 t
.tm_mon
= systime
->wMonth
- 1;
166 t
.tm_year
= systime
->wYear
- 1900;
169 /* correct for timezone and daylight */
172 /* set the new time */
174 tv
.tv_usec
= systime
->wMilliseconds
* 1000;
176 /* error and sanity check*/
177 if( sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
){
179 SetLastError(ERROR_INVALID_PARAMETER
);
181 err
=settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
184 SetLastError(ERROR_PRIVILEGE_NOT_HELD
);
186 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
187 systime
->wYear
, systime
->wMonth
, systime
->wDay
, systime
->wHour
,
188 systime
->wMinute
, systime
->wSecond
,
189 sec
-oldsec
, err
== -1 ? "No Permission" :
190 sec
==(time_t)-1 ? "" : "is too large." );
195 /***********************************************************************
196 * GetTimeZoneInformation (KERNEL32.302)
198 DWORD WINAPI
GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo
)
203 memset(tzinfo
, 0, sizeof(TIME_ZONE_INFORMATION
));
206 bias
=TIME_GetBias(gmt
,&daylight
);
208 tzinfo
->Bias
= -bias
/ 60;
209 tzinfo
->StandardBias
= 0;
210 tzinfo
->DaylightBias
= -60;
212 return TIME_ZONE_ID_UNKNOWN
;
216 /***********************************************************************
217 * SetTimeZoneInformation (KERNEL32.515)
219 BOOL WINAPI
SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo
)
223 tz
.tz_minuteswest
= tzinfo
->Bias
;
225 tz
.tz_dsttime
= DST_NONE
;
229 return !settimeofday(NULL
, &tz
);
233 /***********************************************************************
234 * GetSystemTimeAsFileTime (KERNEL32)
236 VOID WINAPI
GetSystemTimeAsFileTime(LPFILETIME systemtimeAsfiletime
)
239 gettimeofday( &now
, 0 );
240 /* FIXME: convert to UTC */
241 DOSFS_UnixTimeToFileTime( now
.tv_sec
, systemtimeAsfiletime
, now
.tv_usec
* 10 );
244 /***********************************************************************
245 * SystemTimeToTzSpecificLocalTime (KERNEL32.683)
247 BOOL WINAPI
SystemTimeToTzSpecificLocalTime(
248 LPTIME_ZONE_INFORMATION lpTimeZoneInformation
,
249 LPSYSTEMTIME lpUniversalTime
,
250 LPSYSTEMTIME lpLocalTime
) {
253 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);