Added a few more large integer functions.
[wine] / dlls / kernel / time.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  */
6
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/time.h>
10 #include <sys/times.h>
11 #include "file.h"
12 #include "ntddk.h"
13 #include "winerror.h"
14 #include "debugtools.h"
15
16 DEFAULT_DEBUG_CHANNEL(win32);
17
18 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
19 #define SETTIME_MAX_ADJUST 120
20
21 /* TIME_GetBias: helper function calculates delta local time from UTC */
22 static int TIME_GetBias( time_t utc, int *pdaylight)
23 {
24     struct tm *ptm = localtime(&utc);
25     *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
26     ptm = gmtime(&utc);
27     ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
28     return (int)(utc-mktime(ptm));
29 }
30
31
32 /***********************************************************************
33  *              SetLocalTime            (KERNEL32.655)
34  *
35  * FIXME: correct ? Is the timezone param of settimeofday() needed ?
36  * I don't have any docu about SetLocal/SystemTime(), argl...
37  */
38 BOOL WINAPI SetLocalTime(const SYSTEMTIME *systime)
39 {
40     struct timeval tv;
41     struct tm t;
42     time_t sec;
43     time_t oldsec=time(NULL);
44     int err;
45
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;
53     t.tm_isdst = -1;
54     sec = mktime (&t);
55
56     /* set the new time */
57     tv.tv_sec = sec;
58     tv.tv_usec = systime->wMilliseconds * 1000;
59
60     /* error and sanity check*/
61     if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
62         err = 1;
63         SetLastError(ERROR_INVALID_PARAMETER);
64     } else {
65         err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
66         if(err == 0)
67             return TRUE;
68         SetLastError(ERROR_PRIVILEGE_NOT_HELD);
69     }
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." );
75     return FALSE;
76 }
77
78
79 /***********************************************************************
80  *           GetSystemTimeAdjustment     (KERNEL32.407)
81  *
82  */
83 DWORD WINAPI GetSystemTimeAdjustment( LPDWORD lpTimeAdjustment,
84                                       LPDWORD lpTimeIncrement,
85                                       LPBOOL lpTimeAdjustmentDisabled )
86 {
87     *lpTimeAdjustment = 0;
88     *lpTimeIncrement = 0;
89     *lpTimeAdjustmentDisabled = TRUE;
90     return TRUE;
91 }
92
93
94 /***********************************************************************
95  *              SetSystemTime            (KERNEL32.507)
96  */
97 BOOL WINAPI SetSystemTime(const SYSTEMTIME *systime)
98 {
99     struct timeval tv;
100     struct timezone tz;
101     struct tm t;
102     time_t sec, oldsec;
103     int dst, bias;
104     int err;
105
106     /* call gettimeofday to get the current timezone */
107     gettimeofday(&tv, &tz);
108     oldsec=tv.tv_sec;
109     /* get delta local time from utc */
110     bias=TIME_GetBias(oldsec,&dst);
111
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;
119     t.tm_isdst = dst;
120     sec = mktime (&t);
121     /* correct for timezone and daylight */
122     sec += bias;
123
124     /* set the new time */
125     tv.tv_sec = sec;
126     tv.tv_usec = systime->wMilliseconds * 1000;
127
128     /* error and sanity check*/
129     if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
130         err = 1;
131         SetLastError(ERROR_INVALID_PARAMETER);
132     } else {
133         err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
134         if(err == 0)
135             return TRUE;
136         SetLastError(ERROR_PRIVILEGE_NOT_HELD);
137     }
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." );
143     return FALSE;
144 }
145
146
147 /***********************************************************************
148  *              GetTimeZoneInformation  (KERNEL32.302)
149  */
150 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
151 {
152     time_t gmt;
153     int bias, daylight;
154
155     memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
156
157     gmt = time(NULL);
158     bias=TIME_GetBias(gmt,&daylight);
159
160     tzinfo->Bias = -bias / 60;
161     tzinfo->StandardBias = 0;
162     tzinfo->DaylightBias = -60;
163
164     return TIME_ZONE_ID_UNKNOWN;
165 }
166
167
168 /***********************************************************************
169  *              SetTimeZoneInformation  (KERNEL32.515)
170  */
171 BOOL WINAPI SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo)
172 {
173     struct timezone tz;
174
175     tz.tz_minuteswest = tzinfo->Bias;
176 #ifdef DST_NONE
177     tz.tz_dsttime = DST_NONE;
178 #else
179     tz.tz_dsttime = 0;
180 #endif
181     return !settimeofday(NULL, &tz);
182 }
183
184
185 /***********************************************************************
186  *              SystemTimeToTzSpecificLocalTime  (KERNEL32.683)
187  */
188 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
189   LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
190   LPSYSTEMTIME lpUniversalTime,
191   LPSYSTEMTIME lpLocalTime)
192 {
193   FIXME(":stub\n");
194   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
195        return FALSE;
196 }
197
198
199 /***********************************************************************
200  *              GetSystemTimeAsFileTime  (KERNEL32)
201  */
202 VOID WINAPI GetSystemTimeAsFileTime( LPFILETIME time )
203 {
204     NtQuerySystemTime( (LARGE_INTEGER *)time );
205 }
206
207
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
217  */
218 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
219 {
220     LONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
221     ((LARGE_INTEGER *)filetime)->QuadPart = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
222 }
223
224 /*********************************************************************
225  *      GetProcessTimes                         [KERNEL32.262]
226  *
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.
232  */
233 BOOL WINAPI GetProcessTimes( HANDLE hprocess,LPFILETIME lpCreationTime,LPFILETIME lpExitTime,
234                              LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
235 {
236     struct tms tms;
237
238     times(&tms);
239     TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
240     TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
241     return TRUE;
242 }