daemon.c: fix arg parsing bugs
[git/git.git] / date.c
CommitLineData
ecee9d9e
ET
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
ecee9d9e
ET
7#include <time.h>
8
e99d59ff
LT
9#include "cache.h"
10
ecee9d9e
ET
11static time_t my_mktime(struct tm *tm)
12{
13 static const int mdays[] = {
14 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
15 };
16 int year = tm->tm_year - 70;
17 int month = tm->tm_mon;
18 int day = tm->tm_mday;
19
20 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
21 return -1;
22 if (month < 0 || month > 11) /* array bounds */
23 return -1;
24 if (month < 2 || (year + 2) % 4)
25 day--;
26 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
27 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
28}
29
30static const char *month_names[] = {
89967023
LT
31 "January", "February", "March", "April", "May", "June",
32 "July", "August", "September", "October", "November", "December"
ecee9d9e
ET
33};
34
35static const char *weekday_names[] = {
89967023 36 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
ecee9d9e
ET
37};
38
f80cd783
LT
39/*
40 * The "tz" thing is passed in as this strange "decimal parse of tz"
41 * thing, which means that tz -0100 is passed in as the integer -100,
42 * even though it means "sixty minutes off"
43 */
44const char *show_date(unsigned long time, int tz)
45{
46 struct tm *tm;
47 time_t t;
48 static char timebuf[200];
49 int minutes;
50
51 minutes = tz < 0 ? -tz : tz;
fbab835c 52 minutes = (minutes / 100)*60 + (minutes % 100);
f80cd783 53 minutes = tz < 0 ? -minutes : minutes;
fbab835c 54 t = time + minutes * 60;
f80cd783
LT
55 tm = gmtime(&t);
56 if (!tm)
57 return NULL;
58 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
59 weekday_names[tm->tm_wday],
60 month_names[tm->tm_mon],
61 tm->tm_mday,
62 tm->tm_hour, tm->tm_min, tm->tm_sec,
63 tm->tm_year + 1900, tz);
64 return timebuf;
65}
66
89967023
LT
67/*
68 * Check these. And note how it doesn't do the summer-time conversion.
69 *
70 * In my world, it's always summer, and things are probably a bit off
71 * in other ways too.
72 */
73static const struct {
74 const char *name;
75 int offset;
5e2a78a4 76 int dst;
89967023 77} timezone_names[] = {
5e2a78a4
LT
78 { "IDLW", -12, 0, }, /* International Date Line West */
79 { "NT", -11, 0, }, /* Nome */
80 { "CAT", -10, 0, }, /* Central Alaska */
81 { "HST", -10, 0, }, /* Hawaii Standard */
82 { "HDT", -10, 1, }, /* Hawaii Daylight */
83 { "YST", -9, 0, }, /* Yukon Standard */
84 { "YDT", -9, 1, }, /* Yukon Daylight */
85 { "PST", -8, 0, }, /* Pacific Standard */
86 { "PDT", -8, 1, }, /* Pacific Daylight */
87 { "MST", -7, 0, }, /* Mountain Standard */
88 { "MDT", -7, 1, }, /* Mountain Daylight */
89 { "CST", -6, 0, }, /* Central Standard */
90 { "CDT", -6, 1, }, /* Central Daylight */
91 { "EST", -5, 0, }, /* Eastern Standard */
92 { "EDT", -5, 1, }, /* Eastern Daylight */
93 { "AST", -3, 0, }, /* Atlantic Standard */
94 { "ADT", -3, 1, }, /* Atlantic Daylight */
95 { "WAT", -1, 0, }, /* West Africa */
96
97 { "GMT", 0, 0, }, /* Greenwich Mean */
98 { "UTC", 0, 0, }, /* Universal (Coordinated) */
99
100 { "WET", 0, 0, }, /* Western European */
101 { "BST", 0, 1, }, /* British Summer */
102 { "CET", +1, 0, }, /* Central European */
103 { "MET", +1, 0, }, /* Middle European */
104 { "MEWT", +1, 0, }, /* Middle European Winter */
105 { "MEST", +1, 1, }, /* Middle European Summer */
106 { "CEST", +1, 1, }, /* Central European Summer */
107 { "MESZ", +1, 1, }, /* Middle European Summer */
108 { "FWT", +1, 0, }, /* French Winter */
109 { "FST", +1, 1, }, /* French Summer */
110 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
92e2311b 111 { "EEST", +2, 1, }, /* Eastern European Daylight */
5e2a78a4
LT
112 { "WAST", +7, 0, }, /* West Australian Standard */
113 { "WADT", +7, 1, }, /* West Australian Daylight */
114 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
115 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
116 { "EAST", +10, 0, }, /* Eastern Australian Standard */
117 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
118 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
119 { "NZT", +11, 0, }, /* New Zealand */
120 { "NZST", +11, 0, }, /* New Zealand Standard */
121 { "NZDT", +11, 1, }, /* New Zealand Daylight */
122 { "IDLE", +12, 0, }, /* International Date Line East */
89967023 123};
ecee9d9e 124
89967023
LT
125#define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
126
127static int match_string(const char *date, const char *str)
ecee9d9e 128{
89967023
LT
129 int i = 0;
130
131 for (i = 0; *date; date++, str++, i++) {
132 if (*date == *str)
133 continue;
134 if (toupper(*date) == toupper(*str))
135 continue;
136 if (!isalnum(*date))
137 break;
138 return 0;
139 }
140 return i;
ecee9d9e
ET
141}
142
68849b54
LT
143static int skip_alpha(const char *date)
144{
145 int i = 0;
146 do {
147 i++;
148 } while (isalpha(date[i]));
149 return i;
150}
151
89967023
LT
152/*
153* Parse month, weekday, or timezone name
154*/
155static int match_alpha(const char *date, struct tm *tm, int *offset)
ecee9d9e 156{
89967023 157 int i;
ecee9d9e 158
89967023
LT
159 for (i = 0; i < 12; i++) {
160 int match = match_string(date, month_names[i]);
161 if (match >= 3) {
162 tm->tm_mon = i;
163 return match;
ecee9d9e 164 }
89967023 165 }
ecee9d9e 166
89967023
LT
167 for (i = 0; i < 7; i++) {
168 int match = match_string(date, weekday_names[i]);
169 if (match >= 3) {
170 tm->tm_wday = i;
171 return match;
172 }
173 }
ecee9d9e 174
89967023
LT
175 for (i = 0; i < NR_TZ; i++) {
176 int match = match_string(date, timezone_names[i].name);
177 if (match >= 3) {
5e2a78a4
LT
178 int off = timezone_names[i].offset;
179
180 /* This is bogus, but we like summer */
181 off += timezone_names[i].dst;
182
92e2311b
LT
183 /* Only use the tz name offset if we don't have anything better */
184 if (*offset == -1)
185 *offset = 60*off;
186
89967023 187 return match;
ecee9d9e
ET
188 }
189 }
ecee9d9e 190
68849b54
LT
191 if (match_string(date, "PM") == 2) {
192 if (tm->tm_hour > 0 && tm->tm_hour < 12)
193 tm->tm_hour += 12;
194 return 2;
195 }
196
89967023 197 /* BAD CRAP */
68849b54 198 return skip_alpha(date);
89967023 199}
ecee9d9e 200
198b0fb6 201static int is_date(int year, int month, int day, struct tm *tm)
89967023 202{
198b0fb6
LT
203 if (month > 0 && month < 13 && day > 0 && day < 32) {
204 if (year == -1) {
205 tm->tm_mon = month-1;
206 tm->tm_mday = day;
207 return 1;
89967023 208 }
198b0fb6
LT
209 if (year >= 1970 && year < 2100) {
210 year -= 1900;
211 } else if (year > 70 && year < 100) {
212 /* ok */
213 } else if (year < 38) {
214 year += 100;
215 } else
216 return 0;
217
218 tm->tm_mon = month-1;
219 tm->tm_mday = day;
220 tm->tm_year = year;
221 return 1;
89967023 222 }
198b0fb6
LT
223 return 0;
224}
ecee9d9e 225
26a2d8ae 226static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
198b0fb6
LT
227{
228 long num2, num3;
229
230 num2 = strtol(end+1, &end, 10);
231 num3 = -1;
232 if (*end == c && isdigit(end[1]))
233 num3 = strtol(end+1, &end, 10);
234
235 /* Time? Date? */
89967023 236 switch (c) {
198b0fb6
LT
237 case ':':
238 if (num3 < 0)
239 num3 = 0;
240 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
241 tm->tm_hour = num;
242 tm->tm_min = num2;
243 tm->tm_sec = num3;
89967023
LT
244 break;
245 }
198b0fb6 246 return 0;
89967023
LT
247
248 case '-':
249 case '/':
198b0fb6
LT
250 if (num > 70) {
251 /* yyyy-mm-dd? */
252 if (is_date(num, num2, num3, tm))
253 break;
254 /* yyyy-dd-mm? */
255 if (is_date(num, num3, num2, tm))
89967023 256 break;
198b0fb6
LT
257 }
258 /* mm/dd/yy ? */
259 if (is_date(num3, num2, num, tm))
89967023 260 break;
198b0fb6
LT
261 /* dd/mm/yy ? */
262 if (is_date(num3, num, num2, tm))
263 break;
264 return 0;
265 }
266 return end - date;
267}
268
269/*
270 * We've seen a digit. Time? Year? Date?
271 */
26a2d8ae 272static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
198b0fb6
LT
273{
274 int n;
275 char *end;
276 unsigned long num;
277
278 num = strtoul(date, &end, 10);
279
280 /*
281 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
282 */
283 if (num > 946684800) {
284 time_t time = num;
9cb480f2
JH
285 if (gmtime_r(&time, tm)) {
286 *tm_gmt = 1;
198b0fb6 287 return end - date;
9cb480f2 288 }
198b0fb6
LT
289 }
290
291 /*
292 * Check for special formats: num[:-/]num[same]num
293 */
294 switch (*end) {
295 case ':':
296 case '/':
297 case '-':
298 if (isdigit(end[1])) {
299 int match = match_multi_number(num, *end, date, end, tm);
300 if (match)
301 return match;
89967023
LT
302 }
303 }
198b0fb6
LT
304
305 /*
306 * None of the special formats? Try to guess what
307 * the number meant. We use the number of digits
308 * to make a more educated guess..
309 */
310 n = 0;
311 do {
312 n++;
313 } while (isdigit(date[n]));
314
315 /* Four-digit year or a timezone? */
316 if (n == 4) {
317 if (num <= 1200 && *offset == -1) {
318 unsigned int minutes = num % 100;
319 unsigned int hours = num / 100;
320 *offset = hours*60 + minutes;
321 } else if (num > 1900 && num < 2100)
322 tm->tm_year = num - 1900;
323 return n;
324 }
325
326 /*
327 * NOTE! We will give precedence to day-of-month over month or
328 * year numebers in the 1-12 range. So 05 is always "mday 5",
329 * unless we already have a mday..
330 *
331 * IOW, 01 Apr 05 parses as "April 1st, 2005".
332 */
333 if (num > 0 && num < 32 && tm->tm_mday < 0) {
334 tm->tm_mday = num;
335 return n;
336 }
337
338 /* Two-digit year? */
339 if (n == 2 && tm->tm_year < 0) {
340 if (num < 10 && tm->tm_mday >= 0) {
341 tm->tm_year = num + 100;
342 return n;
343 }
344 if (num >= 70) {
345 tm->tm_year = num;
346 return n;
347 }
348 }
349
350 if (num > 0 && num < 32) {
351 tm->tm_mday = num;
352 } else if (num > 1900) {
353 tm->tm_year = num - 1900;
354 } else if (num > 70) {
355 tm->tm_year = num;
356 } else if (num > 0 && num < 13) {
357 tm->tm_mon = num-1;
358 }
ecee9d9e 359
198b0fb6 360 return n;
89967023 361}
ecee9d9e 362
26a2d8ae 363static int match_tz(const char *date, int *offp)
89967023
LT
364{
365 char *end;
366 int offset = strtoul(date+1, &end, 10);
367 int min, hour;
198b0fb6 368 int n = end - date - 1;
ecee9d9e 369
89967023
LT
370 min = offset % 100;
371 hour = offset / 100;
ecee9d9e 372
198b0fb6
LT
373 /*
374 * Don't accept any random crap.. At least 3 digits, and
375 * a valid minute. We might want to check that the minutes
376 * are divisible by 30 or something too.
377 */
68849b54
LT
378 if (min < 60 && n > 2) {
379 offset = hour*60+min;
380 if (*date == '-')
381 offset = -offset;
ecee9d9e 382
68849b54
LT
383 *offp = offset;
384 }
89967023
LT
385 return end - date;
386}
ecee9d9e 387
01c6ad29
LT
388static int date_string(unsigned long date, int offset, char *buf, int len)
389{
390 int sign = '+';
391
392 if (offset < 0) {
393 offset = -offset;
394 sign = '-';
395 }
396 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
397}
398
89967023
LT
399/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
400 (i.e. English) day/month names, and it doesn't work correctly with %z. */
2a39064c 401int parse_date(const char *date, char *result, int maxlen)
89967023
LT
402{
403 struct tm tm;
01c6ad29 404 int offset, tm_gmt;
89967023 405 time_t then;
ecee9d9e 406
89967023
LT
407 memset(&tm, 0, sizeof(tm));
408 tm.tm_year = -1;
409 tm.tm_mon = -1;
410 tm.tm_mday = -1;
eaa85129
LT
411 tm.tm_isdst = -1;
412 offset = -1;
9cb480f2 413 tm_gmt = 0;
89967023
LT
414
415 for (;;) {
416 int match = 0;
417 unsigned char c = *date;
418
419 /* Stop at end of string or newline */
420 if (!c || c == '\n')
421 break;
422
423 if (isalpha(c))
424 match = match_alpha(date, &tm, &offset);
425 else if (isdigit(c))
9cb480f2 426 match = match_digit(date, &tm, &offset, &tm_gmt);
89967023
LT
427 else if ((c == '-' || c == '+') && isdigit(date[1]))
428 match = match_tz(date, &offset);
429
430 if (!match) {
431 /* BAD CRAP */
432 match = 1;
433 }
434
435 date += match;
436 }
ecee9d9e 437
eaa85129
LT
438 /* mktime uses local timezone */
439 then = my_mktime(&tm);
440 if (offset == -1)
441 offset = (then - mktime(&tm)) / 60;
442
ecee9d9e 443 if (then == -1)
2a39064c 444 return -1;
ecee9d9e 445
9cb480f2
JH
446 if (!tm_gmt)
447 then -= offset * 60;
01c6ad29 448 return date_string(then, offset, result, maxlen);
ecee9d9e
ET
449}
450
451void datestamp(char *buf, int bufsize)
452{
453 time_t now;
454 int offset;
455
456 time(&now);
457
458 offset = my_mktime(localtime(&now)) - now;
459 offset /= 60;
460
01c6ad29 461 date_string(now, offset, buf, bufsize);
ecee9d9e 462}