File: datetime.c

package info (click to toggle)
irssi-plugin-xmpp 0.53-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,540 kB
  • sloc: ansic: 8,300; makefile: 108; perl: 58
file content (95 lines) | stat: -rw-r--r-- 2,345 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2009 Colin DIDIER
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * XEP-0082: XMPP Date and Time Profiles
 */

#ifdef __linux__
#define _XOPEN_SOURCE 
#endif

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define FORMAT  "%Y-%m-%dT%T"

#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))

static long
parse_timezone(const char *tz)
{
	const char *rfc822_timezones[][4] = {
		{ "M", NULL },			/* UTC-12 */
		{ "L", NULL },
		{ "K", NULL },
		{ "I", NULL },
		{ "H", "PST", NULL }, 		/* UTC-8 */
		{ "G", "MST", "PDT", NULL },	/* UTC-7 */
		{ "F", "CST", "MDT", NULL },	/* UTC-6 */
		{ "E", "EST", "CDT", NULL },	/* UTC-5 */
		{ "D", "EDT", NULL },		/* UTC-4 */
		{ "C", NULL },
		{ "B", NULL },
		{ "A", NULL },
		{ "Z", "UT", "GMT", NULL },	/* UTC */
		{ "N", NULL },
		{ "O", NULL },
		{ "P", NULL },
		{ "Q", NULL },
		{ "R", NULL },
		{ "S", NULL },
		{ "T", NULL },
		{ "U", NULL },
		{ "V", NULL },
		{ "W", NULL },
		{ "X", NULL },
		{ "Y", NULL },			/* UTC+12 */
		{ NULL },
	};
	unsigned int i, j;

	if ((*tz == '+' || *tz == '-') && strlen(tz) == 5) {
		i = atoi(tz);
		return ((i/100)*60 + i%100) * 60;
	}
	for (i = 0; i < nitems(rfc822_timezones); ++i)
		for (j = 0; rfc822_timezones[i][j] != NULL; ++j)
			if (strcmp(rfc822_timezones[i][j], tz) == 0)
				return (i - 12) * 3600;
	return 0;
}

time_t
xep82_datetime(const char *stamp)
{
	struct tm tm;
	long offset;
	char *s;

	memset(&tm, 0, sizeof(struct tm));
	if ((s = strptime(stamp, FORMAT, &tm)) == NULL)
		return (time_t)-1;
	/* ignore fractional second addendum */
	if (*s++ == '.')
		while (isdigit(*s)) s++;
	tm.tm_isdst = -1;
	offset = *s != '\0' ? parse_timezone(s) : 0;
	return mktime(&tm) - offset;
}