Simplify get-timezone.
[sbcl.git] / src / runtime / time.c
blob0ae34c98e2a3d384542698dd8a014dceb8601606
1 /*
2 * time support routines that are easier to do in C than in Lisp
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
16 #include <stdio.h>
17 #include <time.h>
18 #include "sbcl.h"
19 #include "runtime.h"
21 #ifdef LISP_FEATURE_HPUX
22 struct tm *gmtime_r(const time_t *timer, struct tm *result);
23 struct tm *localtime_r(const time_t *timer, struct tm *result);
24 #endif
26 int get_timezone(time_t when, boolean *dst)
28 struct tm ltm, gtm;
29 int sw;
31 #ifdef LISP_FEATURE_WIN32
32 /* No _r versions on Windows, but the API documentation also
33 * doesn't warn them about being non-reentrant... So here's
34 * hoping they actually are -- once Windows grows threads
35 * this better be checked, though.
37 * The Windows versions also don't support times before the
38 * epoch, so we kludge it. */
39 if (when < 0)
40 when = 0;
41 ltm = *localtime(&when);
42 gtm = *gmtime(&when);
43 #else
44 ltm = *localtime_r(&when, &ltm);
45 gtm = *gmtime_r(&when, &gtm);
46 #endif
48 sw = (((gtm.tm_hour*60)+gtm.tm_min)*60+gtm.tm_sec) - (((ltm.tm_hour*60)+ltm.tm_min)*60+ltm.tm_sec);
49 if ((gtm.tm_wday + 1) % 7 == ltm.tm_wday)
50 sw -= 24*3600;
51 else if (gtm.tm_wday == (ltm.tm_wday + 1) % 7)
52 sw += 24*3600;
53 *dst = ltm.tm_isdst;
54 return sw;