Release 970824
[wine] / win32 / 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 <time.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include "windows.h"
12 #include "winerror.h"
13 #include "stddebug.h"
14 #include "debug.h"
15
16 /***********************************************************************
17  *              GetLocalTime            (KERNEL32.228)
18  */
19 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
20 {
21     time_t local_time;
22     struct tm *local_tm;
23     struct timeval tv;
24
25     gettimeofday(&tv, NULL);
26     local_time = tv.tv_sec;
27     local_tm = localtime(&local_time);
28
29     systime->wYear = local_tm->tm_year + 1900;
30     systime->wMonth = local_tm->tm_mon + 1;
31     systime->wDayOfWeek = local_tm->tm_wday;
32     systime->wDay = local_tm->tm_mday;
33     systime->wHour = local_tm->tm_hour;
34     systime->wMinute = local_tm->tm_min;
35     systime->wSecond = local_tm->tm_sec;
36     systime->wMilliseconds = (tv.tv_usec / 1000) % 1000;
37 }
38
39 /***********************************************************************
40  *              GetSystemTime            (KERNEL32.285)
41  */
42 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
43 {
44     time_t local_time;
45     struct tm *local_tm;
46     struct timeval tv;
47
48     gettimeofday(&tv, NULL);
49     local_time = tv.tv_sec;
50     local_tm = gmtime(&local_time);
51
52     systime->wYear = local_tm->tm_year + 1900;
53     systime->wMonth = local_tm->tm_mon + 1;
54     systime->wDayOfWeek = local_tm->tm_wday;
55     systime->wDay = local_tm->tm_mday;
56     systime->wHour = local_tm->tm_hour;
57     systime->wMinute = local_tm->tm_min;
58     systime->wSecond = local_tm->tm_sec;
59     systime->wMilliseconds = (tv.tv_usec / 1000) % 1000;
60 }
61
62
63 /***********************************************************************
64  *              SetSystemTime            (KERNEL32.507)
65  */
66 BOOL32 WINAPI SetSystemTime(const SYSTEMTIME *systime)
67 {
68     struct timeval tv;
69     struct timezone tz;
70     struct tm t;
71     time_t sec;
72
73     /* call gettimeofday to get the current timezone */
74     gettimeofday(&tv, &tz);
75
76     /* get the number of seconds */
77     t.tm_sec = systime->wSecond;
78     t.tm_min = systime->wMinute;
79     t.tm_hour = systime->wHour;
80     t.tm_mday = systime->wDay;
81     t.tm_mon = systime->wMonth;
82     t.tm_year = systime->wYear;
83     sec = mktime (&t);
84
85     /* set the new time */
86     tv.tv_sec = sec;
87     tv.tv_usec = systime->wMilliseconds * 1000;
88     if (settimeofday(&tv, &tz))
89     {
90         return FALSE;
91     }
92     else
93     {
94         return TRUE;
95     }
96 }
97
98
99 /***********************************************************************
100  *              GetTimeZoneInformation  (KERNEL32.302)
101  */
102 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
103 {
104     time_t gmt, lt;
105
106     memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
107
108     gmt = time(NULL);
109     lt = mktime(localtime(&gmt));
110     tzinfo->Bias = (gmt - lt) / 60;
111     tzinfo->StandardBias = 0;
112     tzinfo->DaylightBias = -60;
113
114     return TIME_ZONE_ID_UNKNOWN;
115 }
116
117
118 /***********************************************************************
119  *              SetTimeZoneInformation  (KERNEL32.515)
120  */
121 BOOL32 WINAPI SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo)
122 {
123     struct timezone tz;
124
125     tz.tz_minuteswest = tzinfo->Bias;
126 #ifdef DST_NONE
127     tz.tz_dsttime = DST_NONE;
128 #else
129     tz.tz_dsttime = 0;
130 #endif
131     return !settimeofday(NULL, &tz);
132 }
133
134
135 /***********************************************************************
136  *              Sleep  (KERNEL32.523)
137  */
138 VOID WINAPI Sleep(DWORD cMilliseconds)
139 {
140     if(cMilliseconds == INFINITE32)
141         while(1) sleep(1000); /* Spin forever */
142     usleep(cMilliseconds*1000);
143 }