2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
10 #include <sys/times.h>
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(win32);
18 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
19 #define SETTIME_MAX_ADJUST 120
21 /* TIME_GetBias: helper function calculates delta local time from UTC */
22 static int TIME_GetBias( time_t utc, int *pdaylight)
24 struct tm *ptm = localtime(&utc);
25 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
27 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
28 return (int)(utc-mktime(ptm));
32 /***********************************************************************
33 * SetLocalTime (KERNEL32.655)
35 * FIXME: correct ? Is the timezone param of settimeofday() needed ?
36 * I don't have any docu about SetLocal/SystemTime(), argl...
38 BOOL WINAPI SetLocalTime(const SYSTEMTIME *systime)
43 time_t oldsec=time(NULL);
46 /* get the number of seconds */
47 t.tm_sec = systime->wSecond;
48 t.tm_min = systime->wMinute;
49 t.tm_hour = systime->wHour;
50 t.tm_mday = systime->wDay;
51 t.tm_mon = systime->wMonth - 1;
52 t.tm_year = systime->wYear - 1900;
56 /* set the new time */
58 tv.tv_usec = systime->wMilliseconds * 1000;
60 /* error and sanity check*/
61 if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
63 SetLastError(ERROR_INVALID_PARAMETER);
65 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
68 SetLastError(ERROR_PRIVILEGE_NOT_HELD);
70 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
71 systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
72 systime->wMinute, systime->wSecond,
73 sec-oldsec, err == -1 ? "No Permission" :
74 sec==(time_t)-1 ? "" : "is too large." );
79 /***********************************************************************
80 * GetSystemTimeAdjustment (KERNEL32.407)
83 DWORD WINAPI GetSystemTimeAdjustment( LPDWORD lpTimeAdjustment,
84 LPDWORD lpTimeIncrement,
85 LPBOOL lpTimeAdjustmentDisabled )
87 *lpTimeAdjustment = 0;
89 *lpTimeAdjustmentDisabled = TRUE;
94 /***********************************************************************
95 * SetSystemTime (KERNEL32.507)
97 BOOL WINAPI SetSystemTime(const SYSTEMTIME *systime)
106 /* call gettimeofday to get the current timezone */
107 gettimeofday(&tv, &tz);
109 /* get delta local time from utc */
110 bias=TIME_GetBias(oldsec,&dst);
112 /* get the number of seconds */
113 t.tm_sec = systime->wSecond;
114 t.tm_min = systime->wMinute;
115 t.tm_hour = systime->wHour;
116 t.tm_mday = systime->wDay;
117 t.tm_mon = systime->wMonth - 1;
118 t.tm_year = systime->wYear - 1900;
121 /* correct for timezone and daylight */
124 /* set the new time */
126 tv.tv_usec = systime->wMilliseconds * 1000;
128 /* error and sanity check*/
129 if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
131 SetLastError(ERROR_INVALID_PARAMETER);
133 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
136 SetLastError(ERROR_PRIVILEGE_NOT_HELD);
138 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
139 systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
140 systime->wMinute, systime->wSecond,
141 sec-oldsec, err == -1 ? "No Permission" :
142 sec==(time_t)-1 ? "" : "is too large." );
147 /***********************************************************************
148 * GetTimeZoneInformation (KERNEL32.302)
150 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
155 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
158 bias=TIME_GetBias(gmt,&daylight);
160 tzinfo->Bias = -bias / 60;
161 tzinfo->StandardBias = 0;
162 tzinfo->DaylightBias = -60;
164 return TIME_ZONE_ID_UNKNOWN;
168 /***********************************************************************
169 * SetTimeZoneInformation (KERNEL32.515)
171 BOOL WINAPI SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo)
175 tz.tz_minuteswest = tzinfo->Bias;
177 tz.tz_dsttime = DST_NONE;
181 return !settimeofday(NULL, &tz);
185 /***********************************************************************
186 * SystemTimeToTzSpecificLocalTime (KERNEL32.683)
188 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
189 LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
190 LPSYSTEMTIME lpUniversalTime,
191 LPSYSTEMTIME lpLocalTime)
194 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
199 /***********************************************************************
200 * GetSystemTimeAsFileTime (KERNEL32)
202 VOID WINAPI GetSystemTimeAsFileTime( LPFILETIME time )
204 NtQuerySystemTime( (LARGE_INTEGER *)time );
208 /*********************************************************************
209 * TIME_ClockTimeToFileTime
210 * (olorin@fandra.org, 20-Sep-1998)
211 * Converts clock_t into FILETIME.
212 * Used by GetProcessTime.
213 * Differences to UnixTimeToFileTime:
214 * 1) Divided by CLK_TCK
215 * 2) Time is relative. There is no 'starting date', so there is
216 * no need in offset correction, like in UnixTimeToFileTime
218 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
220 LONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
221 ((LARGE_INTEGER *)filetime)->QuadPart = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
224 /*********************************************************************
225 * GetProcessTimes [KERNEL32.262]
227 * FIXME: lpCreationTime, lpExitTime are NOT INITIALIZED.
228 * olorin@fandra.org: Would be nice to substract the cpu time,
229 * used by Wine at startup.
230 * Also, there is a need to separate times
231 * used by different applications.
233 BOOL WINAPI GetProcessTimes( HANDLE hprocess,LPFILETIME lpCreationTime,LPFILETIME lpExitTime,
234 LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
239 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
240 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);