ReactOS 0.4.15-dev-7906-g1b85a5f
timezone.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: LGPL, See LGPL.txt in the top level directory
3 * PROJECT: ReactOS CRT library
4 * FILE: lib/sdk/crt/time/timezone.c
5 * PURPOSE: Implementation of time zone functions
6 * PROGRAMERS: Timo Kreuzer
7 */
8#include "precomp.h"
9
10char _tz_is_set = 0;
11
12/* buffers must hold 64 characters! */
13static char tz_name[64] = "PST";
14static char tz_dst_name[64] = "PDT";
15
16long dst_begin = 0;
17long dst_end = 0;
18
19/******************************************************************************
20 * \var _tzname
21 */
22char * _tzname[2] = {
23 tz_name,
25};
26
27/******************************************************************************
28 * \var _daylight
29 */
30int _daylight = 0;
31
32/******************************************************************************
33 * \name __p__daylight
34 * \brief Returns a pointer to the _daylight variable;
35 */
36void *
38{
39 return &_daylight;
40}
41
42/******************************************************************************
43 * \var _timezone
44 * \brief
45 */
46long _timezone = 28800;
47
48/******************************************************************************
49 * \name __p__timezone
50 * \brief Returns a pointer to the _timezone variable;
51 */
52long *
54{
55 return &_timezone;
56}
57
58/******************************************************************************
59 * \var _dstbias
60 * \brief
61 */
62long _dstbias = 0;
63
64/******************************************************************************
65 * \name __p__dstbias
66 * \brief Returns a pointer to the _dstbias variable;
67 */
68long *
70{
71 return &_dstbias;
72}
73
74/******************************************************************************
75 * \name __p__tzname
76 * \brief Returns a pointer to the _tzname buffer;
77 */
78char **
80{
81 return _tzname;
82}
83
84/******************************************************************************
85 * \name _tzset
86 * \brief Initializes the variables _daylight, _timezone, and _tzname from the
87 * "TZ" environment variable if available or else by calling
88 * GetTimeZoneInformation.
89 * \sa http://msdn.microsoft.com/en-us/library/90s5c885.aspx
90 */
91void
92_tzset(void)
93{
94 const char * str;
95
96 if (_tz_is_set)
97 {
98 return;
99 }
100
101 /* Try to read the timezone from environment */
102 str = getenv("TZ");
103 if (str && str[0] != 0)
104 {
105 long hour = 0, min = 0, sec = 0;
106 size_t len = strnlen(str, 16);
107 int sign = 1;
108
109 dst_begin = 0;
110
111 for (;;)
112 {
113 /* Copy timezone name */
114 strncpy(tz_name, str, 3);
115 str += 3;
116 len -= 3;
117
118 if (len < 1) break;
119
120 if (*str == '+' || *str == '-')
121 {
122 sign = *str == '-' ? -1 : 1;
123 str++;
124 len--;
125 }
126
127 if (len < 1) break;
128
129 hour = atol(str);
130
131 while (*str != 0 && *str != ':') str++;
132 if (*str == 0) break;
133
134 min = atol(++str);
135
136 while (*str != 0 && *str != ':') str++;
137 if (*str == 0) break;
138
139 sec = atol(++str);
140
141 while (*str != 0 && *str <= '9') str++;
142 if (*str == 0) break;
143
144 /* Copy DST name */
146
147 // FIXME: set dst_begin etc
148
149 /* We are finished */
150 break;
151 }
152
153 _timezone = sign * (((hour * 60) + min) * 60 + sec);
154
155 }
156 else
157 {
159 DWORD ret;
160
163 {
164 return;
165 }
166
168 0,
169 tzi.StandardName,
170 -1,
171 tz_name,
172 sizeof(tz_name),
173 NULL,
174 NULL);
175
177 0,
178 tzi.DaylightName,
179 -1,
181 sizeof(tz_dst_name),
182 NULL,
183 NULL);
184
185 _timezone = tzi.Bias * 60;
186
187 if (tzi.DaylightDate.wMonth)
188 {
189 struct tm _tm;
190
191 _daylight = 1;
192 _dstbias = (tzi.DaylightBias - tzi.StandardBias) * 60;
193 _tm.tm_year = 70;
194 _tm.tm_mon = tzi.DaylightDate.wMonth - 1;
195 _tm.tm_mday = tzi.DaylightDate.wDay;
196 _tm.tm_hour = tzi.DaylightDate.wHour;
197 _tm.tm_min = tzi.DaylightDate.wMinute;
198 _tm.tm_sec = tzi.DaylightDate.wSecond;
199 dst_begin = (long)_mkgmtime(&_tm);
200 _tm.tm_mon = tzi.StandardDate.wMonth - 1;
201 _tm.tm_mday = tzi.StandardDate.wDay;
202 _tm.tm_hour = tzi.StandardDate.wHour;
203 _tm.tm_min = tzi.StandardDate.wMinute;
204 _tm.tm_sec = tzi.StandardDate.wSecond;
205 dst_end = (long)_mkgmtime(&_tm);
206 }
207 else
208 {
209 _daylight = 0;
210 _dstbias = 0;
211 }
212
213 }
214 _tz_is_set = 1;
215}
216
217/*********************************************************************
218 * _get_tzname (MSVCRT.@)
219 */
220int CDECL _get_tzname(size_t *ret, char *buf, size_t bufsize, int index)
221{
222 char *str_timezone;
223
224 switch (index)
225 {
226 case 0:
227 str_timezone = tz_name;
228 break;
229
230 case 1:
231 str_timezone = tz_dst_name;
232 break;
233
234 default:
235 *_errno() = EINVAL;
236 return EINVAL;
237 }
238
239 if (!ret || (!buf && (bufsize > 0)) || (buf && !bufsize))
240 {
241 *_errno() = EINVAL;
242 return EINVAL;
243 }
244
245 *ret = strlen(str_timezone) + 1;
246 if(!buf && !bufsize)
247 return 0;
248
249 strncpy(buf, str_timezone, bufsize);
250 return 0;
251}
#define EINVAL
Definition: acclib.h:90
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
#define NULL
Definition: types.h:112
#define CDECL
Definition: compat.h:29
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
Definition: timezone.c:262
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint index
Definition: glext.h:6031
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_ long __cdecl atol(_In_z_ const char *_Str)
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
#define sign(x)
Definition: mapdesc.cc:613
#define min(a, b)
Definition: monoChain.cc:55
#define long
Definition: qsort.c:33
const WCHAR * str
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:19
_CRTIMP time_t __cdecl _mkgmtime(struct tm *_Tm)
Definition: time.h:419
long * __p__dstbias(void)
Definition: timezone.c:69
void * __p__daylight(void)
Definition: timezone.c:37
char ** __p__tzname(void)
Definition: timezone.c:79
int _daylight
Definition: timezone.c:30
void _tzset(void)
Definition: timezone.c:92
char * _tzname[2]
Definition: timezone.c:22
long * __p__timezone(void)
Definition: timezone.c:53
long dst_begin
Definition: timezone.c:16
long _dstbias
Definition: timezone.c:62
static char tz_dst_name[64]
Definition: timezone.c:14
long _timezone
Definition: timezone.c:46
long dst_end
Definition: timezone.c:17
static char tz_name[64]
Definition: timezone.c:13
int CDECL _get_tzname(size_t *ret, char *buf, size_t bufsize, int index)
Definition: timezone.c:220
char _tz_is_set
Definition: timezone.c:10
WORD wMonth
Definition: winbase.h:906
WORD wHour
Definition: winbase.h:909
WORD wSecond
Definition: winbase.h:911
WORD wMinute
Definition: winbase.h:910
WORD wDay
Definition: winbase.h:908
SYSTEMTIME DaylightDate
Definition: winbase.h:1211
WCHAR DaylightName[32]
Definition: winbase.h:1210
WCHAR StandardName[32]
Definition: winbase.h:1207
SYSTEMTIME StandardDate
Definition: winbase.h:1208
Definition: time.h:68
int tm_mon
Definition: time.h:73
int tm_year
Definition: time.h:74
int tm_hour
Definition: time.h:71
int tm_sec
Definition: time.h:69
int tm_mday
Definition: time.h:72
int tm_min
Definition: time.h:70
int ret
#define TIME_ZONE_ID_INVALID
Definition: winbase.h:286