Use Interlocked* functions in AddRef and Release.
[wine.git] / dlls / kernel / time.c
blob2be543f884c5e88337e956ec7051e7a2480c0739
1 /*
2 * Win32 kernel time functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
32 #endif
33 #ifdef HAVE_SYS_TIMES_H
34 # include <sys/times.h>
35 #endif
37 #define NONAMELESSUNION
38 #define NONAMELESSSTRUCT
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winreg.h"
42 #include "ntstatus.h"
43 #include "winternl.h"
44 #include "winerror.h"
45 #include "winnls.h"
46 #include "kernel_private.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(time);
52 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
53 #define SETTIME_MAX_ADJUST 120
54 #define CALINFO_MAX_YEAR 2029
56 #define LL2FILETIME( ll, pft )\
57 (pft)->dwLowDateTime = (UINT)(ll); \
58 (pft)->dwHighDateTime = (UINT)((ll) >> 32);
59 #define FILETIME2LL( pft, ll) \
60 ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
62 /***********************************************************************
63 * SetLocalTime (KERNEL32.@)
65 * Set the local time using current time zone and daylight
66 * savings settings.
68 * RETURNS
69 * Success: TRUE. The time was set
70 * Failure: FALSE, if the time was invalid or caller does not have
71 * permission to change the time.
73 BOOL WINAPI SetLocalTime(
74 const SYSTEMTIME *systime) /* [in] The desired local time. */
76 FILETIME ft;
77 LARGE_INTEGER st, st2;
78 NTSTATUS status;
80 SystemTimeToFileTime( systime, &ft );
81 st.u.LowPart = ft.dwLowDateTime;
82 st.u.HighPart = ft.dwHighDateTime;
83 RtlLocalTimeToSystemTime( &st, &st2 );
85 if ((status = NtSetSystemTime(&st2, NULL)))
86 SetLastError( RtlNtStatusToDosError(status) );
87 return !status;
91 /***********************************************************************
92 * GetSystemTimeAdjustment (KERNEL32.@)
94 * Get the period between clock interrupts and the amount the clock
95 * is adjusted each interrupt so as to keep it in sync with an external source.
97 * RETURNS
98 * TRUE.
100 * BUGS
101 * Only the special case of disabled time adjustments is supported.
103 BOOL WINAPI GetSystemTimeAdjustment(
104 PDWORD lpTimeAdjustment, /* [out] The clock adjustment per interupt in 100's of nanoseconds. */
105 PDWORD lpTimeIncrement, /* [out] The time between clock interupts in 100's of nanoseconds. */
106 PBOOL lpTimeAdjustmentDisabled) /* [out] The clock synchonisation has been disabled. */
108 *lpTimeAdjustment = 0;
109 *lpTimeIncrement = 0;
110 *lpTimeAdjustmentDisabled = TRUE;
111 return TRUE;
115 /***********************************************************************
116 * SetSystemTime (KERNEL32.@)
118 * Set the system time in utc.
120 * RETURNS
121 * Success: TRUE. The time was set
122 * Failure: FALSE, if the time was invalid or caller does not have
123 * permission to change the time.
125 BOOL WINAPI SetSystemTime(
126 const SYSTEMTIME *systime) /* [in] The desired system time. */
128 FILETIME ft;
129 LARGE_INTEGER t;
130 NTSTATUS status;
132 SystemTimeToFileTime( systime, &ft );
133 t.u.LowPart = ft.dwLowDateTime;
134 t.u.HighPart = ft.dwHighDateTime;
135 if ((status = NtSetSystemTime(&t, NULL)))
136 SetLastError( RtlNtStatusToDosError(status) );
137 return !status;
140 /***********************************************************************
141 * SetSystemTimeAdjustment (KERNEL32.@)
143 * Enables or disables the timing adjustments to the system's clock.
145 * RETURNS
146 * Success: TRUE.
147 * Failure: FALSE.
149 BOOL WINAPI SetSystemTimeAdjustment(
150 DWORD dwTimeAdjustment,
151 BOOL bTimeAdjustmentDisabled)
153 /* Fake function for now... */
154 FIXME("(%08lx,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
155 return TRUE;
159 /***********************************************************************
160 * GetTimeZoneInformation (KERNEL32.@)
162 * Get information about the current local time zone.
164 * RETURNS
165 * Success: TIME_ZONE_ID_STANDARD. tzinfo contains the time zone info.
166 * Failure: TIME_ZONE_ID_INVALID.
167 * FIXME: return TIME_ZONE_ID_DAYLIGHT when daylight saving is on.
169 DWORD WINAPI GetTimeZoneInformation(
170 LPTIME_ZONE_INFORMATION tzinfo) /* [out] Destination for time zone information */
172 NTSTATUS status;
173 if ((status = RtlQueryTimeZoneInformation(tzinfo)))
174 SetLastError( RtlNtStatusToDosError(status) );
175 return TIME_ZONE_ID_STANDARD;
179 /***********************************************************************
180 * SetTimeZoneInformation (KERNEL32.@)
182 * Change the settings of the current local time zone.
184 * RETURNS
185 * Success: TRUE. The time zone was updated with the settings from tzinfo
186 * Failure: FALSE.
188 BOOL WINAPI SetTimeZoneInformation(
189 const LPTIME_ZONE_INFORMATION tzinfo) /* [in] The new time zone. */
191 NTSTATUS status;
192 if ((status = RtlSetTimeZoneInformation(tzinfo)))
193 SetLastError( RtlNtStatusToDosError(status) );
194 return !status;
198 /***********************************************************************
199 * _DayLightCompareDate
201 * Compares two dates without looking at the year
203 * RETURNS
205 * -1 if date < compareDate
206 * 0 if date == compareDate
207 * 1 if date > compareDate
208 * -2 if an error occures
211 static int _DayLightCompareDate(
212 const LPSYSTEMTIME date, /* [in] The local time to compare. */
213 const LPSYSTEMTIME compareDate) /* [in] The daylight saving begin
214 or end date */
216 int limit_day, dayinsecs;
218 if (date->wMonth < compareDate->wMonth)
219 return -1; /* We are in a month before the date limit. */
221 if (date->wMonth > compareDate->wMonth)
222 return 1; /* We are in a month after the date limit. */
224 if (compareDate->wDayOfWeek <= 6)
226 SYSTEMTIME tmp;
227 FILETIME tmp_ft;
229 /* compareDate->wDay is interpreted as number of the week in the month
230 * 5 means: the last week in the month */
231 int weekofmonth = compareDate->wDay;
233 /* calculate day of week for the first day in the month */
234 memcpy(&tmp, date, sizeof(SYSTEMTIME));
235 tmp.wDay = 1;
236 tmp.wDayOfWeek = -1;
238 if (weekofmonth == 5)
240 /* Go to the beginning of the next month. */
241 if (++tmp.wMonth > 12)
243 tmp.wMonth = 1;
244 ++tmp.wYear;
248 if (!SystemTimeToFileTime(&tmp, &tmp_ft))
249 return -2;
251 if (weekofmonth == 5)
253 LONGLONG t;
254 FILETIME2LL( &tmp_ft, t)
255 /* subtract one day */
256 t -= (LONGLONG) 24*60*60*10000000;
257 LL2FILETIME( t, &tmp_ft);
260 if (!FileTimeToSystemTime(&tmp_ft, &tmp))
261 return -2;
263 if (weekofmonth == 5)
265 /* calculate the last matching day of the week in this month */
266 int dif = tmp.wDayOfWeek - compareDate->wDayOfWeek;
267 if (dif < 0)
268 dif += 7;
270 limit_day = tmp.wDay - dif;
272 else
274 /* calculate the matching day of the week in the given week */
275 int dif = compareDate->wDayOfWeek - tmp.wDayOfWeek;
276 if (dif < 0)
277 dif += 7;
279 limit_day = tmp.wDay + 7*(weekofmonth-1) + dif;
282 else
284 limit_day = compareDate->wDay;
287 /* convert to seconds */
288 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 +
289 compareDate->wMinute ) * 60;
290 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 +
291 date->wMinute ) * 60 + date->wSecond;
292 /* and compare */
293 return dayinsecs < limit_day ? -1 :
294 dayinsecs > limit_day ? 1 :
295 0; /* date is equal to the date limit. */
299 /***********************************************************************
300 * _GetTimezoneBias
302 * Calculates the local time bias for a given time zone
304 * RETURNS
306 * Returns TRUE when the time zone bias was calculated.
309 static BOOL _GetTimezoneBias(
310 const LPTIME_ZONE_INFORMATION
311 lpTimeZoneInformation, /* [in] The time zone data. */
312 LPSYSTEMTIME lpSysOrLocalTime, /* [in] The system or local time. */
313 BOOL islocal, /* [in] it is local time */
314 LONG* pBias /* [out] The calulated bias in minutes */
317 int ret;
318 BOOL beforeStandardDate, afterDaylightDate;
319 BOOL daylightsaving = FALSE;
320 LONG bias = lpTimeZoneInformation->Bias;
321 FILETIME ft;
322 LONGLONG llTime = 0; /* initialized to prevent gcc complaining */
323 SYSTEMTIME stTemp;
324 LPSYSTEMTIME lpTime = lpSysOrLocalTime;
326 if (lpTimeZoneInformation->DaylightDate.wMonth != 0)
328 if (lpTimeZoneInformation->StandardDate.wMonth == 0 ||
329 lpTimeZoneInformation->StandardDate.wDay<1 ||
330 lpTimeZoneInformation->StandardDate.wDay>5 ||
331 lpTimeZoneInformation->DaylightDate.wDay<1 ||
332 lpTimeZoneInformation->DaylightDate.wDay>5)
334 SetLastError(ERROR_INVALID_PARAMETER);
335 return FALSE;
338 if (!islocal) {
339 if (!SystemTimeToFileTime(lpSysOrLocalTime, &ft)) return -2;
340 FILETIME2LL( &ft, llTime );
341 llTime -= ( lpTimeZoneInformation->Bias +
342 lpTimeZoneInformation->DaylightBias ) * (LONGLONG)600000000;
343 LL2FILETIME( llTime, &ft)
344 FileTimeToSystemTime(&ft, &stTemp);
345 lpTime = &stTemp;
348 /* check for daylight saving */
349 ret = _DayLightCompareDate(lpTime, &lpTimeZoneInformation->StandardDate);
350 if (ret == -2)
351 return FALSE;
353 beforeStandardDate = ret < 0;
355 if (!islocal) {
356 llTime -= ( lpTimeZoneInformation->StandardBias -
357 lpTimeZoneInformation->DaylightBias ) * (LONGLONG)600000000;
358 LL2FILETIME( llTime, &ft)
359 FileTimeToSystemTime(&ft, &stTemp);
360 lpTime = &stTemp;
363 ret = _DayLightCompareDate(lpTime, &lpTimeZoneInformation->DaylightDate);
364 if (ret == -2)
365 return FALSE;
367 afterDaylightDate = ret >= 0;
369 if( lpTimeZoneInformation->DaylightDate.wMonth < /* Northern */
370 lpTimeZoneInformation->StandardDate.wMonth ) { /* hemisphere */
371 if( beforeStandardDate && afterDaylightDate )
372 daylightsaving = TRUE;
373 } else /* Down south */
374 if( beforeStandardDate || afterDaylightDate )
375 daylightsaving = TRUE;
378 if (daylightsaving)
379 bias += lpTimeZoneInformation->DaylightBias;
380 else if (lpTimeZoneInformation->StandardDate.wMonth != 0)
381 bias += lpTimeZoneInformation->StandardBias;
383 *pBias = bias;
385 return TRUE;
389 /***********************************************************************
390 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
392 * Convert a utc system time to a local time in a given time zone.
394 * RETURNS
395 * Success: TRUE. lpLocalTime contains the converted time
396 * Failure: FALSE.
399 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
400 LPTIME_ZONE_INFORMATION
401 lpTimeZoneInformation, /* [in] The desired time zone. */
402 LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
403 LPSYSTEMTIME lpLocalTime) /* [out] The local time in the time zone. */
405 FILETIME ft;
406 LONG lBias;
407 LONGLONG llTime;
408 TIME_ZONE_INFORMATION tzinfo;
410 if (lpTimeZoneInformation != NULL)
412 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
414 else
416 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
417 return FALSE;
420 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
421 return FALSE;
422 FILETIME2LL( &ft, llTime)
423 if (!_GetTimezoneBias(&tzinfo, lpUniversalTime, FALSE, &lBias))
424 return FALSE;
425 /* convert minutes to 100-nanoseconds-ticks */
426 llTime -= (LONGLONG)lBias * 600000000;
427 LL2FILETIME( llTime, &ft)
429 return FileTimeToSystemTime(&ft, lpLocalTime);
433 /***********************************************************************
434 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
436 * Converts a local time to a time in utc.
438 * RETURNS
439 * Success: TRUE. lpUniversalTime contains the converted time
440 * Failure: FALSE.
442 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
443 LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
444 LPSYSTEMTIME lpLocalTime, /* [in] The local time. */
445 LPSYSTEMTIME lpUniversalTime) /* [out] The calculated utc time. */
447 FILETIME ft;
448 LONG lBias;
449 LONGLONG t;
450 TIME_ZONE_INFORMATION tzinfo;
452 if (lpTimeZoneInformation != NULL)
454 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
456 else
458 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
459 return FALSE;
462 if (!SystemTimeToFileTime(lpLocalTime, &ft))
463 return FALSE;
464 FILETIME2LL( &ft, t)
465 if (!_GetTimezoneBias(&tzinfo, lpLocalTime, TRUE, &lBias))
466 return FALSE;
467 /* convert minutes to 100-nanoseconds-ticks */
468 t += (LONGLONG)lBias * 600000000;
469 LL2FILETIME( t, &ft)
470 return FileTimeToSystemTime(&ft, lpUniversalTime);
474 /***********************************************************************
475 * GetSystemTimeAsFileTime (KERNEL32.@)
477 * Get the current time in utc format.
479 * RETURNS
480 * Nothing.
482 VOID WINAPI GetSystemTimeAsFileTime(
483 LPFILETIME time) /* [out] Destination for the current utc time */
485 LARGE_INTEGER t;
486 NtQuerySystemTime( &t );
487 time->dwLowDateTime = t.u.LowPart;
488 time->dwHighDateTime = t.u.HighPart;
492 /*********************************************************************
493 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
495 * Used by GetProcessTimes to convert clock_t into FILETIME.
497 * Differences to UnixTimeToFileTime:
498 * 1) Divided by CLK_TCK
499 * 2) Time is relative. There is no 'starting date', so there is
500 * no need for offset correction, like in UnixTimeToFileTime
502 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
504 ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
505 secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
506 filetime->dwLowDateTime = (DWORD)secs;
507 filetime->dwHighDateTime = (DWORD)(secs >> 32);
510 /*********************************************************************
511 * GetProcessTimes (KERNEL32.@)
513 * Get the user and kernel execution times of a process,
514 * along with the creation and exit times if known.
516 * RETURNS
517 * TRUE.
519 * NOTES
520 * olorin@fandra.org:
521 * Would be nice to subtract the cpu time used by Wine at startup.
522 * Also, there is a need to separate times used by different applications.
524 * BUGS
525 * lpCreationTime and lpExitTime are not initialised in the Wine implementation.
527 BOOL WINAPI GetProcessTimes(
528 HANDLE hprocess, /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
529 LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
530 LPFILETIME lpExitTime, /* [out] The exit time of the process if exited. */
531 LPFILETIME lpKernelTime, /* [out] The time spent in kernal routines in 100's of nanoseconds. */
532 LPFILETIME lpUserTime) /* [out] The time spent in user routines in 100's of nanoseconds. */
534 struct tms tms;
536 times(&tms);
537 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
538 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
539 return TRUE;
542 /*********************************************************************
543 * GetCalendarInfoA (KERNEL32.@)
546 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
547 LPSTR lpCalData, int cchData, LPDWORD lpValue)
549 int ret;
550 LPWSTR lpCalDataW = NULL;
552 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
553 lcid, Calendar, CalType, lpCalData, cchData, lpValue);
555 lcid = ConvertDefaultLocale(lcid);
557 if (NLS_IsUnicodeOnlyLcid(lcid))
559 SetLastError(ERROR_INVALID_PARAMETER);
560 return 0;
563 if (cchData &&
564 !(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchData*sizeof(WCHAR))))
565 return 0;
567 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchData, lpValue);
568 if(ret && lpCalDataW && lpCalData)
569 WideCharToMultiByte(CP_ACP, 0, lpCalDataW, cchData, lpCalData, cchData, NULL, NULL);
570 if(lpCalDataW)
571 HeapFree(GetProcessHeap(), 0, lpCalDataW);
573 return ret;
576 /*********************************************************************
577 * GetCalendarInfoW (KERNEL32.@)
579 * See GetCalendarInfoA.
581 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
582 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
584 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
585 Locale, Calendar, CalType, lpCalData, cchData, lpValue);
587 if (CalType & CAL_NOUSEROVERRIDE)
588 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
589 if (CalType & CAL_USE_CP_ACP)
590 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
592 if (CalType & CAL_RETURN_NUMBER) {
593 if (lpCalData != NULL)
594 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
595 if (cchData != 0)
596 WARN("cchData not 0 (%d) when it should!\n", cchData);
597 } else {
598 if (lpValue != NULL)
599 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
602 /* FIXME: No verification is made yet wrt Locale
603 * for the CALTYPES not requiring GetLocaleInfoA */
604 switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
605 case CAL_ICALINTVALUE:
606 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
607 return E_FAIL;
608 case CAL_SCALNAME:
609 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
610 return E_FAIL;
611 case CAL_IYEAROFFSETRANGE:
612 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
613 return E_FAIL;
614 case CAL_SERASTRING:
615 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
616 return E_FAIL;
617 case CAL_SSHORTDATE:
618 return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
619 case CAL_SLONGDATE:
620 return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
621 case CAL_SDAYNAME1:
622 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
623 case CAL_SDAYNAME2:
624 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
625 case CAL_SDAYNAME3:
626 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
627 case CAL_SDAYNAME4:
628 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
629 case CAL_SDAYNAME5:
630 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
631 case CAL_SDAYNAME6:
632 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
633 case CAL_SDAYNAME7:
634 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
635 case CAL_SABBREVDAYNAME1:
636 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
637 case CAL_SABBREVDAYNAME2:
638 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
639 case CAL_SABBREVDAYNAME3:
640 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
641 case CAL_SABBREVDAYNAME4:
642 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
643 case CAL_SABBREVDAYNAME5:
644 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
645 case CAL_SABBREVDAYNAME6:
646 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
647 case CAL_SABBREVDAYNAME7:
648 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
649 case CAL_SMONTHNAME1:
650 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
651 case CAL_SMONTHNAME2:
652 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
653 case CAL_SMONTHNAME3:
654 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
655 case CAL_SMONTHNAME4:
656 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
657 case CAL_SMONTHNAME5:
658 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
659 case CAL_SMONTHNAME6:
660 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
661 case CAL_SMONTHNAME7:
662 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
663 case CAL_SMONTHNAME8:
664 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
665 case CAL_SMONTHNAME9:
666 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
667 case CAL_SMONTHNAME10:
668 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
669 case CAL_SMONTHNAME11:
670 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
671 case CAL_SMONTHNAME12:
672 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
673 case CAL_SMONTHNAME13:
674 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
675 case CAL_SABBREVMONTHNAME1:
676 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
677 case CAL_SABBREVMONTHNAME2:
678 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
679 case CAL_SABBREVMONTHNAME3:
680 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
681 case CAL_SABBREVMONTHNAME4:
682 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
683 case CAL_SABBREVMONTHNAME5:
684 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
685 case CAL_SABBREVMONTHNAME6:
686 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
687 case CAL_SABBREVMONTHNAME7:
688 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
689 case CAL_SABBREVMONTHNAME8:
690 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
691 case CAL_SABBREVMONTHNAME9:
692 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
693 case CAL_SABBREVMONTHNAME10:
694 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
695 case CAL_SABBREVMONTHNAME11:
696 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
697 case CAL_SABBREVMONTHNAME12:
698 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
699 case CAL_SABBREVMONTHNAME13:
700 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
701 case CAL_SYEARMONTH:
702 return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
703 case CAL_ITWODIGITYEARMAX:
704 if (lpValue) *lpValue = CALINFO_MAX_YEAR;
705 break;
706 default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
707 return E_FAIL;
709 return 0;
712 /*********************************************************************
713 * SetCalendarInfoA (KERNEL32.@)
716 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
718 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
719 Locale, Calendar, CalType, debugstr_a(lpCalData));
720 return 0;
723 /*********************************************************************
724 * SetCalendarInfoW (KERNEL32.@)
726 * See SetCalendarInfoA.
728 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
730 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
731 Locale, Calendar, CalType, debugstr_w(lpCalData));
732 return 0;
735 /*********************************************************************
736 * LocalFileTimeToFileTime (KERNEL32.@)
738 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
740 NTSTATUS status;
741 LARGE_INTEGER local, utc;
743 local.u.LowPart = localft->dwLowDateTime;
744 local.u.HighPart = localft->dwHighDateTime;
745 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
747 utcft->dwLowDateTime = utc.u.LowPart;
748 utcft->dwHighDateTime = utc.u.HighPart;
750 else SetLastError( RtlNtStatusToDosError(status) );
752 return !status;
755 /*********************************************************************
756 * FileTimeToLocalFileTime (KERNEL32.@)
758 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
760 NTSTATUS status;
761 LARGE_INTEGER local, utc;
763 utc.u.LowPart = utcft->dwLowDateTime;
764 utc.u.HighPart = utcft->dwHighDateTime;
765 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
767 localft->dwLowDateTime = local.u.LowPart;
768 localft->dwHighDateTime = local.u.HighPart;
770 else SetLastError( RtlNtStatusToDosError(status) );
772 return !status;
775 /*********************************************************************
776 * FileTimeToSystemTime (KERNEL32.@)
778 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
780 TIME_FIELDS tf;
781 LARGE_INTEGER t;
783 t.u.LowPart = ft->dwLowDateTime;
784 t.u.HighPart = ft->dwHighDateTime;
785 RtlTimeToTimeFields(&t, &tf);
787 syst->wYear = tf.Year;
788 syst->wMonth = tf.Month;
789 syst->wDay = tf.Day;
790 syst->wHour = tf.Hour;
791 syst->wMinute = tf.Minute;
792 syst->wSecond = tf.Second;
793 syst->wMilliseconds = tf.Milliseconds;
794 syst->wDayOfWeek = tf.Weekday;
795 return TRUE;
798 /*********************************************************************
799 * SystemTimeToFileTime (KERNEL32.@)
801 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
803 TIME_FIELDS tf;
804 LARGE_INTEGER t;
806 tf.Year = syst->wYear;
807 tf.Month = syst->wMonth;
808 tf.Day = syst->wDay;
809 tf.Hour = syst->wHour;
810 tf.Minute = syst->wMinute;
811 tf.Second = syst->wSecond;
812 tf.Milliseconds = syst->wMilliseconds;
814 RtlTimeFieldsToTime(&tf, &t);
815 ft->dwLowDateTime = t.u.LowPart;
816 ft->dwHighDateTime = t.u.HighPart;
817 return TRUE;
820 /*********************************************************************
821 * CompareFileTime (KERNEL32.@)
823 * Compare two FILETIME's to each other.
825 * PARAMS
826 * x [I] First time
827 * y [I] time to compare to x
829 * RETURNS
830 * -1, 0, or 1 indicating that x is less than, equal to, or greater
831 * than y respectively.
833 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
835 if (!x || !y) return -1;
837 if (x->dwHighDateTime > y->dwHighDateTime)
838 return 1;
839 if (x->dwHighDateTime < y->dwHighDateTime)
840 return -1;
841 if (x->dwLowDateTime > y->dwLowDateTime)
842 return 1;
843 if (x->dwLowDateTime < y->dwLowDateTime)
844 return -1;
845 return 0;
848 /*********************************************************************
849 * GetLocalTime (KERNEL32.@)
851 * Get the current local time.
853 * RETURNS
854 * Nothing.
856 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
858 FILETIME lft;
859 LARGE_INTEGER ft, ft2;
861 NtQuerySystemTime(&ft);
862 RtlSystemTimeToLocalTime(&ft, &ft2);
863 lft.dwLowDateTime = ft2.u.LowPart;
864 lft.dwHighDateTime = ft2.u.HighPart;
865 FileTimeToSystemTime(&lft, systime);
868 /*********************************************************************
869 * GetSystemTime (KERNEL32.@)
871 * Get the current system time.
873 * RETURNS
874 * Nothing.
876 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
878 FILETIME ft;
879 LARGE_INTEGER t;
881 NtQuerySystemTime(&t);
882 ft.dwLowDateTime = t.u.LowPart;
883 ft.dwHighDateTime = t.u.HighPart;
884 FileTimeToSystemTime(&ft, systime);
887 /*********************************************************************
888 * GetDaylightFlag (KERNEL32.@)
890 * returns TRUE if daylight saving time is in operation
892 * Note: this function is called from the Win98's control applet
893 * timedate.cpl
895 BOOL WINAPI GetDaylightFlag(void)
897 time_t t = time(NULL);
898 struct tm *ptm = localtime( &t);
899 return ptm->tm_isdst > 0;
903 /***********************************************************************
904 * DosDateTimeToFileTime (KERNEL32.@)
906 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
908 struct tm newtm;
909 #ifndef HAVE_TIMEGM
910 struct tm *gtm;
911 time_t time1, time2;
912 #endif
914 newtm.tm_sec = (fattime & 0x1f) * 2;
915 newtm.tm_min = (fattime >> 5) & 0x3f;
916 newtm.tm_hour = (fattime >> 11);
917 newtm.tm_mday = (fatdate & 0x1f);
918 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
919 newtm.tm_year = (fatdate >> 9) + 80;
920 #ifdef HAVE_TIMEGM
921 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
922 #else
923 time1 = mktime(&newtm);
924 gtm = gmtime(&time1);
925 time2 = mktime(gtm);
926 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
927 #endif
928 return TRUE;
932 /***********************************************************************
933 * FileTimeToDosDateTime (KERNEL32.@)
935 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
936 LPWORD fattime )
938 LARGE_INTEGER li;
939 ULONG t;
940 time_t unixtime;
941 struct tm* tm;
943 li.u.LowPart = ft->dwLowDateTime;
944 li.u.HighPart = ft->dwHighDateTime;
945 RtlTimeToSecondsSince1970( &li, &t );
946 unixtime = t;
947 tm = gmtime( &unixtime );
948 if (fattime)
949 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
950 if (fatdate)
951 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
952 + tm->tm_mday;
953 return TRUE;