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