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 * GetSystemTime (KERNEL32.285)
105 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
111 gettimeofday(&tv, NULL);
112 local_time = tv.tv_sec;
113 local_tm = gmtime(&local_time);
115 systime->wYear = local_tm->tm_year + 1900;
116 systime->wMonth = local_tm->tm_mon + 1;
117 systime->wDayOfWeek = local_tm->tm_wday;
118 systime->wDay = local_tm->tm_mday;
119 systime->wHour = local_tm->tm_hour;
120 systime->wMinute = local_tm->tm_min;
121 systime->wSecond = local_tm->tm_sec;
122 systime->wMilliseconds = (tv.tv_usec / 1000) % 1000;
126 /***********************************************************************
127 * SetSystemTime (KERNEL32.507)
129 BOOL WINAPI SetSystemTime(const SYSTEMTIME *systime)
138 /* call gettimeofday to get the current timezone */
139 gettimeofday(&tv, &tz);
141 /* get delta local time from utc */
142 bias=TIME_GetBias(oldsec,&dst);
145 /* get the number of seconds */
146 t.tm_sec = systime->wSecond;
147 t.tm_min = systime->wMinute;
148 t.tm_hour = systime->wHour;
149 t.tm_mday = systime->wDay;
150 t.tm_mon = systime->wMonth - 1;
151 t.tm_year = systime->wYear - 1900;
154 /* correct for timezone and daylight */
157 /* set the new time */
159 tv.tv_usec = systime->wMilliseconds * 1000;
161 /* error and sanity check*/
162 if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
164 SetLastError(ERROR_INVALID_PARAMETER);
166 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
169 SetLastError(ERROR_PRIVILEGE_NOT_HELD);
171 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
172 systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
173 systime->wMinute, systime->wSecond,
174 sec-oldsec, err == -1 ? "No Permission" :
175 sec==(time_t)-1 ? "" : "is too large." );
180 /***********************************************************************
181 * GetTimeZoneInformation (KERNEL32.302)
183 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
188 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
191 bias=TIME_GetBias(gmt,&daylight);
193 tzinfo->Bias = -bias / 60;
194 tzinfo->StandardBias = 0;
195 tzinfo->DaylightBias = -60;
197 return TIME_ZONE_ID_UNKNOWN;
201 /***********************************************************************
202 * SetTimeZoneInformation (KERNEL32.515)
204 BOOL WINAPI SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo)
208 tz.tz_minuteswest = tzinfo->Bias;
210 tz.tz_dsttime = DST_NONE;
214 return !settimeofday(NULL, &tz);
218 /***********************************************************************
219 * GetSystemTimeAsFileTime (KERNEL32)
221 VOID WINAPI GetSystemTimeAsFileTime(LPFILETIME systemtimeAsfiletime)
224 gettimeofday( &now, 0 );
225 /* FIXME: convert to UTC */
226 DOSFS_UnixTimeToFileTime( now.tv_sec, systemtimeAsfiletime, now.tv_usec * 10 );
229 /***********************************************************************
230 * SystemTimeToTzSpecificLocalTime (KERNEL32.683)
232 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
233 LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
234 LPSYSTEMTIME lpUniversalTime,
235 LPSYSTEMTIME lpLocalTime) {
238 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);