Add isds_box_state_period structure
[libisds.git] / src / unix.c
blob9e10f697293878b91d9752062ce497ca125b0c03
1 #include "isds_priv.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5 #include "isds.h"
6 #include "utils.h"
8 static char *tz_orig; /* Copy of original TZ variable */
10 #if HAVE_LIBCURL
11 /* Convert UTF-8 @string representation of ISO 8601 date to @time.
12 * XXX: Not all ISO formats are supported */
13 _hidden isds_error _isds_datestring2tm(const xmlChar *string, struct tm *time) {
14 char *offset;
15 if (!string || !time) return IE_INVAL;
17 /* xsd:date is ISO 8601 string, thus ASCII */
18 offset = strptime((char*)string, "%Y-%m-%d", time);
19 if (offset && *offset == '\0')
20 return IE_SUCCESS;
22 offset = strptime((char*)string, "%Y%m%d", time);
23 if (offset && *offset == '\0')
24 return IE_SUCCESS;
26 offset = strptime((char*)string, "%Y-%j", time);
27 if (offset && *offset == '\0')
28 return IE_SUCCESS;
30 return IE_NOTSUP;
32 #endif
34 /* Switches time zone to UTC.
35 * XXX: This is not reentrant and not thread-safe */
36 static void _isds_switch_tz_to_utc(void) {
37 char *tz;
39 tz = getenv("TZ");
40 if (tz) {
41 tz_orig = strdup(tz);
42 if (!tz_orig)
43 PANIC("Can not back original time zone up");
44 } else {
45 tz_orig = NULL;
48 if (setenv("TZ", "", 1))
49 PANIC("Can not change time zone to UTC temporarily");
51 tzset();
55 /* Switches time zone to original value.
56 * XXX: This is not reentrant and not thread-safe */
57 static void _isds_switch_tz_to_native(void) {
58 if (tz_orig) {
59 if (setenv("TZ", tz_orig, 1))
60 PANIC("Can not restore time zone by setting TZ variable");
61 free(tz_orig);
62 tz_orig = NULL;
63 } else {
64 if(unsetenv("TZ"))
65 PANIC("Can not restore time zone by unsetting TZ variable");
67 tzset();
70 /* Convert UTC broken time to time_t.
71 * @broken_utc it time in UTC in broken format. Despite its content is not
72 * touched, it'sw not-const because underlying POSIX function has non-const
73 * signature.
74 * @return (time_t) -1 in case of error */
75 _hidden time_t _isds_timegm(struct tm *broken_utc) {
76 time_t ret;
78 _isds_switch_tz_to_utc();
79 ret = mktime(broken_utc);
80 _isds_switch_tz_to_native();
82 return ret;