2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
11 #include <sys/times.h>
16 #include "wine/unicode.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(win32);
21 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
22 #define SETTIME_MAX_ADJUST 120
25 /* This structure is used to store strings that represent all of the time zones
26 in the world. (This is used to help GetTimeZoneInformation)
30 const char *psTZFromUnix;
31 WCHAR psTZWindows[32];
36 static const struct tagTZ_INFO TZ_INFO[] =
39 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
42 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
45 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
48 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
51 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
54 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
57 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
60 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
63 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
66 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
69 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
72 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
75 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
78 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
81 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
84 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
87 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
90 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
93 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
96 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
99 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
102 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
105 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
108 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
111 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
114 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
117 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
120 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
123 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
126 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
129 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
132 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
135 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
138 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
141 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
144 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
147 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
150 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
153 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
156 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
159 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
162 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
165 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
168 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
171 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
174 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
177 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
180 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
183 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
186 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
189 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
192 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
195 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
198 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
201 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
204 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
207 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
210 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
213 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
216 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
219 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
222 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
225 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
228 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
232 /* TIME_GetTZAsStr: helper function that returns the given timezone as a string.
233 This could be done with a hash table instead of merely iterating through
234 a table, however with the small amount of entries (60 or so) I didn't think
236 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
239 struct tm *ptm = localtime(&utc);
242 if (!strftime (psTZName, 7, "%Z", ptm))
245 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
247 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
248 TZ_INFO[i].bias == bias &&
249 TZ_INFO[i].dst == dst
251 return TZ_INFO[i].psTZWindows;
258 /* TIME_GetBias: helper function calculates delta local time from UTC */
259 static int TIME_GetBias( time_t utc, int *pdaylight)
261 struct tm *ptm = localtime(&utc);
262 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
264 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
265 return (int)(utc-mktime(ptm));
269 /***********************************************************************
270 * SetLocalTime (KERNEL32.@)
272 * Sets the local time using current time zone and daylight
277 * True if the time was set, false if the time was invalid or the
278 * necessary permissions were not held.
280 BOOL WINAPI SetLocalTime(
281 const SYSTEMTIME *systime) /* [in] The desired local time. */
286 time_t oldsec=time(NULL);
289 /* get the number of seconds */
290 t.tm_sec = systime->wSecond;
291 t.tm_min = systime->wMinute;
292 t.tm_hour = systime->wHour;
293 t.tm_mday = systime->wDay;
294 t.tm_mon = systime->wMonth - 1;
295 t.tm_year = systime->wYear - 1900;
299 /* set the new time */
301 tv.tv_usec = systime->wMilliseconds * 1000;
303 /* error and sanity check*/
304 if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
306 SetLastError(ERROR_INVALID_PARAMETER);
308 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
311 SetLastError(ERROR_PRIVILEGE_NOT_HELD);
313 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
314 systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
315 systime->wMinute, systime->wSecond,
316 sec-oldsec, err == -1 ? "No Permission" :
317 sec==(time_t)-1 ? "" : "is too large." );
322 /***********************************************************************
323 * GetSystemTimeAdjustment (KERNEL32.@)
325 * Indicates the period between clock interrupt and the amount the clock
326 * is adjusted each interrupt so as to keep it insync with an external source.
330 * Always returns true.
334 * Only the special case of disabled time adjustments is supported.
335 * (also the signature is wrong it should have a return type of BOOL)
337 DWORD WINAPI GetSystemTimeAdjustment(
338 LPDWORD lpTimeAdjustment, /* [out] The clock adjustment per interupt in 100's of nanoseconds. */
339 LPDWORD lpTimeIncrement, /* [out] The time between clock interupts in 100's of nanoseconds. */
340 LPBOOL lpTimeAdjustmentDisabled) /* [out] The clock synchonisation has been disabled. */
342 *lpTimeAdjustment = 0;
343 *lpTimeIncrement = 0;
344 *lpTimeAdjustmentDisabled = TRUE;
349 /***********************************************************************
350 * SetSystemTime (KERNEL32.@)
352 * Sets the system time (utc).
356 * True if the time was set, false if the time was invalid or the
357 * necessary permissions were not held.
359 BOOL WINAPI SetSystemTime(
360 const SYSTEMTIME *systime) /* [in] The desired system time. */
369 /* call gettimeofday to get the current timezone */
370 gettimeofday(&tv, &tz);
372 /* get delta local time from utc */
373 bias=TIME_GetBias(oldsec,&dst);
375 /* get the number of seconds */
376 t.tm_sec = systime->wSecond;
377 t.tm_min = systime->wMinute;
378 t.tm_hour = systime->wHour;
379 t.tm_mday = systime->wDay;
380 t.tm_mon = systime->wMonth - 1;
381 t.tm_year = systime->wYear - 1900;
384 /* correct for timezone and daylight */
387 /* set the new time */
389 tv.tv_usec = systime->wMilliseconds * 1000;
391 /* error and sanity check*/
392 if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
394 SetLastError(ERROR_INVALID_PARAMETER);
396 err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
399 SetLastError(ERROR_PRIVILEGE_NOT_HELD);
401 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
402 systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
403 systime->wMinute, systime->wSecond,
404 sec-oldsec, err == -1 ? "No Permission" :
405 sec==(time_t)-1 ? "" : "is too large." );
410 /***********************************************************************
411 * GetTimeZoneInformation (KERNEL32.@)
413 * Fills in the a time zone information structure with values based on
414 * the current local time.
418 * The daylight savings time standard or TIME_ZONE_ID_INVALID if the call failed.
420 DWORD WINAPI GetTimeZoneInformation(
421 LPTIME_ZONE_INFORMATION tzinfo) /* [out] The time zone structure to be filled in. */
428 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
431 bias=TIME_GetBias(gmt,&daylight);
433 tzinfo->Bias = -bias / 60;
434 tzinfo->StandardBias = 0;
435 tzinfo->DaylightBias = -60;
436 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
437 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
438 return TIME_ZONE_ID_STANDARD;
442 /***********************************************************************
443 * SetTimeZoneInformation (KERNEL32.@)
445 * Set the local time zone with values based on the time zone structure.
449 * True on successful setting of the time zone.
453 * Use the obsolete unix timezone structure and tz_dsttime member.
455 BOOL WINAPI SetTimeZoneInformation(
456 const LPTIME_ZONE_INFORMATION tzinfo) /* [in] The new time zone. */
460 tz.tz_minuteswest = tzinfo->Bias;
462 tz.tz_dsttime = DST_NONE;
466 return !settimeofday(NULL, &tz);
470 /***********************************************************************
471 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
473 * Converts the system time (utc) to the local time in the specified time zone.
477 * Returns true when the local time was calculated.
481 * Does not handle daylight savings time adjustments correctly.
483 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
484 LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
485 LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
486 LPSYSTEMTIME lpLocalTime) /* [out] The local time in the time zone. */
489 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
494 /***********************************************************************
495 * GetSystemTimeAsFileTime (KERNEL32.@)
497 * Fills in a file time structure with the current time in UTC format.
499 VOID WINAPI GetSystemTimeAsFileTime(
500 LPFILETIME time) /* [out] The file time struct to be filled with the system time. */
502 NtQuerySystemTime( (LARGE_INTEGER *)time );
506 /*********************************************************************
507 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
509 * Used by GetProcessTimes to convert clock_t into FILETIME.
511 * Differences to UnixTimeToFileTime:
512 * 1) Divided by CLK_TCK
513 * 2) Time is relative. There is no 'starting date', so there is
514 * no need in offset correction, like in UnixTimeToFileTime
516 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
518 LONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
519 ((LARGE_INTEGER *)filetime)->QuadPart = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
522 /*********************************************************************
523 * GetProcessTimes (KERNEL32.@)
525 * Returns the user and kernel execution times of a process,
526 * along with the creation and exit times if known.
529 * Would be nice to subtract the cpu time, used by Wine at startup.
530 * Also, there is a need to separate times used by different applications.
534 * Always returns true.
538 * lpCreationTime, lpExitTime are NOT INITIALIZED.
540 BOOL WINAPI GetProcessTimes(
541 HANDLE hprocess, /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
542 LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
543 LPFILETIME lpExitTime, /* [out] The exit time of the process if exited. */
544 LPFILETIME lpKernelTime, /* [out] The time spent in kernal routines in 100's of nanoseconds. */
545 LPFILETIME lpUserTime) /* [out] The time spent in user routines in 100's of nanoseconds. */
550 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
551 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
555 /*********************************************************************
556 * GetCalendarInfoA (KERNEL32.@)
559 int WINAPI GetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType,
560 LPSTR lpCalData, int cchData, LPDWORD lpValue)
562 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): stub\n",
563 Locale, Calendar, CalType, lpCalData, cchData, lpValue);
564 /* dont have the #define values for those */
568 if (CalType & CAL_NOUSEROVERRIDE)
569 MESSAGE("CAL_NOUSEROVERRIDE|");
570 if (CalType & CAL_RETURN_NUMBER)
571 MESSAGE("CAL_RETURN_NUMBER|");
572 if (CalType & CAL_USE_CP_ACP)
573 MESSAGE("CAL_USE_CP_ACP|");
576 /*switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {*/
577 switch (CalType & 0xffff) {
578 #define X(x) case x: MESSAGE(#x"\n");break;
581 X(CAL_IYEAROFFSETRANGE)
592 X(CAL_SABBREVDAYNAME1)
593 X(CAL_SABBREVDAYNAME2)
594 X(CAL_SABBREVDAYNAME3)
595 X(CAL_SABBREVDAYNAME4)
596 X(CAL_SABBREVDAYNAME5)
597 X(CAL_SABBREVDAYNAME6)
598 X(CAL_SABBREVDAYNAME7)
612 X(CAL_SABBREVMONTHNAME1)
613 X(CAL_SABBREVMONTHNAME2)
614 X(CAL_SABBREVMONTHNAME3)
615 X(CAL_SABBREVMONTHNAME4)
616 X(CAL_SABBREVMONTHNAME5)
617 X(CAL_SABBREVMONTHNAME6)
618 X(CAL_SABBREVMONTHNAME7)
619 X(CAL_SABBREVMONTHNAME8)
620 X(CAL_SABBREVMONTHNAME9)
621 X(CAL_SABBREVMONTHNAME10)
622 X(CAL_SABBREVMONTHNAME11)
623 X(CAL_SABBREVMONTHNAME12)
624 X(CAL_SABBREVMONTHNAME13)
627 default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
633 /*********************************************************************
634 * GetCalendarInfoW (KERNEL32.@)
637 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
638 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
640 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): stub\n",
641 Locale, Calendar, CalType, lpCalData, cchData, lpValue);
645 /*********************************************************************
646 * SetCalendarInfoA (KERNEL32.@)
649 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
651 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
652 Locale, Calendar, CalType, debugstr_a(lpCalData));
656 /*********************************************************************
657 * SetCalendarInfoW (KERNEL32.@)
660 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
662 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
663 Locale, Calendar, CalType, debugstr_w(lpCalData));