date.c: split up dst information in the timezone table
[git/git.git] / date.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <time.h>
12
13 static time_t my_mktime(struct tm *tm)
14 {
15 static const int mdays[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 };
18 int year = tm->tm_year - 70;
19 int month = tm->tm_mon;
20 int day = tm->tm_mday;
21
22 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
23 return -1;
24 if (month < 0 || month > 11) /* array bounds */
25 return -1;
26 if (month < 2 || (year + 2) % 4)
27 day--;
28 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
29 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
30 }
31
32 static const char *month_names[] = {
33 "January", "February", "March", "April", "May", "June",
34 "July", "August", "September", "October", "November", "December"
35 };
36
37 static const char *weekday_names[] = {
38 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
39 };
40
41 /*
42 * Check these. And note how it doesn't do the summer-time conversion.
43 *
44 * In my world, it's always summer, and things are probably a bit off
45 * in other ways too.
46 */
47 static const struct {
48 const char *name;
49 int offset;
50 int dst;
51 } timezone_names[] = {
52 { "IDLW", -12, 0, }, /* International Date Line West */
53 { "NT", -11, 0, }, /* Nome */
54 { "CAT", -10, 0, }, /* Central Alaska */
55 { "HST", -10, 0, }, /* Hawaii Standard */
56 { "HDT", -10, 1, }, /* Hawaii Daylight */
57 { "YST", -9, 0, }, /* Yukon Standard */
58 { "YDT", -9, 1, }, /* Yukon Daylight */
59 { "PST", -8, 0, }, /* Pacific Standard */
60 { "PDT", -8, 1, }, /* Pacific Daylight */
61 { "MST", -7, 0, }, /* Mountain Standard */
62 { "MDT", -7, 1, }, /* Mountain Daylight */
63 { "CST", -6, 0, }, /* Central Standard */
64 { "CDT", -6, 1, }, /* Central Daylight */
65 { "EST", -5, 0, }, /* Eastern Standard */
66 { "EDT", -5, 1, }, /* Eastern Daylight */
67 { "AST", -3, 0, }, /* Atlantic Standard */
68 { "ADT", -3, 1, }, /* Atlantic Daylight */
69 { "WAT", -1, 0, }, /* West Africa */
70
71 { "GMT", 0, 0, }, /* Greenwich Mean */
72 { "UTC", 0, 0, }, /* Universal (Coordinated) */
73
74 { "WET", 0, 0, }, /* Western European */
75 { "BST", 0, 1, }, /* British Summer */
76 { "CET", +1, 0, }, /* Central European */
77 { "MET", +1, 0, }, /* Middle European */
78 { "MEWT", +1, 0, }, /* Middle European Winter */
79 { "MEST", +1, 1, }, /* Middle European Summer */
80 { "CEST", +1, 1, }, /* Central European Summer */
81 { "MESZ", +1, 1, }, /* Middle European Summer */
82 { "FWT", +1, 0, }, /* French Winter */
83 { "FST", +1, 1, }, /* French Summer */
84 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
85 { "WAST", +7, 0, }, /* West Australian Standard */
86 { "WADT", +7, 1, }, /* West Australian Daylight */
87 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
88 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
89 { "EAST", +10, 0, }, /* Eastern Australian Standard */
90 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
91 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
92 { "NZT", +11, 0, }, /* New Zealand */
93 { "NZST", +11, 0, }, /* New Zealand Standard */
94 { "NZDT", +11, 1, }, /* New Zealand Daylight */
95 { "IDLE", +12, 0, }, /* International Date Line East */
96 };
97
98 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
99
100 static int match_string(const char *date, const char *str)
101 {
102 int i = 0;
103
104 for (i = 0; *date; date++, str++, i++) {
105 if (*date == *str)
106 continue;
107 if (toupper(*date) == toupper(*str))
108 continue;
109 if (!isalnum(*date))
110 break;
111 return 0;
112 }
113 return i;
114 }
115
116 /*
117 * Parse month, weekday, or timezone name
118 */
119 static int match_alpha(const char *date, struct tm *tm, int *offset)
120 {
121 int i;
122
123 for (i = 0; i < 12; i++) {
124 int match = match_string(date, month_names[i]);
125 if (match >= 3) {
126 tm->tm_mon = i;
127 return match;
128 }
129 }
130
131 for (i = 0; i < 7; i++) {
132 int match = match_string(date, weekday_names[i]);
133 if (match >= 3) {
134 tm->tm_wday = i;
135 return match;
136 }
137 }
138
139 for (i = 0; i < NR_TZ; i++) {
140 int match = match_string(date, timezone_names[i].name);
141 if (match >= 3) {
142 int off = timezone_names[i].offset;
143
144 /* This is bogus, but we like summer */
145 off += timezone_names[i].dst;
146
147 *offset = 60*off;
148 return match;
149 }
150 }
151
152 /* BAD CRAP */
153 return 0;
154 }
155
156 static int match_digit(char *date, struct tm *tm, int *offset)
157 {
158 char *end, c;
159 unsigned long num, num2, num3;
160
161 num = strtoul(date, &end, 10);
162
163 /* Time? num:num[:num] */
164 if (num < 24 && end[0] == ':' && isdigit(end[1])) {
165 tm->tm_hour = num;
166 num = strtoul(end+1, &end, 10);
167 if (num < 60) {
168 tm->tm_min = num;
169 if (end[0] == ':' && isdigit(end[1])) {
170 num = strtoul(end+1, &end, 10);
171 if (num < 61)
172 tm->tm_sec = num;
173 }
174 }
175 return end - date;
176 }
177
178 /* Year? Day of month? Numeric date-string?*/
179 c = *end;
180 switch (c) {
181 default:
182 if (num > 0 && num < 32) {
183 tm->tm_mday = num;
184 break;
185 }
186 if (num > 1900) {
187 tm->tm_year = num - 1900;
188 break;
189 }
190 if (num > 70) {
191 tm->tm_year = num;
192 break;
193 }
194 break;
195
196 case '-':
197 case '/':
198 if (num && num < 32 && isdigit(end[1])) {
199 num2 = strtoul(end+1, &end, 10);
200 if (!num2 || num2 > 31)
201 break;
202 if (num > 12) {
203 if (num2 > 12)
204 break;
205 num3 = num;
206 num = num2;
207 num2 = num3;
208 }
209 tm->tm_mon = num - 1;
210 tm->tm_mday = num2;
211 if (*end == c && isdigit(end[1])) {
212 num3 = strtoul(end+1, &end, 10);
213 if (num3 > 1900)
214 num3 -= 1900;
215 else if (num3 < 38)
216 num3 += 100;
217 tm->tm_year = num3;
218 }
219 break;
220 }
221 }
222
223 return end - date;
224
225 }
226
227 static int match_tz(char *date, int *offp)
228 {
229 char *end;
230 int offset = strtoul(date+1, &end, 10);
231 int min, hour;
232
233 min = offset % 100;
234 hour = offset / 100;
235
236 offset = hour*60+min;
237 if (*date == '-')
238 offset = -offset;
239
240 *offp = offset;
241 return end - date;
242 }
243
244 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
245 (i.e. English) day/month names, and it doesn't work correctly with %z. */
246 void parse_date(char *date, char *result, int maxlen)
247 {
248 struct tm tm;
249 int offset;
250 time_t then;
251
252 memset(&tm, 0, sizeof(tm));
253 tm.tm_year = -1;
254 tm.tm_mon = -1;
255 tm.tm_mday = -1;
256 tm.tm_isdst = -1;
257 offset = -1;
258
259 for (;;) {
260 int match = 0;
261 unsigned char c = *date;
262
263 /* Stop at end of string or newline */
264 if (!c || c == '\n')
265 break;
266
267 if (isalpha(c))
268 match = match_alpha(date, &tm, &offset);
269 else if (isdigit(c))
270 match = match_digit(date, &tm, &offset);
271 else if ((c == '-' || c == '+') && isdigit(date[1]))
272 match = match_tz(date, &offset);
273
274 if (!match) {
275 /* BAD CRAP */
276 match = 1;
277 }
278
279 date += match;
280 }
281
282 /* mktime uses local timezone */
283 then = my_mktime(&tm);
284 if (offset == -1)
285 offset = (then - mktime(&tm)) / 60;
286
287 if (then == -1)
288 return;
289
290 then -= offset * 60;
291
292 snprintf(result, maxlen, "%lu %+03d%02d", then, offset/60, offset % 60);
293 }
294
295 void datestamp(char *buf, int bufsize)
296 {
297 time_t now;
298 int offset;
299
300 time(&now);
301
302 offset = my_mktime(localtime(&now)) - now;
303 offset /= 60;
304
305 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);
306 }