Paper size and orientation prop sheet for wineps.
[wine.git] / win32 / time.c
blob42d422c71de4f94f2cef8ef9995e8cee9bc03749
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 */
7 #include <string.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include "file.h"
12 #include "winerror.h"
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 */
25 ptm = gmtime(&utc);
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)
36 time_t local_time;
37 struct tm *local_tm;
38 struct timeval tv;
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)
63 struct timeval tv;
64 struct tm t;
65 time_t sec;
66 time_t oldsec=time(NULL);
67 int err;
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;
76 t.tm_isdst = -1;
77 sec = mktime (&t);
79 /* set the new time */
80 tv.tv_sec = sec;
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 ){
85 err = 1;
86 SetLastError(ERROR_INVALID_PARAMETER);
87 } else {
88 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
89 if(err == 0)
90 return TRUE;
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." );
98 return FALSE;
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;
113 return TRUE;
117 /***********************************************************************
118 * GetSystemTime (KERNEL32.285)
120 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
122 time_t local_time;
123 struct tm *local_tm;
124 struct timeval tv;
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)
146 struct timeval tv;
147 struct timezone tz;
148 struct tm t;
149 time_t sec, oldsec;
150 int dst, bias;
151 int err;
153 /* call gettimeofday to get the current timezone */
154 gettimeofday(&tv, &tz);
155 oldsec=tv.tv_sec;
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;
167 t.tm_isdst = dst;
168 sec = mktime (&t);
169 /* correct for timezone and daylight */
170 sec += bias;
172 /* set the new time */
173 tv.tv_sec = sec;
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 ){
178 err = 1;
179 SetLastError(ERROR_INVALID_PARAMETER);
180 } else {
181 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
182 if(err == 0)
183 return TRUE;
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." );
191 return FALSE;
195 /***********************************************************************
196 * GetTimeZoneInformation (KERNEL32.302)
198 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
200 time_t gmt;
201 int bias, daylight;
203 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
205 gmt = time(NULL);
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)
221 struct timezone tz;
223 tz.tz_minuteswest = tzinfo->Bias;
224 #ifdef DST_NONE
225 tz.tz_dsttime = DST_NONE;
226 #else
227 tz.tz_dsttime = 0;
228 #endif
229 return !settimeofday(NULL, &tz);
233 /***********************************************************************
234 * GetSystemTimeAsFileTime (KERNEL32)
236 VOID WINAPI GetSystemTimeAsFileTime(LPFILETIME systemtimeAsfiletime)
238 struct timeval now;
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) {
252 FIXME(":stub\n");
253 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
254 return FALSE;