Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbeyj@wpi.edu>
[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 "file.h"
12 #include "winerror.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(win32)
16
17 /***********************************************************************
18  *              GetLocalTime            (KERNEL32.228)
19  */
20 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
21 {
22     time_t local_time;
23     struct tm *local_tm;
24     struct timeval tv;
25
26     gettimeofday(&tv, NULL);
27     local_time = tv.tv_sec;
28     local_tm = localtime(&local_time);
29
30     systime->wYear = local_tm->tm_year + 1900;
31     systime->wMonth = local_tm->tm_mon + 1;
32     systime->wDayOfWeek = local_tm->tm_wday;
33     systime->wDay = local_tm->tm_mday;
34     systime->wHour = local_tm->tm_hour;
35     systime->wMinute = local_tm->tm_min;
36     systime->wSecond = local_tm->tm_sec;
37     systime->wMilliseconds = (tv.tv_usec / 1000) % 1000;
38 }
39
40 /***********************************************************************
41  *              GetSystemTime            (KERNEL32.285)
42  */
43 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
44 {
45     time_t local_time;
46     struct tm *local_tm;
47     struct timeval tv;
48
49     gettimeofday(&tv, NULL);
50     local_time = tv.tv_sec;
51     local_tm = gmtime(&local_time);
52
53     systime->wYear = local_tm->tm_year + 1900;
54     systime->wMonth = local_tm->tm_mon + 1;
55     systime->wDayOfWeek = local_tm->tm_wday;
56     systime->wDay = local_tm->tm_mday;
57     systime->wHour = local_tm->tm_hour;
58     systime->wMinute = local_tm->tm_min;
59     systime->wSecond = local_tm->tm_sec;
60     systime->wMilliseconds = (tv.tv_usec / 1000) % 1000;
61 }
62
63
64 /***********************************************************************
65  *              SetSystemTime            (KERNEL32.507)
66  */
67 BOOL WINAPI SetSystemTime(const SYSTEMTIME *systime)
68 {
69     struct timeval tv;
70     struct timezone tz;
71     struct tm t;
72     time_t sec;
73
74     /* call gettimeofday to get the current timezone */
75     gettimeofday(&tv, &tz);
76
77     /* get the number of seconds */
78     t.tm_sec = systime->wSecond;
79     t.tm_min = systime->wMinute;
80     t.tm_hour = systime->wHour;
81     t.tm_mday = systime->wDay;
82     t.tm_mon = systime->wMonth;
83     t.tm_year = systime->wYear;
84     sec = mktime (&t);
85
86     /* set the new time */
87     tv.tv_sec = sec;
88     tv.tv_usec = systime->wMilliseconds * 1000;
89     if (settimeofday(&tv, &tz))
90     {
91         return FALSE;
92     }
93     else
94     {
95         return TRUE;
96     }
97 }
98
99
100 /***********************************************************************
101  *              GetTimeZoneInformation  (KERNEL32.302)
102  */
103 DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
104 {
105     time_t gmt, lt;
106
107     memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
108
109     gmt = time(NULL);
110     lt = mktime(gmtime(&gmt));
111     tzinfo->Bias = (lt - gmt) / 60;
112     tzinfo->StandardBias = 0;
113     tzinfo->DaylightBias = -60;
114
115     return TIME_ZONE_ID_UNKNOWN;
116 }
117
118
119 /***********************************************************************
120  *              SetTimeZoneInformation  (KERNEL32.515)
121  */
122 BOOL WINAPI SetTimeZoneInformation(const LPTIME_ZONE_INFORMATION tzinfo)
123 {
124     struct timezone tz;
125
126     tz.tz_minuteswest = tzinfo->Bias;
127 #ifdef DST_NONE
128     tz.tz_dsttime = DST_NONE;
129 #else
130     tz.tz_dsttime = 0;
131 #endif
132     return !settimeofday(NULL, &tz);
133 }
134
135
136 /***********************************************************************
137  *              GetSystemTimeAsFileTime  (KERNEL32)
138  */
139 VOID WINAPI GetSystemTimeAsFileTime(LPFILETIME systemtimeAsfiletime) {
140         DOSFS_UnixTimeToFileTime(time(NULL),systemtimeAsfiletime,0);
141 }
142
143 /***********************************************************************
144  *              SystemTimeToTzSpecificLocalTime32  (KERNEL32.683)
145  */
146 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
147   LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
148   LPSYSTEMTIME lpUniversalTime,
149   LPSYSTEMTIME lpLocalTime) {
150
151   FIXME(":stub\n"); 
152   SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 
153        return FALSE;
154 }