aboutsummaryrefslogtreecommitdiff
path: root/src/libhttpd/date.c
blob: 333495784964d03daa3344c754eddf00e10f8fb1 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <u.h>
#include <libc.h>
#include <httpd.h>

/*
 * print dates in the format
 * Wkd, DD Mon YYYY HH:MM:SS GMT
 * parse dates of formats
 * Wkd, DD Mon YYYY HH:MM:SS GMT
 * Weekday, DD-Mon-YY HH:MM:SS GMT
 * Wkd Mon ( D|DD) HH:MM:SS YYYY
 * plus anything similar
 */
static char *
weekdayname[7] =
{
	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
static char *
wdayname[7] =
{
	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};

static char *
monname[12] =
{
	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

static	int	dateindex(char*, char**, int);

static int
dtolower(int c)
{
	if(c >= 'A' && c <= 'Z')
		return c - 'A' + 'a';
	return c;
}

static int
disalpha(int c)
{
	return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z';
}

static int
disdig(int c)
{
	return c >= '0' && c <= '9';
}

int
hdatefmt(Fmt *f)
{
	Tm *tm;
	ulong t;

	t = va_arg(f->args, ulong);
	tm = gmtime(t);
	return fmtprint(f, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
		wdayname[tm->wday], tm->mday, monname[tm->mon], tm->year+1900,
		tm->hour, tm->min, tm->sec);
}

static char*
dateword(char *date, char *buf)
{
	char *p;
	int c;

	p = buf;
	while(!disalpha(c = *date) && !disdig(c) && c)
		date++;
	while(disalpha(c = *date)){
		if(p - buf < 30)
			*p++ = dtolower(c);
		date++;
	}
	*p = 0;
	return date;
}

static int
datenum(char **d)
{
	char *date;
	int c, n;

	date = *d;
	while(!disdig(c = *date) && c)
		date++;
	if(c == 0){
		*d = date;
		return -1;
	}
	n = 0;
	while(disdig(c = *date)){
		n = n * 10 + c - '0';
		date++;
	}
	*d = date;
	return n;
}

/*
 * parse a date and return the seconds since the epoch
 * return 0 for a failure
 */
ulong
hdate2sec(char *date)
{
	Tm tm;
	char buf[32];

	/*
	 * Weekday|Wday
	 */
	date = dateword(date, buf);
	tm.wday = dateindex(buf, wdayname, 7);
	if(tm.wday < 0)
		tm.wday = dateindex(buf, weekdayname, 7);
	if(tm.wday < 0)
		return 0;

	/*
	 * check for the two major formats
	 */
	date = dateword(date, buf);
	tm.mon = dateindex(buf, monname, 12);
	if(tm.mon >= 0){
		/*
		 * MM
		 */
		tm.mday = datenum(&date);
		if(tm.mday < 1 || tm.mday > 31)
			return 0;

		/*
		 * HH:MM:SS
		 */
		tm.hour = datenum(&date);
		if(tm.hour < 0 || tm.hour >= 24)
			return 0;
		tm.min = datenum(&date);
		if(tm.min < 0 || tm.min >= 60)
			return 0;
		tm.sec = datenum(&date);
		if(tm.sec < 0 || tm.sec >= 60)
			return 0;

		/*
		 * YYYY
		 */
		tm.year = datenum(&date);
		if(tm.year < 70 || tm.year > 99 && tm.year < 1970)
			return 0;
		if(tm.year >= 1970)
			tm.year -= 1900;
	}else{
		/*
		 * MM-Mon-(YY|YYYY)
		 */
		tm.mday = datenum(&date);
		if(tm.mday < 1 || tm.mday > 31)
			return 0;
		date = dateword(date, buf);
		tm.mon = dateindex(buf, monname, 12);
		if(tm.mon < 0 || tm.mon >= 12)
			return 0;
		tm.year = datenum(&date);
		if(tm.year < 70 || tm.year > 99 && tm.year < 1970)
			return 0;
		if(tm.year >= 1970)
			tm.year -= 1900;

		/*
		 * HH:MM:SS
		 */
		tm.hour = datenum(&date);
		if(tm.hour < 0 || tm.hour >= 24)
			return 0;
		tm.min = datenum(&date);
		if(tm.min < 0 || tm.min >= 60)
			return 0;
		tm.sec = datenum(&date);
		if(tm.sec < 0 || tm.sec >= 60)
			return 0;

		/*
		 * timezone
		 */
		dateword(date, buf);
		if(strncmp(buf, "gmt", 3) != 0)
			return 0;
	}

	strcpy(tm.zone, "GMT");
	tm.tzoff = 0;
	tm.yday = 0;
	return tm2sec(&tm);
}

static int
dateindex(char *d, char **tab, int n)
{
	int i;

	for(i = 0; i < n; i++)
		if(cistrcmp(d, tab[i]) == 0)
			return i;
	return -1;
}