Debian Codegraph @

src/core/xep/datetime.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2009 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 /*
19 * XEP-0082: XMPP Date and Time Profiles
20 */
21
22 #ifdef __linux__
23 #define _XOPEN_SOURCE
24 #endif
25
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #define FORMAT "%Y-%m-%dT%T"
32
33 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
34
35 static long
36 parse_timezone(const char *tz)
37 {
38 const char *rfc822_timezones[][4] = {
39 { "M", NULL }, /* UTC-12 */
40 { "L", NULL },
41 { "K", NULL },
42 { "I", NULL },
43 { "H", "PST", NULL }, /* UTC-8 */
44 { "G", "MST", "PDT", NULL }, /* UTC-7 */
45 { "F", "CST", "MDT", NULL }, /* UTC-6 */
46 { "E", "EST", "CDT", NULL }, /* UTC-5 */
47 { "D", "EDT", NULL }, /* UTC-4 */
48 { "C", NULL },
49 { "B", NULL },
50 { "A", NULL },
51 { "Z", "UT", "GMT", NULL }, /* UTC */
52 { "N", NULL },
53 { "O", NULL },
54 { "P", NULL },
55 { "Q", NULL },
56 { "R", NULL },
57 { "S", NULL },
58 { "T", NULL },
59 { "U", NULL },
60 { "V", NULL },
61 { "W", NULL },
62 { "X", NULL },
63 { "Y", NULL }, /* UTC+12 */
64 { NULL },
65 };
66 unsigned int i, j;
67
68 if ((*tz == '+' || *tz == '-') && strlen(tz) == 5) {
69 i = atoi(tz);
70 return ((i/100)*60 + i%100) * 60;
71 }
72 for (i = 0; i < nitems(rfc822_timezones); ++i)
73 for (j = 0; rfc822_timezones[i][j] != NULL; ++j)
74 if (strcmp(rfc822_timezones[i][j], tz) == 0)
75 return (i - 12) * 3600;
76 return 0;
77 }
78
79 time_t
80 xep82_datetime(const char *stamp)
81 {
82 struct tm tm;
83 long offset;
84 char *s;
85
86 memset(&tm, 0, sizeof(struct tm));
87 if ((s = strptime(stamp, FORMAT, &tm)) == NULL)
88 return (time_t)-1;
89 /* ignore fractional second addendum */
90 if (*s++ == '.')
91 while (isdigit(*s)) s++;
92 tm.tm_isdst = -1;
93 offset = *s != '\0' ? parse_timezone(s) : 0;
94 return mktime(&tm) - offset;
95 }