31
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include <sys/cdefs.h>
|
|
20 |
#ifndef lint
|
|
21 |
#ifndef NOID
|
|
22 |
static char elsieid[] __unused = "@(#)localtime.c 7.78";
|
|
23 |
#endif /* !defined NOID */
|
|
24 |
#endif /* !defined lint */
|
|
25 |
__FBSDID("$FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.40 2004/08/24 00:15:37 peter Exp $");
|
|
26 |
|
|
27 |
/*
|
|
28 |
** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
|
|
29 |
** POSIX-style TZ environment variable handling from Guy Harris
|
|
30 |
** (guy@auspex.com).
|
|
31 |
*/
|
|
32 |
#include <time.h>
|
|
33 |
|
|
34 |
/*LINTLIBRARY*/
|
|
35 |
#include <unistd.h>
|
|
36 |
#include "namespace.h"
|
|
37 |
#include <sys/types.h>
|
|
38 |
#include <pthread.h>
|
|
39 |
#include "private.h"
|
|
40 |
#include "un-namespace.h"
|
|
41 |
#include "tzfile.h"
|
|
42 |
#include <fcntl.h>
|
|
43 |
#include <sys/stat.h>
|
|
44 |
#include "libc_private.h"
|
|
45 |
#include "common_def.h"
|
|
46 |
#include <wchar.h>
|
|
47 |
#include "timefuncs.h"
|
|
48 |
|
|
49 |
#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
|
|
50 |
#include "libc_wsd_defs.h"
|
|
51 |
#endif
|
|
52 |
|
|
53 |
#define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
|
|
54 |
#define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
|
|
55 |
|
|
56 |
/*
|
|
57 |
** SunOS 4.1.1 headers lack O_BINARY.
|
|
58 |
*/
|
|
59 |
|
|
60 |
#ifdef O_BINARY
|
|
61 |
#define OPEN_MODE (O_RDONLY | O_BINARY)
|
|
62 |
#endif /* defined O_BINARY */
|
|
63 |
#ifndef O_BINARY
|
|
64 |
#define OPEN_MODE O_RDONLY
|
|
65 |
#endif /* !defined O_BINARY */
|
|
66 |
|
|
67 |
#ifndef WILDABBR
|
|
68 |
/*
|
|
69 |
** Someone might make incorrect use of a time zone abbreviation:
|
|
70 |
** 1. They might reference tzname[0] before calling tzset (explicitly
|
|
71 |
** or implicitly).
|
|
72 |
** 2. They might reference tzname[1] before calling tzset (explicitly
|
|
73 |
** or implicitly).
|
|
74 |
** 3. They might reference tzname[1] after setting to a time zone
|
|
75 |
** in which Daylight Saving Time is never observed.
|
|
76 |
** 4. They might reference tzname[0] after setting to a time zone
|
|
77 |
** in which Standard Time is never observed.
|
|
78 |
** 5. They might reference tm.TM_ZONE after calling offtime.
|
|
79 |
** What's best to do in the above cases is open to debate;
|
|
80 |
** for now, we just set things up so that in any of the five cases
|
|
81 |
** WILDABBR is used. Another possibility: initialize tzname[0] to the
|
|
82 |
** string "tzname[0] used before set", and similarly for the other cases.
|
|
83 |
** And another: initialize tzname[0] to "ERA", with an explanation in the
|
|
84 |
** manual page of what this "time zone abbreviation" means (doing this so
|
|
85 |
** that tzname[0] has the "normal" length of three characters).
|
|
86 |
*/
|
|
87 |
#define WILDABBR " "
|
|
88 |
#endif /* !defined WILDABBR */
|
|
89 |
|
|
90 |
#ifdef __SYMBIAN32__
|
|
91 |
extern int GetStdOffset(void);
|
|
92 |
extern int GetIsDst(void);
|
|
93 |
#endif //__SYMBIAN32__
|
|
94 |
|
|
95 |
#ifdef __SYMBIAN32__
|
|
96 |
#ifndef EMULATOR
|
|
97 |
|
|
98 |
static char wildabbr[] = "WILDABBR";
|
|
99 |
#else //EMULATOR
|
|
100 |
|
|
101 |
GET_STATIC_ARRAY_FROM_TLS(wildabbr, char)
|
|
102 |
#define wildabbr (GET_WSD_VAR_NAME(wildabbr, s)())
|
|
103 |
#endif //EMULATOR
|
|
104 |
#else
|
|
105 |
static char wildabbr[] = "WILDABBR";
|
|
106 |
#endif //__SYMBIAN32__
|
|
107 |
|
|
108 |
/*
|
|
109 |
* In June 2004 it was decided UTC was a more appropriate default time
|
|
110 |
* zone than GMT.
|
|
111 |
*/
|
|
112 |
|
|
113 |
#ifdef __SYMBIAN32__
|
|
114 |
#if !(defined(EMULATOR))
|
|
115 |
static const char gmt[] = "UTC";
|
|
116 |
#endif //EMULATOR
|
|
117 |
#else
|
|
118 |
static const char gmt[] = "UTC";
|
|
119 |
#endif //__SYMBIAN32__
|
|
120 |
|
|
121 |
/*
|
|
122 |
** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
|
|
123 |
** We default to US rules as of 1999-08-17.
|
|
124 |
** POSIX 1003.1 section 8.1.1 says that the default DST rules are
|
|
125 |
** implementation dependent; for historical reasons, US rules are a
|
|
126 |
** common default.
|
|
127 |
*/
|
|
128 |
#ifndef TZDEFRULESTRING
|
|
129 |
#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
|
|
130 |
#endif /* !defined TZDEFDST */
|
|
131 |
|
|
132 |
#ifdef __SYMBIAN32__
|
|
133 |
#ifndef EMULATOR
|
|
134 |
struct ttinfo { /* time type information */
|
|
135 |
long tt_gmtoff; /* UTC offset in seconds */
|
|
136 |
int tt_isdst; /* used to set tm_isdst */
|
|
137 |
int tt_abbrind; /* abbreviation list index */
|
|
138 |
int tt_ttisstd; /* TRUE if transition is std time */
|
|
139 |
int tt_ttisgmt; /* TRUE if transition is UTC */
|
|
140 |
};
|
|
141 |
|
|
142 |
struct lsinfo { /* leap second information */
|
|
143 |
time_t ls_trans; /* transition time */
|
|
144 |
long ls_corr; /* correction to apply */
|
|
145 |
};
|
|
146 |
#endif //EMULATOR
|
|
147 |
#else
|
|
148 |
struct ttinfo { /* time type information */
|
|
149 |
long tt_gmtoff; /* UTC offset in seconds */
|
|
150 |
int tt_isdst; /* used to set tm_isdst */
|
|
151 |
int tt_abbrind; /* abbreviation list index */
|
|
152 |
int tt_ttisstd; /* TRUE if transition is std time */
|
|
153 |
int tt_ttisgmt; /* TRUE if transition is UTC */
|
|
154 |
};
|
|
155 |
|
|
156 |
struct lsinfo { /* leap second information */
|
|
157 |
time_t ls_trans; /* transition time */
|
|
158 |
long ls_corr; /* correction to apply */
|
|
159 |
};
|
|
160 |
#endif //__SYMBIAN32__
|
|
161 |
|
|
162 |
#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
|
|
163 |
|
|
164 |
#ifdef TZNAME_MAX
|
|
165 |
#define MY_TZNAME_MAX TZNAME_MAX
|
|
166 |
#endif /* defined TZNAME_MAX */
|
|
167 |
#ifndef TZNAME_MAX
|
|
168 |
#define MY_TZNAME_MAX 255
|
|
169 |
#endif /* !defined TZNAME_MAX */
|
|
170 |
|
|
171 |
#ifdef __SYMBIAN32__
|
|
172 |
#ifndef EMULATOR
|
|
173 |
struct state {
|
|
174 |
int leapcnt;
|
|
175 |
int timecnt;
|
|
176 |
int typecnt;
|
|
177 |
int charcnt;
|
|
178 |
time_t ats[TZ_MAX_TIMES];
|
|
179 |
unsigned char types[TZ_MAX_TIMES];
|
|
180 |
struct ttinfo ttis[TZ_MAX_TYPES];
|
|
181 |
char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
|
|
182 |
(2 * (MY_TZNAME_MAX + 1)))];
|
|
183 |
struct lsinfo lsis[TZ_MAX_LEAPS];
|
|
184 |
};
|
|
185 |
#endif //EMULATOR
|
|
186 |
#else
|
|
187 |
struct state {
|
|
188 |
int leapcnt;
|
|
189 |
int timecnt;
|
|
190 |
int typecnt;
|
|
191 |
int charcnt;
|
|
192 |
time_t ats[TZ_MAX_TIMES];
|
|
193 |
unsigned char types[TZ_MAX_TIMES];
|
|
194 |
struct ttinfo ttis[TZ_MAX_TYPES];
|
|
195 |
char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
|
|
196 |
(2 * (MY_TZNAME_MAX + 1)))];
|
|
197 |
struct lsinfo lsis[TZ_MAX_LEAPS];
|
|
198 |
};
|
|
199 |
#endif //__SYMBIAN32__
|
|
200 |
|
|
201 |
struct rule {
|
|
202 |
int r_type; /* type of rule--see below */
|
|
203 |
int r_day; /* day number of rule */
|
|
204 |
int r_week; /* week number of rule */
|
|
205 |
int r_mon; /* month number of rule */
|
|
206 |
long r_time; /* transition time of rule */
|
|
207 |
};
|
|
208 |
|
|
209 |
#define JULIAN_DAY 0 /* Jn - Julian day */
|
|
210 |
#define DAY_OF_YEAR 1 /* n - day of year */
|
|
211 |
#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
|
|
212 |
|
|
213 |
/*
|
|
214 |
** Prototypes for static functions.
|
|
215 |
*/
|
|
216 |
|
|
217 |
#ifndef __SYMBIAN32__
|
|
218 |
static long detzcode(const char * codep);
|
|
219 |
#endif //__SYMBIAN32__
|
|
220 |
static const char * getzname(const char * strp);
|
|
221 |
static const char * getnum(const char * strp, int * nump, int min,
|
|
222 |
int max);
|
|
223 |
static const char * getsecs(const char * strp, long * secsp);
|
|
224 |
static const char * getoffset(const char * strp, long * offsetp);
|
|
225 |
static const char * getrule(const char * strp, struct rule * rulep);
|
|
226 |
static void gmtload(struct state * sp);
|
|
227 |
static void gmtsub(const time_t * timep, long offset,
|
|
228 |
struct tm * tmp);
|
|
229 |
static void localsub(const time_t * timep, long offset,
|
|
230 |
struct tm * tmp);
|
|
231 |
static int increment_overflow(int * number, int delta);
|
|
232 |
static int normalize_overflow(int * tensptr, int * unitsptr,
|
|
233 |
int base);
|
|
234 |
static void settzname(void);
|
|
235 |
static time_t time1(struct tm * tmp,
|
|
236 |
void(*funcp) (const time_t *,
|
|
237 |
long, struct tm *),
|
|
238 |
long offset);
|
|
239 |
static time_t time2(struct tm *tmp,
|
|
240 |
void(*funcp) (const time_t *,
|
|
241 |
long, struct tm*),
|
|
242 |
long offset, int * okayp);
|
|
243 |
static time_t time2sub(struct tm *tmp,
|
|
244 |
void(*funcp) (const time_t *,
|
|
245 |
long, struct tm*),
|
|
246 |
long offset, int * okayp, int do_norm_secs);
|
|
247 |
static void timesub(const time_t * timep, long offset,
|
|
248 |
const struct state * sp, struct tm * tmp);
|
|
249 |
static int tmcomp(const struct tm * atmp,
|
|
250 |
const struct tm * btmp);
|
|
251 |
static time_t transtime(time_t janfirst, int year,
|
|
252 |
const struct rule * rulep, long offset);
|
|
253 |
static int tzload(const char * name, struct state * sp);
|
|
254 |
static int tzparse(const char * name, struct state * sp,
|
|
255 |
int lastditch);
|
|
256 |
|
|
257 |
#ifdef ALL_STATE
|
|
258 |
|
|
259 |
#ifdef __SYMBIAN32__
|
|
260 |
#ifndef EMULATOR
|
|
261 |
|
|
262 |
static struct state * lclptr;
|
|
263 |
static struct state * gmtptr;
|
|
264 |
#else //EMULATOR
|
|
265 |
|
|
266 |
GET_STATIC_VAR_FROM_TLS(lclptr, state *)
|
|
267 |
GET_STATIC_VAR_FROM_TLS(gmtptr, state *)
|
|
268 |
|
|
269 |
#define lclptr (*GET_WSD_VAR_NAME(lclptr, s)())
|
|
270 |
#define gmtptr (*GET_WSD_VAR_NAME(gmtptr, s)())
|
|
271 |
#endif //EMULATOR
|
|
272 |
#else //__SYMBIAN32__
|
|
273 |
static struct state * lclptr;
|
|
274 |
static struct state * gmtptr;
|
|
275 |
#endif //__SYMBIAN32__
|
|
276 |
|
|
277 |
#endif /* defined ALL_STATE */
|
|
278 |
|
|
279 |
#ifndef ALL_STATE
|
|
280 |
|
|
281 |
#ifdef __SYMBIAN32__
|
|
282 |
#ifndef EMULATOR
|
|
283 |
|
|
284 |
static struct state lclmem;
|
|
285 |
static struct state gmtmem;
|
|
286 |
#else //EMULATOR
|
|
287 |
|
|
288 |
GET_STATIC_VAR_FROM_TLS(lclmem, struct state)
|
|
289 |
GET_STATIC_VAR_FROM_TLS(gmtmem, struct state)
|
|
290 |
#define lclmem (*GET_WSD_VAR_NAME(lclmem, s)())
|
|
291 |
#define gmtmem (*GET_WSD_VAR_NAME(gmtmem, s)())
|
|
292 |
#endif //EMULATOR
|
|
293 |
#else //__SYMBIAN32__
|
|
294 |
static struct state lclmem;
|
|
295 |
static struct state gmtmem;
|
|
296 |
#endif //__SYMBIAN32__
|
|
297 |
|
|
298 |
#define lclptr (&lclmem)
|
|
299 |
#define gmtptr (&gmtmem)
|
|
300 |
#endif /* State Farm */
|
|
301 |
|
|
302 |
#ifndef TZ_STRLEN_MAX
|
|
303 |
#define TZ_STRLEN_MAX 255
|
|
304 |
#endif /* !defined TZ_STRLEN_MAX */
|
|
305 |
|
|
306 |
#ifdef __SYMBIAN32__
|
|
307 |
#ifndef EMULATOR
|
|
308 |
|
|
309 |
static char lcl_TZname[TZ_STRLEN_MAX + 1];
|
|
310 |
static int lcl_is_set;
|
|
311 |
static int gmt_is_set;
|
|
312 |
static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
313 |
static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
314 |
|
|
315 |
char **_tzname = NULL;
|
|
316 |
#else //EMULATOR
|
|
317 |
|
|
318 |
GET_STATIC_ARRAY_FROM_TLS(lcl_TZname, char)
|
|
319 |
GET_STATIC_VAR_FROM_TLS(lcl_is_set, int)
|
|
320 |
GET_STATIC_VAR_FROM_TLS(gmt_is_set, int)
|
|
321 |
GET_STATIC_VAR_FROM_TLS(lcl_mutex, pthread_mutex_t)
|
|
322 |
GET_STATIC_VAR_FROM_TLS(gmt_mutex, pthread_mutex_t)
|
|
323 |
|
|
324 |
|
|
325 |
#define lcl_TZname (GET_WSD_VAR_NAME(lcl_TZname, s)())
|
|
326 |
#define lcl_is_set (*GET_WSD_VAR_NAME(lcl_is_set, s)())
|
|
327 |
#define gmt_is_set (*GET_WSD_VAR_NAME(gmt_is_set, s)())
|
|
328 |
#define lcl_mutex (*GET_WSD_VAR_NAME(lcl_mutex, s)())
|
|
329 |
#define gmt_mutex (*GET_WSD_VAR_NAME(gmt_mutex, s)())
|
|
330 |
|
|
331 |
#endif //EMULATOR
|
|
332 |
#else //__SYMBIAN32__
|
|
333 |
|
|
334 |
static char lcl_TZname[TZ_STRLEN_MAX + 1];
|
|
335 |
static int lcl_is_set;
|
|
336 |
static int gmt_is_set;
|
|
337 |
static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
338 |
static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
339 |
|
|
340 |
char * tzname[2] = {
|
|
341 |
wildabbr,
|
|
342 |
wildabbr
|
|
343 |
};
|
|
344 |
#endif //__SYMBIAN32__
|
|
345 |
|
|
346 |
/*
|
|
347 |
** Section 4.12.3 of X3.159-1989 requires that
|
|
348 |
** Except for the strftime function, these functions [asctime,
|
|
349 |
** ctime, gmtime, localtime] return values in one of two static
|
|
350 |
** objects: a broken-down time structure and an array of char.
|
|
351 |
** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
|
|
352 |
*/
|
|
353 |
#ifdef __SYMBIAN32__
|
|
354 |
#ifndef EMULATOR
|
|
355 |
|
|
356 |
struct tm stm;
|
|
357 |
#else //EMULATOR
|
|
358 |
|
|
359 |
GET_GLOBAL_VAR_FROM_TLS(stm, struct tm)
|
|
360 |
#define stm (*GET_WSD_VAR_NAME(stm, g)())
|
|
361 |
#endif //EMULATOR
|
|
362 |
#else //__SYMBIAN32__
|
|
363 |
static struct tm tm;
|
|
364 |
#endif //__SYMBIAN32__
|
|
365 |
|
|
366 |
#ifdef USG_COMPAT
|
|
367 |
|
|
368 |
#ifdef __SYMBIAN32__
|
|
369 |
#ifndef EMULATOR
|
|
370 |
|
|
371 |
time_t timezone = 0;
|
|
372 |
int daylight = 0;
|
|
373 |
#else //EMULATOR
|
|
374 |
|
|
375 |
GET_GLOBAL_VAR_FROM_TLS(timezone, time_t)
|
|
376 |
GET_GLOBAL_VAR_FROM_TLS(daylight, int)
|
|
377 |
|
|
378 |
#define timezone (*GET_WSD_VAR_NAME(timezone, g)())
|
|
379 |
#define daylight (*GET_WSD_VAR_NAME(daylight, g)())
|
|
380 |
#endif //EMULATOR
|
|
381 |
#else //__SYMBIAN32__
|
|
382 |
time_t timezone = 0;
|
|
383 |
int daylight = 0;
|
|
384 |
#endif //__SYMBIAN32__
|
|
385 |
|
|
386 |
#endif /* defined USG_COMPAT */
|
|
387 |
|
|
388 |
#ifdef ALTZONE
|
|
389 |
|
|
390 |
#ifdef __SYMBIAN32__
|
|
391 |
#ifndef EMULATOR
|
|
392 |
|
|
393 |
time_t altzone = 0;
|
|
394 |
#else //EMULATOR
|
|
395 |
|
|
396 |
GET_GLOBAL_VAR_FROM_TLS(altzone, time_t)
|
|
397 |
#define altzone (*GET_WSD_VAR_NAME(altzone, g)())
|
|
398 |
#endif //EMULATOR
|
|
399 |
#else //__SYMBIAN32__
|
|
400 |
time_t altzone = 0;
|
|
401 |
#endif //__SYMBIAN32__
|
|
402 |
|
|
403 |
#endif /* defined ALTZONE */
|
|
404 |
|
|
405 |
#ifdef __SYMBIAN32__
|
|
406 |
#ifdef EMULATOR
|
|
407 |
|
|
408 |
GET_STATIC_VAR_FROM_TLS(localtime_key, pthread_key_t)
|
|
409 |
GET_STATIC_VAR_FROM_TLS(localtime_mutex, pthread_mutex_t)
|
|
410 |
GET_STATIC_VAR_FROM_TLS(gmtime_key, pthread_key_t)
|
|
411 |
GET_STATIC_VAR_FROM_TLS(gmtime_mutex, pthread_mutex_t)
|
|
412 |
|
|
413 |
#define localtime_mutex (*GET_WSD_VAR_NAME(localtime_mutex, s)())
|
|
414 |
#define localtime_key (*GET_WSD_VAR_NAME(localtime_key, s)())
|
|
415 |
#define gmtime_mutex (*GET_WSD_VAR_NAME(gmtime_mutex, s)())
|
|
416 |
#define gmtime_key (*GET_WSD_VAR_NAME(gmtime_key, s)())
|
|
417 |
|
|
418 |
#endif //EMULATOR
|
|
419 |
#endif //__SYMBIAN32__
|
|
420 |
|
|
421 |
#if !defined(__SYMBIAN32__) || defined(__SYMBIAN_COMPILE_UNUSED__)
|
|
422 |
static long
|
|
423 |
detzcode(codep)
|
|
424 |
const char * const codep;
|
|
425 |
{
|
|
426 |
long result;
|
|
427 |
int i;
|
|
428 |
|
|
429 |
result = (codep[0] & 0x80) ? ~0L : 0L;
|
|
430 |
for (i = 0; i < 4; ++i)
|
|
431 |
result = (result << 8) | (codep[i] & 0xff);
|
|
432 |
return result;
|
|
433 |
}
|
|
434 |
#endif //if defined(__SYMBIAN32__) || defined (__SYMBIAN_COMPILE_UNUSED__)
|
|
435 |
|
|
436 |
static void
|
|
437 |
settzname(void)
|
|
438 |
{
|
|
439 |
struct state * sp = lclptr;
|
|
440 |
int i;
|
|
441 |
|
|
442 |
tzname[0] = wildabbr;
|
|
443 |
tzname[1] = wildabbr;
|
|
444 |
#ifdef USG_COMPAT
|
|
445 |
daylight = 0;
|
|
446 |
timezone = 0;
|
|
447 |
#endif /* defined USG_COMPAT */
|
|
448 |
#ifdef ALTZONE
|
|
449 |
altzone = 0;
|
|
450 |
#endif /* defined ALTZONE */
|
|
451 |
#ifdef ALL_STATE
|
|
452 |
if (sp == NULL) {
|
|
453 |
tzname[0] = tzname[1] = gmt;
|
|
454 |
return;
|
|
455 |
}
|
|
456 |
#endif /* defined ALL_STATE */
|
|
457 |
for (i = 0; i < sp->typecnt; ++i) {
|
|
458 |
const struct ttinfo * const ttisp = &sp->ttis[i];
|
|
459 |
|
|
460 |
tzname[ttisp->tt_isdst] =
|
|
461 |
&sp->chars[ttisp->tt_abbrind];
|
|
462 |
#ifdef USG_COMPAT
|
|
463 |
if (ttisp->tt_isdst)
|
|
464 |
daylight = 1;
|
|
465 |
if (i == 0 || !ttisp->tt_isdst)
|
|
466 |
timezone = -(ttisp->tt_gmtoff);
|
|
467 |
#endif /* defined USG_COMPAT */
|
|
468 |
#ifdef ALTZONE
|
|
469 |
if (i == 0 || ttisp->tt_isdst)
|
|
470 |
altzone = -(ttisp->tt_gmtoff);
|
|
471 |
#endif /* defined ALTZONE */
|
|
472 |
}
|
|
473 |
/*
|
|
474 |
** And to get the latest zone names into tzname. . .
|
|
475 |
*/
|
|
476 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
477 |
const struct ttinfo * const ttisp =
|
|
478 |
&sp->ttis[
|
|
479 |
sp->types[i]];
|
|
480 |
|
|
481 |
tzname[ttisp->tt_isdst] =
|
|
482 |
&sp->chars[ttisp->tt_abbrind];
|
|
483 |
}
|
|
484 |
}
|
|
485 |
|
|
486 |
static int
|
|
487 |
tzload(name, sp)
|
|
488 |
const char * name;
|
|
489 |
struct state * const sp;
|
|
490 |
{
|
|
491 |
#ifndef __SYMBIAN32__
|
|
492 |
const char * p;
|
|
493 |
int i;
|
|
494 |
int fid;
|
|
495 |
#else//__SYMBIAN32__
|
|
496 |
if(sp) { }//dummy implementation
|
|
497 |
#endif//__SYMBIAN32__
|
|
498 |
/* XXX The following is from OpenBSD, and I'm not sure it is correct */
|
|
499 |
if (name != NULL && issetugid() != 0)
|
|
500 |
if ((name[0] == ':' && name[1] == '/') ||
|
|
501 |
name[0] == '/' || strchr(name, '.'))
|
|
502 |
name = NULL;
|
|
503 |
if (name == NULL && (name = TZDEFAULT) == NULL)
|
|
504 |
return -1;
|
|
505 |
|
|
506 |
#ifdef __SYMBIAN32__
|
|
507 |
return -1; //We are doing this for the simple reason that these files
|
|
508 |
//do not exist in the Symbian environment and we really dont
|
|
509 |
//need to try accessing them
|
|
510 |
#else //__SYMBIAN32__
|
|
511 |
|
|
512 |
{
|
|
513 |
int doaccess;
|
|
514 |
struct stat stab;
|
|
515 |
/*
|
|
516 |
** Section 4.9.1 of the C standard says that
|
|
517 |
** "FILENAME_MAX expands to an integral constant expression
|
|
518 |
** that is the size needed for an array of char large enough
|
|
519 |
** to hold the longest file name string that the implementation
|
|
520 |
** guarantees can be opened."
|
|
521 |
*/
|
|
522 |
char fullname[FILENAME_MAX + 1];
|
|
523 |
|
|
524 |
if (name[0] == ':')
|
|
525 |
++name;
|
|
526 |
doaccess = name[0] == '/';
|
|
527 |
if (!doaccess) {
|
|
528 |
if ((p = TZDIR) == NULL)
|
|
529 |
return -1;
|
|
530 |
if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
|
|
531 |
return -1;
|
|
532 |
(void) strcpy(fullname, p);
|
|
533 |
(void) strcat(fullname, "/");
|
|
534 |
(void) strcat(fullname, name);
|
|
535 |
/*
|
|
536 |
** Set doaccess if '.' (as in "../") shows up in name.
|
|
537 |
*/
|
|
538 |
if (strchr(name, '.') != NULL)
|
|
539 |
doaccess = TRUE;
|
|
540 |
name = fullname;
|
|
541 |
}
|
|
542 |
if (doaccess && access(name, R_OK) != 0)
|
|
543 |
return -1;
|
|
544 |
if ((fid = _open(name, OPEN_MODE)) == -1)
|
|
545 |
return -1;
|
|
546 |
if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
|
|
547 |
_close(fid);
|
|
548 |
return -1;
|
|
549 |
}
|
|
550 |
}
|
|
551 |
{
|
|
552 |
struct tzhead * tzhp;
|
|
553 |
union {
|
|
554 |
struct tzhead tzhead;
|
|
555 |
char buf[sizeof *sp + sizeof *tzhp];
|
|
556 |
} u;
|
|
557 |
int ttisstdcnt;
|
|
558 |
int ttisgmtcnt;
|
|
559 |
|
|
560 |
i = _read(fid, u.buf, sizeof u.buf);
|
|
561 |
if (_close(fid) != 0)
|
|
562 |
return -1;
|
|
563 |
ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
|
|
564 |
ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
|
|
565 |
sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
|
|
566 |
sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
|
|
567 |
sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
|
|
568 |
sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
|
|
569 |
p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
|
|
570 |
if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
|
|
571 |
sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
|
|
572 |
sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
|
|
573 |
sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
|
|
574 |
(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
|
|
575 |
(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
|
|
576 |
return -1;
|
|
577 |
if (i - (p - u.buf) < sp->timecnt * 4 + /* ats */
|
|
578 |
sp->timecnt + /* types */
|
|
579 |
sp->typecnt * (4 + 2) + /* ttinfos */
|
|
580 |
sp->charcnt + /* chars */
|
|
581 |
sp->leapcnt * (4 + 4) + /* lsinfos */
|
|
582 |
ttisstdcnt + /* ttisstds */
|
|
583 |
ttisgmtcnt) /* ttisgmts */
|
|
584 |
return -1;
|
|
585 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
586 |
sp->ats[i] = detzcode(p);
|
|
587 |
p += 4;
|
|
588 |
}
|
|
589 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
590 |
sp->types[i] = (unsigned char) *p++;
|
|
591 |
if (sp->types[i] >= sp->typecnt)
|
|
592 |
return -1;
|
|
593 |
}
|
|
594 |
for (i = 0; i < sp->typecnt; ++i) {
|
|
595 |
struct ttinfo * ttisp;
|
|
596 |
|
|
597 |
ttisp = &sp->ttis[i];
|
|
598 |
ttisp->tt_gmtoff = detzcode(p);
|
|
599 |
p += 4;
|
|
600 |
ttisp->tt_isdst = (unsigned char) *p++;
|
|
601 |
if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
|
|
602 |
return -1;
|
|
603 |
ttisp->tt_abbrind = (unsigned char) *p++;
|
|
604 |
if (ttisp->tt_abbrind < 0 ||
|
|
605 |
ttisp->tt_abbrind > sp->charcnt)
|
|
606 |
return -1;
|
|
607 |
}
|
|
608 |
for (i = 0; i < sp->charcnt; ++i)
|
|
609 |
sp->chars[i] = *p++;
|
|
610 |
sp->chars[i] = '\0'; /* ensure '\0' at end */
|
|
611 |
for (i = 0; i < sp->leapcnt; ++i) {
|
|
612 |
struct lsinfo * lsisp;
|
|
613 |
|
|
614 |
lsisp = &sp->lsis[i];
|
|
615 |
lsisp->ls_trans = detzcode(p);
|
|
616 |
p += 4;
|
|
617 |
lsisp->ls_corr = detzcode(p);
|
|
618 |
p += 4;
|
|
619 |
}
|
|
620 |
for (i = 0; i < sp->typecnt; ++i) {
|
|
621 |
struct ttinfo * ttisp;
|
|
622 |
|
|
623 |
ttisp = &sp->ttis[i];
|
|
624 |
if (ttisstdcnt == 0)
|
|
625 |
ttisp->tt_ttisstd = FALSE;
|
|
626 |
else {
|
|
627 |
ttisp->tt_ttisstd = *p++;
|
|
628 |
if (ttisp->tt_ttisstd != TRUE &&
|
|
629 |
ttisp->tt_ttisstd != FALSE)
|
|
630 |
return -1;
|
|
631 |
}
|
|
632 |
}
|
|
633 |
for (i = 0; i < sp->typecnt; ++i) {
|
|
634 |
struct ttinfo * ttisp;
|
|
635 |
|
|
636 |
ttisp = &sp->ttis[i];
|
|
637 |
if (ttisgmtcnt == 0)
|
|
638 |
ttisp->tt_ttisgmt = FALSE;
|
|
639 |
else {
|
|
640 |
ttisp->tt_ttisgmt = *p++;
|
|
641 |
if (ttisp->tt_ttisgmt != TRUE &&
|
|
642 |
ttisp->tt_ttisgmt != FALSE)
|
|
643 |
return -1;
|
|
644 |
}
|
|
645 |
}
|
|
646 |
}
|
|
647 |
#endif //__SYMBIAN32__
|
|
648 |
return 0;
|
|
649 |
}
|
|
650 |
|
|
651 |
static const int mon_lengths[2][MONSPERYEAR] = {
|
|
652 |
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
|
653 |
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
|
654 |
};
|
|
655 |
|
|
656 |
static const int year_lengths[2] = {
|
|
657 |
DAYSPERNYEAR, DAYSPERLYEAR
|
|
658 |
};
|
|
659 |
|
|
660 |
/*
|
|
661 |
** Given a pointer into a time zone string, scan until a character that is not
|
|
662 |
** a valid character in a zone name is found. Return a pointer to that
|
|
663 |
** character.
|
|
664 |
*/
|
|
665 |
|
|
666 |
static const char *
|
|
667 |
getzname(strp)
|
|
668 |
const char * strp;
|
|
669 |
{
|
|
670 |
char c;
|
|
671 |
|
|
672 |
while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
|
|
673 |
c != '+')
|
|
674 |
++strp;
|
|
675 |
return strp;
|
|
676 |
}
|
|
677 |
|
|
678 |
/*
|
|
679 |
** Given a pointer into a time zone string, extract a number from that string.
|
|
680 |
** Check that the number is within a specified range; if it is not, return
|
|
681 |
** NULL.
|
|
682 |
** Otherwise, return a pointer to the first character not part of the number.
|
|
683 |
*/
|
|
684 |
|
|
685 |
static const char *
|
|
686 |
getnum(strp, nump, min, max)
|
|
687 |
const char * strp;
|
|
688 |
int * const nump;
|
|
689 |
const int min;
|
|
690 |
const int max;
|
|
691 |
{
|
|
692 |
char c;
|
|
693 |
int num;
|
|
694 |
|
|
695 |
if (strp == NULL || !is_digit(c = *strp))
|
|
696 |
return NULL;
|
|
697 |
num = 0;
|
|
698 |
do {
|
|
699 |
num = num * 10 + (c - '0');
|
|
700 |
if (num > max)
|
|
701 |
return NULL; /* illegal value */
|
|
702 |
c = *++strp;
|
|
703 |
} while (is_digit(c));
|
|
704 |
if (num < min)
|
|
705 |
return NULL; /* illegal value */
|
|
706 |
*nump = num;
|
|
707 |
return strp;
|
|
708 |
}
|
|
709 |
|
|
710 |
/*
|
|
711 |
** Given a pointer into a time zone string, extract a number of seconds,
|
|
712 |
** in hh[:mm[:ss]] form, from the string.
|
|
713 |
** If any error occurs, return NULL.
|
|
714 |
** Otherwise, return a pointer to the first character not part of the number
|
|
715 |
** of seconds.
|
|
716 |
*/
|
|
717 |
|
|
718 |
static const char *
|
|
719 |
getsecs(strp, secsp)
|
|
720 |
const char * strp;
|
|
721 |
long * const secsp;
|
|
722 |
{
|
|
723 |
int num;
|
|
724 |
|
|
725 |
/*
|
|
726 |
** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
|
|
727 |
** "M10.4.6/26", which does not conform to Posix,
|
|
728 |
** but which specifies the equivalent of
|
|
729 |
** ``02:00 on the first Sunday on or after 23 Oct''.
|
|
730 |
*/
|
|
731 |
strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
|
|
732 |
if (strp == NULL)
|
|
733 |
return NULL;
|
|
734 |
*secsp = num * (long) SECSPERHOUR;
|
|
735 |
if (*strp == ':') {
|
|
736 |
++strp;
|
|
737 |
strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
|
|
738 |
if (strp == NULL)
|
|
739 |
return NULL;
|
|
740 |
*secsp += num * SECSPERMIN;
|
|
741 |
if (*strp == ':') {
|
|
742 |
++strp;
|
|
743 |
/* `SECSPERMIN' allows for leap seconds. */
|
|
744 |
strp = getnum(strp, &num, 0, SECSPERMIN);
|
|
745 |
if (strp == NULL)
|
|
746 |
return NULL;
|
|
747 |
*secsp += num;
|
|
748 |
}
|
|
749 |
}
|
|
750 |
return strp;
|
|
751 |
}
|
|
752 |
|
|
753 |
/*
|
|
754 |
** Given a pointer into a time zone string, extract an offset, in
|
|
755 |
** [+-]hh[:mm[:ss]] form, from the string.
|
|
756 |
** If any error occurs, return NULL.
|
|
757 |
** Otherwise, return a pointer to the first character not part of the time.
|
|
758 |
*/
|
|
759 |
|
|
760 |
static const char *
|
|
761 |
getoffset(strp, offsetp)
|
|
762 |
const char * strp;
|
|
763 |
long * const offsetp;
|
|
764 |
{
|
|
765 |
int neg = 0;
|
|
766 |
|
|
767 |
if (*strp == '-') {
|
|
768 |
neg = 1;
|
|
769 |
++strp;
|
|
770 |
} else if (*strp == '+')
|
|
771 |
++strp;
|
|
772 |
strp = getsecs(strp, offsetp);
|
|
773 |
if (strp == NULL)
|
|
774 |
return NULL; /* illegal time */
|
|
775 |
if (neg)
|
|
776 |
*offsetp = -*offsetp;
|
|
777 |
return strp;
|
|
778 |
}
|
|
779 |
|
|
780 |
/*
|
|
781 |
** Given a pointer into a time zone string, extract a rule in the form
|
|
782 |
** date[/time]. See POSIX section 8 for the format of "date" and "time".
|
|
783 |
** If a valid rule is not found, return NULL.
|
|
784 |
** Otherwise, return a pointer to the first character not part of the rule.
|
|
785 |
*/
|
|
786 |
|
|
787 |
static const char *
|
|
788 |
getrule(strp, rulep)
|
|
789 |
const char * strp;
|
|
790 |
struct rule * const rulep;
|
|
791 |
{
|
|
792 |
if (*strp == 'J') {
|
|
793 |
/*
|
|
794 |
** Julian day.
|
|
795 |
*/
|
|
796 |
rulep->r_type = JULIAN_DAY;
|
|
797 |
++strp;
|
|
798 |
strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
|
|
799 |
} else if (*strp == 'M') {
|
|
800 |
/*
|
|
801 |
** Month, week, day.
|
|
802 |
*/
|
|
803 |
rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
|
|
804 |
++strp;
|
|
805 |
strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
|
|
806 |
if (strp == NULL)
|
|
807 |
return NULL;
|
|
808 |
if (*strp++ != '.')
|
|
809 |
return NULL;
|
|
810 |
strp = getnum(strp, &rulep->r_week, 1, 5);
|
|
811 |
if (strp == NULL)
|
|
812 |
return NULL;
|
|
813 |
if (*strp++ != '.')
|
|
814 |
return NULL;
|
|
815 |
strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
|
|
816 |
} else if (is_digit(*strp)) {
|
|
817 |
/*
|
|
818 |
** Day of year.
|
|
819 |
*/
|
|
820 |
rulep->r_type = DAY_OF_YEAR;
|
|
821 |
strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
|
|
822 |
} else return NULL; /* invalid format */
|
|
823 |
if (strp == NULL)
|
|
824 |
return NULL;
|
|
825 |
if (*strp == '/') {
|
|
826 |
/*
|
|
827 |
** Time specified.
|
|
828 |
*/
|
|
829 |
++strp;
|
|
830 |
strp = getsecs(strp, &rulep->r_time);
|
|
831 |
} else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
|
|
832 |
return strp;
|
|
833 |
}
|
|
834 |
|
|
835 |
/*
|
|
836 |
** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
|
|
837 |
** year, a rule, and the offset from UTC at the time that rule takes effect,
|
|
838 |
** calculate the Epoch-relative time that rule takes effect.
|
|
839 |
*/
|
|
840 |
|
|
841 |
static time_t
|
|
842 |
transtime(janfirst, year, rulep, offset)
|
|
843 |
const time_t janfirst;
|
|
844 |
const int year;
|
|
845 |
const struct rule * const rulep;
|
|
846 |
const long offset;
|
|
847 |
{
|
|
848 |
int leapyear;
|
|
849 |
time_t value;
|
|
850 |
int i;
|
|
851 |
int d, m1, yy0, yy1, yy2, dow;
|
|
852 |
|
|
853 |
INITIALIZE(value);
|
|
854 |
leapyear = isleap(year);
|
|
855 |
switch (rulep->r_type) {
|
|
856 |
|
|
857 |
case JULIAN_DAY:
|
|
858 |
/*
|
|
859 |
** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
|
|
860 |
** years.
|
|
861 |
** In non-leap years, or if the day number is 59 or less, just
|
|
862 |
** add SECSPERDAY times the day number-1 to the time of
|
|
863 |
** January 1, midnight, to get the day.
|
|
864 |
*/
|
|
865 |
value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
|
|
866 |
if (leapyear && rulep->r_day >= 60)
|
|
867 |
value += SECSPERDAY;
|
|
868 |
break;
|
|
869 |
|
|
870 |
case DAY_OF_YEAR:
|
|
871 |
/*
|
|
872 |
** n - day of year.
|
|
873 |
** Just add SECSPERDAY times the day number to the time of
|
|
874 |
** January 1, midnight, to get the day.
|
|
875 |
*/
|
|
876 |
value = janfirst + rulep->r_day * SECSPERDAY;
|
|
877 |
break;
|
|
878 |
|
|
879 |
case MONTH_NTH_DAY_OF_WEEK:
|
|
880 |
/*
|
|
881 |
** Mm.n.d - nth "dth day" of month m.
|
|
882 |
*/
|
|
883 |
value = janfirst;
|
|
884 |
for (i = 0; i < rulep->r_mon - 1; ++i)
|
|
885 |
value += mon_lengths[leapyear][i] * SECSPERDAY;
|
|
886 |
|
|
887 |
/*
|
|
888 |
** Use Zeller's Congruence to get day-of-week of first day of
|
|
889 |
** month.
|
|
890 |
*/
|
|
891 |
m1 = (rulep->r_mon + 9) % 12 + 1;
|
|
892 |
yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
|
|
893 |
yy1 = yy0 / 100;
|
|
894 |
yy2 = yy0 % 100;
|
|
895 |
dow = ((26 * m1 - 2) / 10 +
|
|
896 |
1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
|
|
897 |
if (dow < 0)
|
|
898 |
dow += DAYSPERWEEK;
|
|
899 |
|
|
900 |
/*
|
|
901 |
** "dow" is the day-of-week of the first day of the month. Get
|
|
902 |
** the day-of-month (zero-origin) of the first "dow" day of the
|
|
903 |
** month.
|
|
904 |
*/
|
|
905 |
d = rulep->r_day - dow;
|
|
906 |
if (d < 0)
|
|
907 |
d += DAYSPERWEEK;
|
|
908 |
for (i = 1; i < rulep->r_week; ++i) {
|
|
909 |
if (d + DAYSPERWEEK >=
|
|
910 |
mon_lengths[leapyear][rulep->r_mon - 1])
|
|
911 |
break;
|
|
912 |
d += DAYSPERWEEK;
|
|
913 |
}
|
|
914 |
|
|
915 |
/*
|
|
916 |
** "d" is the day-of-month (zero-origin) of the day we want.
|
|
917 |
*/
|
|
918 |
value += d * SECSPERDAY;
|
|
919 |
break;
|
|
920 |
}
|
|
921 |
|
|
922 |
/*
|
|
923 |
** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
|
|
924 |
** question. To get the Epoch-relative time of the specified local
|
|
925 |
** time on that day, add the transition time and the current offset
|
|
926 |
** from UTC.
|
|
927 |
*/
|
|
928 |
return value + rulep->r_time + offset;
|
|
929 |
}
|
|
930 |
|
|
931 |
/*
|
|
932 |
** Given a POSIX section 8-style TZ string, fill in the rule tables as
|
|
933 |
** appropriate.
|
|
934 |
*/
|
|
935 |
|
|
936 |
static int
|
|
937 |
tzparse(name, sp, lastditch)
|
|
938 |
const char * name;
|
|
939 |
struct state * const sp;
|
|
940 |
const int lastditch;
|
|
941 |
{
|
|
942 |
const char * stdname;
|
|
943 |
const char * dstname;
|
|
944 |
size_t stdlen;
|
|
945 |
size_t dstlen;
|
|
946 |
long stdoffset;
|
|
947 |
long dstoffset;
|
|
948 |
time_t * atp;
|
|
949 |
unsigned char * typep;
|
|
950 |
char * cp;
|
|
951 |
int load_result;
|
|
952 |
|
|
953 |
INITIALIZE(dstname);
|
|
954 |
stdname = name;
|
|
955 |
if (lastditch) {
|
|
956 |
stdlen = strlen(name); /* length of standard zone name */
|
|
957 |
name += stdlen;
|
|
958 |
if (stdlen >= sizeof sp->chars)
|
|
959 |
stdlen = (sizeof sp->chars) - 1;
|
|
960 |
#ifndef __SYMBIAN32__
|
|
961 |
stdoffset = 0;
|
|
962 |
#else //__SYMBIAN32__
|
|
963 |
stdoffset = (-1)*GetStdOffset();
|
|
964 |
#endif //__SYMBIAN32__
|
|
965 |
} else {
|
|
966 |
name = getzname(name);
|
|
967 |
stdlen = name - stdname;
|
|
968 |
if (stdlen < 3)
|
|
969 |
return -1;
|
|
970 |
if (*name == '\0')
|
|
971 |
return -1; /* was "stdoffset = 0;" */
|
|
972 |
else {
|
|
973 |
name = getoffset(name, &stdoffset);
|
|
974 |
if (name == NULL)
|
|
975 |
return -1;
|
|
976 |
}
|
|
977 |
}
|
|
978 |
load_result = tzload(TZDEFRULES, sp);
|
|
979 |
if (load_result != 0)
|
|
980 |
sp->leapcnt = 0; /* so, we're off a little */
|
|
981 |
if (*name != '\0') {
|
|
982 |
dstname = name;
|
|
983 |
name = getzname(name);
|
|
984 |
dstlen = name - dstname; /* length of DST zone name */
|
|
985 |
if (dstlen < 3)
|
|
986 |
return -1;
|
|
987 |
if (*name != '\0' && *name != ',' && *name != ';') {
|
|
988 |
name = getoffset(name, &dstoffset);
|
|
989 |
if (name == NULL)
|
|
990 |
return -1;
|
|
991 |
} else dstoffset = stdoffset - SECSPERHOUR;
|
|
992 |
if (*name == '\0' && load_result != 0)
|
|
993 |
name = TZDEFRULESTRING;
|
|
994 |
if (*name == ',' || *name == ';') {
|
|
995 |
struct rule start;
|
|
996 |
struct rule end;
|
|
997 |
int year;
|
|
998 |
time_t janfirst;
|
|
999 |
time_t starttime;
|
|
1000 |
time_t endtime;
|
|
1001 |
|
|
1002 |
++name;
|
|
1003 |
if ((name = getrule(name, &start)) == NULL)
|
|
1004 |
return -1;
|
|
1005 |
if (*name++ != ',')
|
|
1006 |
return -1;
|
|
1007 |
if ((name = getrule(name, &end)) == NULL)
|
|
1008 |
return -1;
|
|
1009 |
if (*name != '\0')
|
|
1010 |
return -1;
|
|
1011 |
sp->typecnt = 2; /* standard time and DST */
|
|
1012 |
/*
|
|
1013 |
** Two transitions per year, from EPOCH_YEAR to 2037.
|
|
1014 |
*/
|
|
1015 |
sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
|
|
1016 |
if (sp->timecnt > TZ_MAX_TIMES)
|
|
1017 |
return -1;
|
|
1018 |
sp->ttis[0].tt_gmtoff = -dstoffset;
|
|
1019 |
sp->ttis[0].tt_isdst = 1;
|
|
1020 |
sp->ttis[0].tt_abbrind = stdlen + 1;
|
|
1021 |
sp->ttis[1].tt_gmtoff = -stdoffset;
|
|
1022 |
sp->ttis[1].tt_isdst = 0;
|
|
1023 |
sp->ttis[1].tt_abbrind = 0;
|
|
1024 |
atp = sp->ats;
|
|
1025 |
typep = sp->types;
|
|
1026 |
janfirst = 0;
|
|
1027 |
for (year = EPOCH_YEAR; year <= 2037; ++year) {
|
|
1028 |
starttime = transtime(janfirst, year, &start,
|
|
1029 |
stdoffset);
|
|
1030 |
endtime = transtime(janfirst, year, &end,
|
|
1031 |
dstoffset);
|
|
1032 |
if (starttime > endtime) {
|
|
1033 |
*atp++ = endtime;
|
|
1034 |
*typep++ = 1; /* DST ends */
|
|
1035 |
*atp++ = starttime;
|
|
1036 |
*typep++ = 0; /* DST begins */
|
|
1037 |
} else {
|
|
1038 |
*atp++ = starttime;
|
|
1039 |
*typep++ = 0; /* DST begins */
|
|
1040 |
*atp++ = endtime;
|
|
1041 |
*typep++ = 1; /* DST ends */
|
|
1042 |
}
|
|
1043 |
janfirst += year_lengths[isleap(year)] *
|
|
1044 |
SECSPERDAY;
|
|
1045 |
}
|
|
1046 |
} else {
|
|
1047 |
long theirstdoffset;
|
|
1048 |
long theirdstoffset;
|
|
1049 |
long theiroffset;
|
|
1050 |
int isdst;
|
|
1051 |
int i;
|
|
1052 |
int j;
|
|
1053 |
|
|
1054 |
if (*name != '\0')
|
|
1055 |
return -1;
|
|
1056 |
/*
|
|
1057 |
** Initial values of theirstdoffset and theirdstoffset.
|
|
1058 |
*/
|
|
1059 |
theirstdoffset = 0;
|
|
1060 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
1061 |
j = sp->types[i];
|
|
1062 |
if (!sp->ttis[j].tt_isdst) {
|
|
1063 |
theirstdoffset =
|
|
1064 |
-sp->ttis[j].tt_gmtoff;
|
|
1065 |
break;
|
|
1066 |
}
|
|
1067 |
}
|
|
1068 |
theirdstoffset = 0;
|
|
1069 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
1070 |
j = sp->types[i];
|
|
1071 |
if (sp->ttis[j].tt_isdst) {
|
|
1072 |
theirdstoffset =
|
|
1073 |
-sp->ttis[j].tt_gmtoff;
|
|
1074 |
break;
|
|
1075 |
}
|
|
1076 |
}
|
|
1077 |
/*
|
|
1078 |
** Initially we're assumed to be in standard time.
|
|
1079 |
*/
|
|
1080 |
isdst = FALSE;
|
|
1081 |
theiroffset = theirstdoffset;
|
|
1082 |
/*
|
|
1083 |
** Now juggle transition times and types
|
|
1084 |
** tracking offsets as you do.
|
|
1085 |
*/
|
|
1086 |
for (i = 0; i < sp->timecnt; ++i) {
|
|
1087 |
j = sp->types[i];
|
|
1088 |
sp->types[i] = sp->ttis[j].tt_isdst;
|
|
1089 |
if (sp->ttis[j].tt_ttisgmt) {
|
|
1090 |
/* No adjustment to transition time */
|
|
1091 |
} else {
|
|
1092 |
/*
|
|
1093 |
** If summer time is in effect, and the
|
|
1094 |
** transition time was not specified as
|
|
1095 |
** standard time, add the summer time
|
|
1096 |
** offset to the transition time;
|
|
1097 |
** otherwise, add the standard time
|
|
1098 |
** offset to the transition time.
|
|
1099 |
*/
|
|
1100 |
/*
|
|
1101 |
** Transitions from DST to DDST
|
|
1102 |
** will effectively disappear since
|
|
1103 |
** POSIX provides for only one DST
|
|
1104 |
** offset.
|
|
1105 |
*/
|
|
1106 |
if (isdst && !sp->ttis[j].tt_ttisstd) {
|
|
1107 |
sp->ats[i] += dstoffset -
|
|
1108 |
theirdstoffset;
|
|
1109 |
} else {
|
|
1110 |
sp->ats[i] += stdoffset -
|
|
1111 |
theirstdoffset;
|
|
1112 |
}
|
|
1113 |
}
|
|
1114 |
theiroffset = -sp->ttis[j].tt_gmtoff;
|
|
1115 |
if (sp->ttis[j].tt_isdst)
|
|
1116 |
theirdstoffset = theiroffset;
|
|
1117 |
else theirstdoffset = theiroffset;
|
|
1118 |
}
|
|
1119 |
/*
|
|
1120 |
** Finally, fill in ttis.
|
|
1121 |
** ttisstd and ttisgmt need not be handled.
|
|
1122 |
*/
|
|
1123 |
sp->ttis[0].tt_gmtoff = -stdoffset;
|
|
1124 |
sp->ttis[0].tt_isdst = FALSE;
|
|
1125 |
sp->ttis[0].tt_abbrind = 0;
|
|
1126 |
sp->ttis[1].tt_gmtoff = -dstoffset;
|
|
1127 |
sp->ttis[1].tt_isdst = TRUE;
|
|
1128 |
sp->ttis[1].tt_abbrind = stdlen + 1;
|
|
1129 |
sp->typecnt = 2;
|
|
1130 |
}
|
|
1131 |
} else {
|
|
1132 |
dstlen = 0;
|
|
1133 |
sp->typecnt = 1; /* only standard time */
|
|
1134 |
sp->timecnt = 0;
|
|
1135 |
sp->ttis[0].tt_gmtoff = -stdoffset;
|
|
1136 |
#ifndef __SYMBIAN32__
|
|
1137 |
sp->ttis[0].tt_isdst = 0;
|
|
1138 |
#else //__SYMBIAN32__
|
|
1139 |
sp->ttis[0].tt_isdst = GetIsDst();
|
|
1140 |
#endif //__SYMBIAN32__
|
|
1141 |
sp->ttis[0].tt_abbrind = 0;
|
|
1142 |
}
|
|
1143 |
sp->charcnt = stdlen + 1;
|
|
1144 |
if (dstlen != 0)
|
|
1145 |
sp->charcnt += dstlen + 1;
|
|
1146 |
if ((size_t) sp->charcnt > sizeof sp->chars)
|
|
1147 |
return -1;
|
|
1148 |
cp = sp->chars;
|
|
1149 |
(void) strncpy(cp, stdname, stdlen);
|
|
1150 |
cp += stdlen;
|
|
1151 |
*cp++ = '\0';
|
|
1152 |
if (dstlen != 0) {
|
|
1153 |
(void) strncpy(cp, dstname, dstlen);
|
|
1154 |
*(cp + dstlen) = '\0';
|
|
1155 |
}
|
|
1156 |
return 0;
|
|
1157 |
}
|
|
1158 |
|
|
1159 |
static void
|
|
1160 |
gmtload(sp)
|
|
1161 |
struct state * const sp;
|
|
1162 |
{
|
|
1163 |
if (tzload(gmt, sp) != 0)
|
|
1164 |
(void) tzparse(gmt, sp, TRUE);
|
|
1165 |
}
|
|
1166 |
|
|
1167 |
static void
|
|
1168 |
tzsetwall_basic(void)
|
|
1169 |
{
|
|
1170 |
if (lcl_is_set < 0)
|
|
1171 |
return;
|
|
1172 |
lcl_is_set = -1;
|
|
1173 |
|
|
1174 |
#ifdef ALL_STATE
|
|
1175 |
if (lclptr == NULL) {
|
|
1176 |
lclptr = (struct state *) malloc(sizeof *lclptr);
|
|
1177 |
if (lclptr == NULL) {
|
|
1178 |
settzname(); /* all we can do */
|
|
1179 |
return;
|
|
1180 |
}
|
|
1181 |
}
|
|
1182 |
#endif /* defined ALL_STATE */
|
|
1183 |
if (tzload((char *) NULL, lclptr) != 0)
|
|
1184 |
gmtload(lclptr);
|
|
1185 |
settzname();
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
#ifndef __SYMBIAN32__
|
|
1189 |
void
|
|
1190 |
tzsetwall(void)
|
|
1191 |
{
|
|
1192 |
_MUTEX_LOCK(&lcl_mutex);
|
|
1193 |
tzsetwall_basic();
|
|
1194 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
1195 |
}
|
|
1196 |
#endif //__SYMBIAN32__
|
|
1197 |
|
|
1198 |
static void
|
|
1199 |
tzset_basic(void)
|
|
1200 |
{
|
|
1201 |
const char * name;
|
|
1202 |
|
|
1203 |
name = getenv("TZ");
|
|
1204 |
if (name == NULL) {
|
|
1205 |
tzsetwall_basic();
|
|
1206 |
return;
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
|
|
1210 |
return;
|
|
1211 |
lcl_is_set = strlen(name) < sizeof lcl_TZname;
|
|
1212 |
if (lcl_is_set)
|
|
1213 |
(void) strcpy(lcl_TZname, name);
|
|
1214 |
|
|
1215 |
#ifdef ALL_STATE
|
|
1216 |
if (lclptr == NULL) {
|
|
1217 |
lclptr = (struct state *) malloc(sizeof *lclptr);
|
|
1218 |
if (lclptr == NULL) {
|
|
1219 |
settzname(); /* all we can do */
|
|
1220 |
return;
|
|
1221 |
}
|
|
1222 |
}
|
|
1223 |
#endif /* defined ALL_STATE */
|
|
1224 |
if (*name == '\0') {
|
|
1225 |
/*
|
|
1226 |
** User wants it fast rather than right.
|
|
1227 |
*/
|
|
1228 |
lclptr->leapcnt = 0; /* so, we're off a little */
|
|
1229 |
lclptr->timecnt = 0;
|
|
1230 |
lclptr->typecnt = 0;
|
|
1231 |
lclptr->ttis[0].tt_isdst = 0;
|
|
1232 |
lclptr->ttis[0].tt_gmtoff = 0;
|
|
1233 |
lclptr->ttis[0].tt_abbrind = 0;
|
|
1234 |
(void) strcpy(lclptr->chars, gmt);
|
|
1235 |
} else if (tzload(name, lclptr) != 0)
|
|
1236 |
if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
|
|
1237 |
(void) gmtload(lclptr);
|
|
1238 |
settzname();
|
|
1239 |
}
|
|
1240 |
|
|
1241 |
EXPORT_C
|
|
1242 |
void
|
|
1243 |
tzset(void)
|
|
1244 |
{
|
|
1245 |
_MUTEX_LOCK(&lcl_mutex);
|
|
1246 |
tzset_basic();
|
|
1247 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
1248 |
}
|
|
1249 |
|
|
1250 |
/*
|
|
1251 |
** The easy way to behave "as if no library function calls" localtime
|
|
1252 |
** is to not call it--so we drop its guts into "localsub", which can be
|
|
1253 |
** freely called. (And no, the PANS doesn't require the above behavior--
|
|
1254 |
** but it *is* desirable.)
|
|
1255 |
**
|
|
1256 |
** The unused offset argument is for the benefit of mktime variants.
|
|
1257 |
*/
|
|
1258 |
|
|
1259 |
/*ARGSUSED*/
|
|
1260 |
static void
|
|
1261 |
localsub(timep, offset, tmp)
|
|
1262 |
const time_t * const timep;
|
|
1263 |
#ifdef __SYMBIAN32__
|
|
1264 |
long offset;
|
|
1265 |
#else//__SYMBIAN32__
|
|
1266 |
const long offset;
|
|
1267 |
#endif//__SYMBIAN32__
|
|
1268 |
struct tm * const tmp;
|
|
1269 |
{
|
|
1270 |
struct state * sp;
|
|
1271 |
const struct ttinfo * ttisp;
|
|
1272 |
int i;
|
|
1273 |
const time_t t = *timep;
|
|
1274 |
|
|
1275 |
sp = lclptr;
|
|
1276 |
#ifdef __SYMBIAN32__
|
|
1277 |
offset = offset; /*to fix warning 'variable/argument not used in function' */
|
|
1278 |
#endif //__SYMBIAN32__
|
|
1279 |
#ifdef ALL_STATE
|
|
1280 |
if (sp == NULL) {
|
|
1281 |
gmtsub(timep, offset, tmp);
|
|
1282 |
return;
|
|
1283 |
}
|
|
1284 |
#endif /* defined ALL_STATE */
|
|
1285 |
if (sp->timecnt == 0 || t < sp->ats[0]) {
|
|
1286 |
i = 0;
|
|
1287 |
while (sp->ttis[i].tt_isdst)
|
|
1288 |
if (++i >= sp->typecnt) {
|
|
1289 |
i = 0;
|
|
1290 |
break;
|
|
1291 |
}
|
|
1292 |
} else {
|
|
1293 |
for (i = 1; i < sp->timecnt; ++i)
|
|
1294 |
if (t < sp->ats[i])
|
|
1295 |
break;
|
|
1296 |
i = sp->types[i - 1];
|
|
1297 |
}
|
|
1298 |
ttisp = &sp->ttis[i];
|
|
1299 |
/*
|
|
1300 |
** To get (wrong) behavior that's compatible with System V Release 2.0
|
|
1301 |
** you'd replace the statement below with
|
|
1302 |
** t += ttisp->tt_gmtoff;
|
|
1303 |
** timesub(&t, 0L, sp, tmp);
|
|
1304 |
*/
|
|
1305 |
timesub(&t, ttisp->tt_gmtoff, sp, tmp);
|
|
1306 |
tmp->tm_isdst = ttisp->tt_isdst;
|
|
1307 |
tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
|
|
1308 |
#ifdef TM_ZONE
|
|
1309 |
tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
|
|
1310 |
#endif /* defined TM_ZONE */
|
|
1311 |
}
|
|
1312 |
|
|
1313 |
EXPORT_C
|
|
1314 |
struct tm *
|
|
1315 |
localtime(timep)
|
|
1316 |
const time_t * const timep;
|
|
1317 |
{
|
|
1318 |
#ifdef __SYMBIAN32__
|
|
1319 |
int err = 0;
|
|
1320 |
#ifndef EMULATOR
|
|
1321 |
static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
1322 |
static pthread_key_t localtime_key = (unsigned)-1;
|
|
1323 |
#endif //EMULATOR
|
|
1324 |
#else //__SYMBIAN32__
|
|
1325 |
static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
1326 |
static pthread_key_t localtime_key = -1;
|
|
1327 |
#endif //__SYMBIAN32__
|
|
1328 |
struct tm *p_tm;
|
|
1329 |
|
|
1330 |
if (__isthreaded != 0) {
|
|
1331 |
_pthread_mutex_lock(&localtime_mutex);
|
|
1332 |
if (localtime_key == (pthread_key_t)-1) {
|
|
1333 |
if (_pthread_key_create(&localtime_key, free) < 0) {
|
|
1334 |
_pthread_mutex_unlock(&localtime_mutex);
|
|
1335 |
return(NULL);
|
|
1336 |
}
|
|
1337 |
}
|
|
1338 |
_pthread_mutex_unlock(&localtime_mutex);
|
|
1339 |
p_tm = _pthread_getspecific(localtime_key);
|
|
1340 |
if (p_tm == NULL) {
|
|
1341 |
if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
|
|
1342 |
== NULL)
|
|
1343 |
return(NULL);
|
|
1344 |
_pthread_setspecific(localtime_key, p_tm);
|
|
1345 |
}
|
|
1346 |
_pthread_mutex_lock(&lcl_mutex);
|
|
1347 |
tzset_basic();
|
|
1348 |
localsub(timep, 0L, p_tm);
|
|
1349 |
#ifdef __SYMBIAN32__
|
|
1350 |
if(getenv("TZ") == NULL)
|
|
1351 |
{
|
|
1352 |
err = ConvertTime(EUtcToLocal, (TInt*)timep, p_tm);
|
|
1353 |
}
|
|
1354 |
#endif
|
|
1355 |
_pthread_mutex_unlock(&lcl_mutex);
|
|
1356 |
if(err)
|
|
1357 |
{
|
|
1358 |
return NULL;
|
|
1359 |
}
|
|
1360 |
|
|
1361 |
return(p_tm);
|
|
1362 |
} else {
|
|
1363 |
tzset_basic();
|
|
1364 |
#ifdef __SYMBIAN32__
|
|
1365 |
localsub(timep, 0L, &stm);
|
|
1366 |
if(getenv("TZ") == NULL)
|
|
1367 |
{
|
|
1368 |
err = ConvertTime(EUtcToLocal,(TInt*)timep,&stm);
|
|
1369 |
if(err)
|
|
1370 |
{
|
|
1371 |
return NULL;
|
|
1372 |
}
|
|
1373 |
}
|
|
1374 |
return(&stm);
|
|
1375 |
#else //__SYMBIAN32__
|
|
1376 |
localsub(timep, 0L, &tm);
|
|
1377 |
return(&tm);
|
|
1378 |
#endif //__SYMBIAN32__
|
|
1379 |
}
|
|
1380 |
}
|
|
1381 |
|
|
1382 |
/*
|
|
1383 |
** Re-entrant version of localtime.
|
|
1384 |
*/
|
|
1385 |
|
|
1386 |
EXPORT_C
|
|
1387 |
struct tm *
|
|
1388 |
localtime_r(timep, tm)
|
|
1389 |
const time_t * const timep;
|
|
1390 |
struct tm * tm;
|
|
1391 |
{
|
|
1392 |
#ifdef __SYMBIAN32__
|
|
1393 |
int err = 0;
|
|
1394 |
#endif //__SYMBIAN32__
|
|
1395 |
_MUTEX_LOCK(&lcl_mutex);
|
|
1396 |
tzset_basic();
|
|
1397 |
localsub(timep, 0L, tm);
|
|
1398 |
#ifdef __SYMBIAN32__
|
|
1399 |
if(getenv("TZ") == NULL)
|
|
1400 |
{
|
|
1401 |
err = ConvertTime(EUtcToLocal,(TInt*)timep,tm);
|
|
1402 |
if(err)
|
|
1403 |
{
|
|
1404 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
1405 |
return NULL;
|
|
1406 |
}
|
|
1407 |
}
|
|
1408 |
#endif //__SYMBIAN32__
|
|
1409 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
1410 |
return tm;
|
|
1411 |
}
|
|
1412 |
|
|
1413 |
/*
|
|
1414 |
** gmtsub is to gmtime as localsub is to localtime.
|
|
1415 |
*/
|
|
1416 |
|
|
1417 |
static void
|
|
1418 |
gmtsub(timep, offset, tmp)
|
|
1419 |
const time_t * const timep;
|
|
1420 |
const long offset;
|
|
1421 |
struct tm * const tmp;
|
|
1422 |
{
|
|
1423 |
_MUTEX_LOCK(&gmt_mutex);
|
|
1424 |
if (!gmt_is_set) {
|
|
1425 |
gmt_is_set = TRUE;
|
|
1426 |
#ifdef ALL_STATE
|
|
1427 |
gmtptr = (struct state *) malloc(sizeof *gmtptr);
|
|
1428 |
if (gmtptr != NULL)
|
|
1429 |
#endif /* defined ALL_STATE */
|
|
1430 |
#ifndef __SYMBIAN32__
|
|
1431 |
gmtload(gmtptr);
|
|
1432 |
#endif //__SYMBIAN32__
|
|
1433 |
}
|
|
1434 |
_MUTEX_UNLOCK(&gmt_mutex);
|
|
1435 |
timesub(timep, offset, gmtptr, tmp);
|
|
1436 |
#ifdef TM_ZONE
|
|
1437 |
/*
|
|
1438 |
** Could get fancy here and deliver something such as
|
|
1439 |
** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
|
|
1440 |
** but this is no time for a treasure hunt.
|
|
1441 |
*/
|
|
1442 |
if (offset != 0)
|
|
1443 |
tmp->TM_ZONE = wildabbr;
|
|
1444 |
else {
|
|
1445 |
#ifdef ALL_STATE
|
|
1446 |
if (gmtptr == NULL)
|
|
1447 |
tmp->TM_ZONE = gmt;
|
|
1448 |
else tmp->TM_ZONE = gmtptr->chars;
|
|
1449 |
#endif /* defined ALL_STATE */
|
|
1450 |
#ifndef ALL_STATE
|
|
1451 |
#ifndef __SYMBIAN32__
|
|
1452 |
tmp->TM_ZONE = gmtptr->chars;
|
|
1453 |
#else //__SYMBIAN32__
|
|
1454 |
tmp->TM_ZONE ="UTC";
|
|
1455 |
#endif //__SYMBIAN32__
|
|
1456 |
#endif /* State Farm */
|
|
1457 |
}
|
|
1458 |
#endif /* defined TM_ZONE */
|
|
1459 |
}
|
|
1460 |
|
|
1461 |
EXPORT_C
|
|
1462 |
struct tm *
|
|
1463 |
gmtime(timep)
|
|
1464 |
const time_t * const timep;
|
|
1465 |
{
|
|
1466 |
#ifdef __SYMBIAN32__
|
|
1467 |
#ifndef EMULATOR
|
|
1468 |
static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
1469 |
static pthread_key_t gmtime_key = (unsigned)-1;
|
|
1470 |
#endif //EMULATOR
|
|
1471 |
#else //__SYMBIAN32__
|
|
1472 |
static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
1473 |
static pthread_key_t gmtime_key = -1;
|
|
1474 |
#endif //__SYMBIAN32__
|
|
1475 |
struct tm *p_tm;
|
|
1476 |
|
|
1477 |
if (__isthreaded != 0) {
|
|
1478 |
_pthread_mutex_lock(&gmtime_mutex);
|
|
1479 |
if (gmtime_key == (pthread_key_t)-1) {
|
|
1480 |
if (_pthread_key_create(&gmtime_key, free) < 0) {
|
|
1481 |
_pthread_mutex_unlock(&gmtime_mutex);
|
|
1482 |
return(NULL);
|
|
1483 |
}
|
|
1484 |
}
|
|
1485 |
_pthread_mutex_unlock(&gmtime_mutex);
|
|
1486 |
/*
|
|
1487 |
* Changed to follow POSIX.1 threads standard, which
|
|
1488 |
* is what BSD currently has.
|
|
1489 |
*/
|
|
1490 |
if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
|
|
1491 |
if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
|
|
1492 |
== NULL) {
|
|
1493 |
return(NULL);
|
|
1494 |
}
|
|
1495 |
_pthread_setspecific(gmtime_key, p_tm);
|
|
1496 |
}
|
|
1497 |
gmtsub(timep, 0L, p_tm);
|
|
1498 |
return(p_tm);
|
|
1499 |
}
|
|
1500 |
else {
|
|
1501 |
#ifdef __SYMBIAN32__
|
|
1502 |
gmtsub(timep, 0L, &stm);
|
|
1503 |
return(&stm);
|
|
1504 |
#else //__SYMBIAN32__
|
|
1505 |
gmtsub(timep, 0L, &tm);
|
|
1506 |
return(&tm);
|
|
1507 |
#endif //__SYMBIAN32__
|
|
1508 |
}
|
|
1509 |
}
|
|
1510 |
|
|
1511 |
/*
|
|
1512 |
* Re-entrant version of gmtime.
|
|
1513 |
*/
|
|
1514 |
|
|
1515 |
EXPORT_C
|
|
1516 |
struct tm *
|
|
1517 |
gmtime_r(timep, tm)
|
|
1518 |
const time_t * const timep;
|
|
1519 |
struct tm * tm;
|
|
1520 |
{
|
|
1521 |
gmtsub(timep, 0L, tm);
|
|
1522 |
return tm;
|
|
1523 |
}
|
|
1524 |
|
|
1525 |
#ifdef STD_INSPIRED
|
|
1526 |
#ifdef __SYMBIAN_COMPILE_UNUSED__
|
|
1527 |
struct tm *
|
|
1528 |
offtime(timep, offset)
|
|
1529 |
const time_t * const timep;
|
|
1530 |
const long offset;
|
|
1531 |
{
|
|
1532 |
#ifdef __SYMBIAN32__
|
|
1533 |
gmtsub(timep, offset, &stm);
|
|
1534 |
return &stm;
|
|
1535 |
#else //__SYMBIAN32__
|
|
1536 |
gmtsub(timep, offset, &tm);
|
|
1537 |
return &tm;
|
|
1538 |
#endif //__SYMBIAN32__
|
|
1539 |
}
|
|
1540 |
#endif //__SYMBIAN_COMPILE_UNUSED__
|
|
1541 |
#endif /* defined STD_INSPIRED */
|
|
1542 |
|
|
1543 |
static void
|
|
1544 |
timesub(timep, offset, sp, tmp)
|
|
1545 |
const time_t * const timep;
|
|
1546 |
const long offset;
|
|
1547 |
const struct state * const sp;
|
|
1548 |
struct tm * const tmp;
|
|
1549 |
{
|
|
1550 |
const struct lsinfo * lp;
|
|
1551 |
long days;
|
|
1552 |
long rem;
|
|
1553 |
long y;
|
|
1554 |
int yleap;
|
|
1555 |
const int * ip;
|
|
1556 |
long corr;
|
|
1557 |
int hit;
|
|
1558 |
int i;
|
|
1559 |
|
|
1560 |
corr = 0;
|
|
1561 |
hit = 0;
|
|
1562 |
#ifdef ALL_STATE
|
|
1563 |
i = (sp == NULL) ? 0 : sp->leapcnt;
|
|
1564 |
#endif /* defined ALL_STATE */
|
|
1565 |
#ifndef ALL_STATE
|
|
1566 |
i = sp->leapcnt;
|
|
1567 |
#endif /* State Farm */
|
|
1568 |
while (--i >= 0) {
|
|
1569 |
lp = &sp->lsis[i];
|
|
1570 |
if (*timep >= lp->ls_trans) {
|
|
1571 |
if (*timep == lp->ls_trans) {
|
|
1572 |
hit = ((i == 0 && lp->ls_corr > 0) ||
|
|
1573 |
lp->ls_corr > sp->lsis[i - 1].ls_corr);
|
|
1574 |
if (hit)
|
|
1575 |
while (i > 0 &&
|
|
1576 |
sp->lsis[i].ls_trans ==
|
|
1577 |
sp->lsis[i - 1].ls_trans + 1 &&
|
|
1578 |
sp->lsis[i].ls_corr ==
|
|
1579 |
sp->lsis[i - 1].ls_corr + 1) {
|
|
1580 |
++hit;
|
|
1581 |
--i;
|
|
1582 |
}
|
|
1583 |
}
|
|
1584 |
corr = lp->ls_corr;
|
|
1585 |
break;
|
|
1586 |
}
|
|
1587 |
}
|
|
1588 |
days = *timep / SECSPERDAY;
|
|
1589 |
rem = *timep % SECSPERDAY;
|
|
1590 |
#ifdef mc68k
|
|
1591 |
if (*timep == 0x80000000) {
|
|
1592 |
/*
|
|
1593 |
** A 3B1 muffs the division on the most negative number.
|
|
1594 |
*/
|
|
1595 |
days = -24855;
|
|
1596 |
rem = -11648;
|
|
1597 |
}
|
|
1598 |
#endif /* defined mc68k */
|
|
1599 |
rem += (offset - corr);
|
|
1600 |
while (rem < 0) {
|
|
1601 |
rem += SECSPERDAY;
|
|
1602 |
--days;
|
|
1603 |
}
|
|
1604 |
while (rem >= SECSPERDAY) {
|
|
1605 |
rem -= SECSPERDAY;
|
|
1606 |
++days;
|
|
1607 |
}
|
|
1608 |
tmp->tm_hour = (int) (rem / SECSPERHOUR);
|
|
1609 |
rem = rem % SECSPERHOUR;
|
|
1610 |
tmp->tm_min = (int) (rem / SECSPERMIN);
|
|
1611 |
/*
|
|
1612 |
** A positive leap second requires a special
|
|
1613 |
** representation. This uses "... ??:59:60" et seq.
|
|
1614 |
*/
|
|
1615 |
tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
|
|
1616 |
tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
|
|
1617 |
if (tmp->tm_wday < 0)
|
|
1618 |
tmp->tm_wday += DAYSPERWEEK;
|
|
1619 |
y = EPOCH_YEAR;
|
|
1620 |
#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
|
|
1621 |
while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
|
|
1622 |
long newy;
|
|
1623 |
|
|
1624 |
newy = y + days / DAYSPERNYEAR;
|
|
1625 |
if (days < 0)
|
|
1626 |
--newy;
|
|
1627 |
days -= (newy - y) * DAYSPERNYEAR +
|
|
1628 |
LEAPS_THRU_END_OF(newy - 1) -
|
|
1629 |
LEAPS_THRU_END_OF(y - 1);
|
|
1630 |
y = newy;
|
|
1631 |
}
|
|
1632 |
tmp->tm_year = y - TM_YEAR_BASE;
|
|
1633 |
tmp->tm_yday = (int) days;
|
|
1634 |
ip = mon_lengths[yleap];
|
|
1635 |
for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
|
|
1636 |
days = days - (long) ip[tmp->tm_mon];
|
|
1637 |
tmp->tm_mday = (int) (days + 1);
|
|
1638 |
tmp->tm_isdst = 0;
|
|
1639 |
#ifdef TM_GMTOFF
|
|
1640 |
tmp->TM_GMTOFF = offset;
|
|
1641 |
#endif /* defined TM_GMTOFF */
|
|
1642 |
}
|
|
1643 |
|
|
1644 |
EXPORT_C
|
|
1645 |
char *
|
|
1646 |
ctime(timep)
|
|
1647 |
const time_t * const timep;
|
|
1648 |
{
|
|
1649 |
/*
|
|
1650 |
** Section 4.12.3.2 of X3.159-1989 requires that
|
|
1651 |
** The ctime function converts the calendar time pointed to by timer
|
|
1652 |
** to local time in the form of a string. It is equivalent to
|
|
1653 |
** asctime(localtime(timer))
|
|
1654 |
*/
|
|
1655 |
return asctime(localtime(timep));
|
|
1656 |
}
|
|
1657 |
|
|
1658 |
EXPORT_C
|
|
1659 |
wchar_t *
|
|
1660 |
wctime(timep)
|
|
1661 |
const time_t * const timep;
|
|
1662 |
{
|
|
1663 |
/*
|
|
1664 |
** Section 4.12.3.2 of X3.159-1989 requires that
|
|
1665 |
** The wctime function converts the calendar time pointed to by timer
|
|
1666 |
** to local time in the form of a wide char string. It is equivalent to
|
|
1667 |
** wasctime(localtime(timer))
|
|
1668 |
*/
|
|
1669 |
if (timep == NULL)
|
|
1670 |
return NULL;
|
|
1671 |
else
|
|
1672 |
return (wchar_t *)wasctime(localtime(timep));
|
|
1673 |
}
|
|
1674 |
|
|
1675 |
EXPORT_C
|
|
1676 |
char *
|
|
1677 |
ctime_r(timep, buf)
|
|
1678 |
const time_t * const timep;
|
|
1679 |
char * buf;
|
|
1680 |
{
|
|
1681 |
struct tm tm;
|
|
1682 |
|
|
1683 |
return asctime_r(localtime_r(timep, &tm), buf);
|
|
1684 |
}
|
|
1685 |
|
|
1686 |
/*
|
|
1687 |
** Adapted from code provided by Robert Elz, who writes:
|
|
1688 |
** The "best" way to do mktime I think is based on an idea of Bob
|
|
1689 |
** Kridle's (so its said...) from a long time ago.
|
|
1690 |
** [kridle@xinet.com as of 1996-01-16.]
|
|
1691 |
** It does a binary search of the time_t space. Since time_t's are
|
|
1692 |
** just 32 bits, its a max of 32 iterations (even at 64 bits it
|
|
1693 |
** would still be very reasonable).
|
|
1694 |
*/
|
|
1695 |
|
|
1696 |
#ifndef WRONG
|
|
1697 |
#define WRONG (-1)
|
|
1698 |
#endif /* !defined WRONG */
|
|
1699 |
|
|
1700 |
/*
|
|
1701 |
** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
|
|
1702 |
*/
|
|
1703 |
|
|
1704 |
static int
|
|
1705 |
increment_overflow(number, delta)
|
|
1706 |
int * number;
|
|
1707 |
int delta;
|
|
1708 |
{
|
|
1709 |
int number0;
|
|
1710 |
|
|
1711 |
number0 = *number;
|
|
1712 |
*number += delta;
|
|
1713 |
return (*number < number0) != (delta < 0);
|
|
1714 |
}
|
|
1715 |
|
|
1716 |
static int
|
|
1717 |
normalize_overflow(tensptr, unitsptr, base)
|
|
1718 |
int * const tensptr;
|
|
1719 |
int * const unitsptr;
|
|
1720 |
const int base;
|
|
1721 |
{
|
|
1722 |
int tensdelta;
|
|
1723 |
|
|
1724 |
tensdelta = (*unitsptr >= 0) ?
|
|
1725 |
(*unitsptr / base) :
|
|
1726 |
(-1 - (-1 - *unitsptr) / base);
|
|
1727 |
*unitsptr -= tensdelta * base;
|
|
1728 |
return increment_overflow(tensptr, tensdelta);
|
|
1729 |
}
|
|
1730 |
|
|
1731 |
static int
|
|
1732 |
tmcomp(atmp, btmp)
|
|
1733 |
const struct tm * const atmp;
|
|
1734 |
const struct tm * const btmp;
|
|
1735 |
{
|
|
1736 |
int result;
|
|
1737 |
|
|
1738 |
if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
|
|
1739 |
(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
|
|
1740 |
(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
|
|
1741 |
(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
|
|
1742 |
(result = (atmp->tm_min - btmp->tm_min)) == 0)
|
|
1743 |
result = atmp->tm_sec - btmp->tm_sec;
|
|
1744 |
return result;
|
|
1745 |
}
|
|
1746 |
|
|
1747 |
static time_t
|
|
1748 |
time2sub(tmp, funcp, offset, okayp, do_norm_secs)
|
|
1749 |
struct tm * const tmp;
|
|
1750 |
void (* const funcp)(const time_t*, long, struct tm*);
|
|
1751 |
const long offset;
|
|
1752 |
int * const okayp;
|
|
1753 |
const int do_norm_secs;
|
|
1754 |
{
|
|
1755 |
const struct state * sp;
|
|
1756 |
int dir;
|
|
1757 |
int bits;
|
|
1758 |
int i, j ;
|
|
1759 |
int saved_seconds;
|
|
1760 |
time_t newt;
|
|
1761 |
time_t t;
|
|
1762 |
struct tm yourtm, mytm;
|
|
1763 |
|
|
1764 |
*okayp = FALSE;
|
|
1765 |
yourtm = *tmp;
|
|
1766 |
if (do_norm_secs) {
|
|
1767 |
if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
|
|
1768 |
SECSPERMIN))
|
|
1769 |
return WRONG;
|
|
1770 |
}
|
|
1771 |
if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
|
|
1772 |
return WRONG;
|
|
1773 |
if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
|
|
1774 |
return WRONG;
|
|
1775 |
if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
|
|
1776 |
return WRONG;
|
|
1777 |
/*
|
|
1778 |
** Turn yourtm.tm_year into an actual year number for now.
|
|
1779 |
** It is converted back to an offset from TM_YEAR_BASE later.
|
|
1780 |
*/
|
|
1781 |
if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
|
|
1782 |
return WRONG;
|
|
1783 |
while (yourtm.tm_mday <= 0) {
|
|
1784 |
if (increment_overflow(&yourtm.tm_year, -1))
|
|
1785 |
return WRONG;
|
|
1786 |
i = yourtm.tm_year + (1 < yourtm.tm_mon);
|
|
1787 |
yourtm.tm_mday += year_lengths[isleap(i)];
|
|
1788 |
}
|
|
1789 |
while (yourtm.tm_mday > DAYSPERLYEAR) {
|
|
1790 |
i = yourtm.tm_year + (1 < yourtm.tm_mon);
|
|
1791 |
yourtm.tm_mday -= year_lengths[isleap(i)];
|
|
1792 |
if (increment_overflow(&yourtm.tm_year, 1))
|
|
1793 |
return WRONG;
|
|
1794 |
}
|
|
1795 |
for ( ; ; ) {
|
|
1796 |
i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
|
|
1797 |
if (yourtm.tm_mday <= i)
|
|
1798 |
break;
|
|
1799 |
yourtm.tm_mday -= i;
|
|
1800 |
if (++yourtm.tm_mon >= MONSPERYEAR) {
|
|
1801 |
yourtm.tm_mon = 0;
|
|
1802 |
if (increment_overflow(&yourtm.tm_year, 1))
|
|
1803 |
return WRONG;
|
|
1804 |
}
|
|
1805 |
}
|
|
1806 |
if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
|
|
1807 |
return WRONG;
|
|
1808 |
/* Don't go below 1900 for POLA */
|
|
1809 |
if (yourtm.tm_year < 0)
|
|
1810 |
return WRONG;
|
|
1811 |
if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
|
|
1812 |
saved_seconds = 0;
|
|
1813 |
else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
|
|
1814 |
/*
|
|
1815 |
** We can't set tm_sec to 0, because that might push the
|
|
1816 |
** time below the minimum representable time.
|
|
1817 |
** Set tm_sec to 59 instead.
|
|
1818 |
** This assumes that the minimum representable time is
|
|
1819 |
** not in the same minute that a leap second was deleted from,
|
|
1820 |
** which is a safer assumption than using 58 would be.
|
|
1821 |
*/
|
|
1822 |
if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
|
|
1823 |
return WRONG;
|
|
1824 |
saved_seconds = yourtm.tm_sec;
|
|
1825 |
yourtm.tm_sec = SECSPERMIN - 1;
|
|
1826 |
} else {
|
|
1827 |
saved_seconds = yourtm.tm_sec;
|
|
1828 |
yourtm.tm_sec = 0;
|
|
1829 |
}
|
|
1830 |
/*
|
|
1831 |
** Divide the search space in half
|
|
1832 |
** (this works whether time_t is signed or unsigned).
|
|
1833 |
*/
|
|
1834 |
bits = TYPE_BIT(time_t) - 1;
|
|
1835 |
/*
|
|
1836 |
** If we have more than this, we will overflow tm_year for tmcomp().
|
|
1837 |
** We should really return an error if we cannot represent it.
|
|
1838 |
*/
|
|
1839 |
if (bits > 56)
|
|
1840 |
bits = 56;
|
|
1841 |
/*
|
|
1842 |
** If time_t is signed, then 0 is just above the median,
|
|
1843 |
** assuming two's complement arithmetic.
|
|
1844 |
** If time_t is unsigned, then (1 << bits) is just above the median.
|
|
1845 |
*/
|
|
1846 |
t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
|
|
1847 |
for ( ; ; ) {
|
|
1848 |
(*funcp)(&t, offset, &mytm);
|
|
1849 |
dir = tmcomp(&mytm, &yourtm);
|
|
1850 |
if (dir != 0) {
|
|
1851 |
if (bits-- < 0)
|
|
1852 |
return WRONG;
|
|
1853 |
if (bits < 0)
|
|
1854 |
--t; /* may be needed if new t is minimal */
|
|
1855 |
else if (dir > 0)
|
|
1856 |
t -= ((time_t) 1) << bits;
|
|
1857 |
else t += ((time_t) 1) << bits;
|
|
1858 |
continue;
|
|
1859 |
}
|
|
1860 |
if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
|
|
1861 |
break;
|
|
1862 |
/*
|
|
1863 |
** Right time, wrong type.
|
|
1864 |
** Hunt for right time, right type.
|
|
1865 |
** It's okay to guess wrong since the guess
|
|
1866 |
** gets checked.
|
|
1867 |
*/
|
|
1868 |
sp = (funcp == localsub) ? lclptr : gmtptr;
|
|
1869 |
#ifdef ALL_STATE
|
|
1870 |
if (sp == NULL)
|
|
1871 |
return WRONG;
|
|
1872 |
#endif /* defined ALL_STATE */
|
|
1873 |
for (i = sp->typecnt - 1; i >= 0; --i) {
|
|
1874 |
if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
|
|
1875 |
continue;
|
|
1876 |
for (j = sp->typecnt - 1; j >= 0; --j) {
|
|
1877 |
if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
|
|
1878 |
continue;
|
|
1879 |
newt = t + sp->ttis[j].tt_gmtoff -
|
|
1880 |
sp->ttis[i].tt_gmtoff;
|
|
1881 |
(*funcp)(&newt, offset, &mytm);
|
|
1882 |
if (tmcomp(&mytm, &yourtm) != 0)
|
|
1883 |
continue;
|
|
1884 |
if (mytm.tm_isdst != yourtm.tm_isdst)
|
|
1885 |
continue;
|
|
1886 |
/*
|
|
1887 |
** We have a match.
|
|
1888 |
*/
|
|
1889 |
t = newt;
|
|
1890 |
goto label;
|
|
1891 |
}
|
|
1892 |
}
|
|
1893 |
return WRONG;
|
|
1894 |
}
|
|
1895 |
label:
|
|
1896 |
newt = t + saved_seconds;
|
|
1897 |
if ((newt < t) != (saved_seconds < 0))
|
|
1898 |
return WRONG;
|
|
1899 |
t = newt;
|
|
1900 |
(*funcp)(&t, offset, tmp);
|
|
1901 |
*okayp = TRUE;
|
|
1902 |
return t;
|
|
1903 |
}
|
|
1904 |
|
|
1905 |
static time_t
|
|
1906 |
time2(tmp, funcp, offset, okayp)
|
|
1907 |
struct tm * const tmp;
|
|
1908 |
void (* const funcp)(const time_t*, long, struct tm*);
|
|
1909 |
const long offset;
|
|
1910 |
int * const okayp;
|
|
1911 |
{
|
|
1912 |
time_t t;
|
|
1913 |
|
|
1914 |
/*
|
|
1915 |
** First try without normalization of seconds
|
|
1916 |
** (in case tm_sec contains a value associated with a leap second).
|
|
1917 |
** If that fails, try with normalization of seconds.
|
|
1918 |
*/
|
|
1919 |
t = time2sub(tmp, funcp, offset, okayp, FALSE);
|
|
1920 |
return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
|
|
1921 |
}
|
|
1922 |
|
|
1923 |
static time_t
|
|
1924 |
time1(tmp, funcp, offset)
|
|
1925 |
struct tm * const tmp;
|
|
1926 |
void (* const funcp)(const time_t *, long, struct tm *);
|
|
1927 |
const long offset;
|
|
1928 |
{
|
|
1929 |
time_t t;
|
|
1930 |
const struct state * sp;
|
|
1931 |
int samei, otheri;
|
|
1932 |
int sameind, otherind;
|
|
1933 |
int i;
|
|
1934 |
int nseen;
|
|
1935 |
int seen[TZ_MAX_TYPES];
|
|
1936 |
int types[TZ_MAX_TYPES];
|
|
1937 |
int okay;
|
|
1938 |
|
|
1939 |
/* #ifdef __SYMBIAN32__
|
|
1940 |
if (tmp->tm_isdst == 1)
|
|
1941 |
tmp->tm_isdst = 0;
|
|
1942 |
#endif //__SYMBIAN32__ */
|
|
1943 |
|
|
1944 |
if (tmp->tm_isdst > 1)
|
|
1945 |
tmp->tm_isdst = 1;
|
|
1946 |
t = time2(tmp, funcp, offset, &okay);
|
|
1947 |
#ifdef PCTS
|
|
1948 |
/*
|
|
1949 |
** PCTS code courtesy Grant Sullivan (grant@osf.org).
|
|
1950 |
*/
|
|
1951 |
if (okay)
|
|
1952 |
return t;
|
|
1953 |
if (tmp->tm_isdst < 0)
|
|
1954 |
tmp->tm_isdst = 0; /* reset to std and try again */
|
|
1955 |
#endif /* defined PCTS */
|
|
1956 |
#ifndef PCTS
|
|
1957 |
if (okay || tmp->tm_isdst < 0)
|
|
1958 |
return t;
|
|
1959 |
#endif /* !defined PCTS */
|
|
1960 |
/*
|
|
1961 |
** We're supposed to assume that somebody took a time of one type
|
|
1962 |
** and did some math on it that yielded a "struct tm" that's bad.
|
|
1963 |
** We try to divine the type they started from and adjust to the
|
|
1964 |
** type they need.
|
|
1965 |
*/
|
|
1966 |
sp = (funcp == localsub) ? lclptr : gmtptr;
|
|
1967 |
#ifdef ALL_STATE
|
|
1968 |
if (sp == NULL)
|
|
1969 |
return WRONG;
|
|
1970 |
#endif /* defined ALL_STATE */
|
|
1971 |
for (i = 0; i < sp->typecnt; ++i)
|
|
1972 |
seen[i] = FALSE;
|
|
1973 |
nseen = 0;
|
|
1974 |
for (i = sp->timecnt - 1; i >= 0; --i)
|
|
1975 |
if (!seen[sp->types[i]]) {
|
|
1976 |
seen[sp->types[i]] = TRUE;
|
|
1977 |
types[nseen++] = sp->types[i];
|
|
1978 |
}
|
|
1979 |
for (sameind = 0; sameind < nseen; ++sameind) {
|
|
1980 |
samei = types[sameind];
|
|
1981 |
if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
|
|
1982 |
continue;
|
|
1983 |
for (otherind = 0; otherind < nseen; ++otherind) {
|
|
1984 |
otheri = types[otherind];
|
|
1985 |
if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
|
|
1986 |
continue;
|
|
1987 |
tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
|
|
1988 |
sp->ttis[samei].tt_gmtoff;
|
|
1989 |
tmp->tm_isdst = !tmp->tm_isdst;
|
|
1990 |
t = time2(tmp, funcp, offset, &okay);
|
|
1991 |
if (okay)
|
|
1992 |
return t;
|
|
1993 |
tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
|
|
1994 |
sp->ttis[samei].tt_gmtoff;
|
|
1995 |
tmp->tm_isdst = !tmp->tm_isdst;
|
|
1996 |
}
|
|
1997 |
}
|
|
1998 |
return WRONG;
|
|
1999 |
}
|
|
2000 |
|
|
2001 |
EXPORT_C
|
|
2002 |
time_t
|
|
2003 |
mktime(tmp)
|
|
2004 |
struct tm * const tmp;
|
|
2005 |
{
|
|
2006 |
time_t mktime_return_value;
|
|
2007 |
struct tm local_tmp;
|
|
2008 |
#ifdef __SYMBIAN32__
|
|
2009 |
int err = 0;
|
|
2010 |
if(tmp==NULL)
|
|
2011 |
return -1;
|
|
2012 |
#endif
|
|
2013 |
local_tmp = (*tmp);
|
|
2014 |
_MUTEX_LOCK(&lcl_mutex);
|
|
2015 |
tzset_basic();
|
|
2016 |
mktime_return_value = time1(tmp, localsub, 0L);
|
|
2017 |
#ifdef __SYMBIAN32__
|
|
2018 |
if(getenv("TZ") == NULL)
|
|
2019 |
{
|
|
2020 |
tmp->tm_isdst = local_tmp.tm_isdst;
|
|
2021 |
tmp->tm_gmtoff = local_tmp.tm_gmtoff;
|
|
2022 |
tmp->tm_zone = local_tmp.tm_zone;
|
|
2023 |
|
|
2024 |
// if(mktime_return_value != WRONG) //temporay fix. Will be taken up during performance fix
|
|
2025 |
// {
|
|
2026 |
err = ConvertTime(ELocalToUtc,&mktime_return_value,tmp);
|
|
2027 |
if(err)
|
|
2028 |
{
|
|
2029 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
2030 |
return(time_t)-1;
|
|
2031 |
}
|
|
2032 |
// }
|
|
2033 |
}
|
|
2034 |
#endif
|
|
2035 |
_MUTEX_UNLOCK(&lcl_mutex);
|
|
2036 |
return(mktime_return_value);
|
|
2037 |
}
|
|
2038 |
|
|
2039 |
#ifdef STD_INSPIRED
|
|
2040 |
|
|
2041 |
#ifdef __SYMBIAN_COMPILE_UNUSED__
|
|
2042 |
time_t
|
|
2043 |
timelocal(tmp)
|
|
2044 |
struct tm * const tmp;
|
|
2045 |
{
|
|
2046 |
tmp->tm_isdst = -1; /* in case it wasn't initialized */
|
|
2047 |
return mktime(tmp);
|
|
2048 |
}
|
|
2049 |
#endif //__SYMBIAN_COMPILE_UNUSED__
|
|
2050 |
EXPORT_C
|
|
2051 |
time_t
|
|
2052 |
timegm(tmp)
|
|
2053 |
struct tm * const tmp;
|
|
2054 |
{
|
|
2055 |
tmp->tm_isdst = 0;
|
|
2056 |
return time1(tmp, gmtsub, 0L);
|
|
2057 |
}
|
|
2058 |
|
|
2059 |
#ifdef __SYMBIAN_COMPILE_UNUSED__
|
|
2060 |
time_t
|
|
2061 |
timeoff(tmp, offset)
|
|
2062 |
struct tm * const tmp;
|
|
2063 |
const long offset;
|
|
2064 |
{
|
|
2065 |
tmp->tm_isdst = 0;
|
|
2066 |
return time1(tmp, gmtsub, offset);
|
|
2067 |
}
|
|
2068 |
#endif //__SYMBIAN_COMPILE_UNUSED__
|
|
2069 |
#endif /* defined STD_INSPIRED */
|
|
2070 |
|
|
2071 |
#ifdef CMUCS
|
|
2072 |
|
|
2073 |
/*
|
|
2074 |
** The following is supplied for compatibility with
|
|
2075 |
** previous versions of the CMUCS runtime library.
|
|
2076 |
*/
|
|
2077 |
|
|
2078 |
long
|
|
2079 |
gtime(tmp)
|
|
2080 |
struct tm * const tmp;
|
|
2081 |
{
|
|
2082 |
const time_t t = mktime(tmp);
|
|
2083 |
|
|
2084 |
if (t == WRONG)
|
|
2085 |
return -1;
|
|
2086 |
return t;
|
|
2087 |
}
|
|
2088 |
|
|
2089 |
#endif /* defined CMUCS */
|
|
2090 |
|
|
2091 |
/*
|
|
2092 |
** XXX--is the below the right way to conditionalize??
|
|
2093 |
*/
|
|
2094 |
|
|
2095 |
#ifdef STD_INSPIRED
|
|
2096 |
#ifdef __SYMBIAN_COMPILE_UNUSED__
|
|
2097 |
/*
|
|
2098 |
** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
|
|
2099 |
** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
|
|
2100 |
** is not the case if we are accounting for leap seconds.
|
|
2101 |
** So, we provide the following conversion routines for use
|
|
2102 |
** when exchanging timestamps with POSIX conforming systems.
|
|
2103 |
*/
|
|
2104 |
|
|
2105 |
static long
|
|
2106 |
leapcorr(timep)
|
|
2107 |
time_t * timep;
|
|
2108 |
{
|
|
2109 |
struct state * sp;
|
|
2110 |
struct lsinfo * lp;
|
|
2111 |
int i;
|
|
2112 |
|
|
2113 |
sp = lclptr;
|
|
2114 |
i = sp->leapcnt;
|
|
2115 |
while (--i >= 0) {
|
|
2116 |
lp = &sp->lsis[i];
|
|
2117 |
if (*timep >= lp->ls_trans)
|
|
2118 |
return lp->ls_corr;
|
|
2119 |
}
|
|
2120 |
return 0;
|
|
2121 |
}
|
|
2122 |
|
|
2123 |
time_t
|
|
2124 |
time2posix(t)
|
|
2125 |
time_t t;
|
|
2126 |
{
|
|
2127 |
tzset();
|
|
2128 |
return t - leapcorr(&t);
|
|
2129 |
}
|
|
2130 |
|
|
2131 |
time_t
|
|
2132 |
posix2time(t)
|
|
2133 |
time_t t;
|
|
2134 |
{
|
|
2135 |
time_t x;
|
|
2136 |
time_t y;
|
|
2137 |
|
|
2138 |
tzset();
|
|
2139 |
/*
|
|
2140 |
** For a positive leap second hit, the result
|
|
2141 |
** is not unique. For a negative leap second
|
|
2142 |
** hit, the corresponding time doesn't exist,
|
|
2143 |
** so we return an adjacent second.
|
|
2144 |
*/
|
|
2145 |
x = t + leapcorr(&t);
|
|
2146 |
y = x - leapcorr(&x);
|
|
2147 |
if (y < t) {
|
|
2148 |
do {
|
|
2149 |
x++;
|
|
2150 |
y = x - leapcorr(&x);
|
|
2151 |
} while (y < t);
|
|
2152 |
if (t != y)
|
|
2153 |
return x - 1;
|
|
2154 |
} else if (y > t) {
|
|
2155 |
do {
|
|
2156 |
--x;
|
|
2157 |
y = x - leapcorr(&x);
|
|
2158 |
} while (y > t);
|
|
2159 |
if (t != y)
|
|
2160 |
return x + 1;
|
|
2161 |
}
|
|
2162 |
return x;
|
|
2163 |
}
|
|
2164 |
#endif //__SYMBIAN_COMPILE_UNUSED__
|
|
2165 |
#endif /* defined STD_INSPIRED */
|