Merge git://git.bogomips.org/git-svn
[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
e99d59ff
LT
7#include "cache.h"
8
bb5799d6
JS
9/*
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
11 */
12time_t tm_to_time_t(const struct tm *tm)
ecee9d9e
ET
13{
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
16 };
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
20
21 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
22 return -1;
23 if (month < 0 || month > 11) /* array bounds */
24 return -1;
25 if (month < 2 || (year + 2) % 4)
26 day--;
27 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
28 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
29}
30
31static const char *month_names[] = {
89967023
LT
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
ecee9d9e
ET
34};
35
36static const char *weekday_names[] = {
6b7b0427 37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
ecee9d9e
ET
38};
39
9a8e35e9
LT
40static time_t gm_time_t(unsigned long time, int tz)
41{
42 int minutes;
43
44 minutes = tz < 0 ? -tz : tz;
45 minutes = (minutes / 100)*60 + (minutes % 100);
46 minutes = tz < 0 ? -minutes : minutes;
47 return time + minutes * 60;
48}
49
f80cd783
LT
50/*
51 * The "tz" thing is passed in as this strange "decimal parse of tz"
52 * thing, which means that tz -0100 is passed in as the integer -100,
53 * even though it means "sixty minutes off"
54 */
2a387043 55static struct tm *time_to_tm(unsigned long time, int tz)
f80cd783 56{
9a8e35e9 57 time_t t = gm_time_t(time, tz);
2a387043
JH
58 return gmtime(&t);
59}
60
a7b02ccf
JH
61/*
62 * What value of "tz" was in effect back then at "time" in the
63 * local timezone?
64 */
65static int local_tzoffset(unsigned long time)
66{
67 time_t t, t_local;
68 struct tm tm;
69 int offset, eastwest;
70
71 t = time;
72 localtime_r(&t, &tm);
bb5799d6 73 t_local = tm_to_time_t(&tm);
a7b02ccf
JH
74
75 if (t_local < t) {
76 eastwest = -1;
77 offset = t - t_local;
78 } else {
79 eastwest = 1;
80 offset = t_local - t;
81 }
82 offset /= 60; /* in minutes */
83 offset = (offset % 60) + ((offset / 60) * 100);
84 return offset * eastwest;
85}
86
f8493ec0 87const char *show_date(unsigned long time, int tz, enum date_mode mode)
2a387043
JH
88{
89 struct tm *tm;
90 static char timebuf[200];
91
7dff9b30
LT
92 if (mode == DATE_RAW) {
93 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
94 return timebuf;
95 }
96
f8493ec0 97 if (mode == DATE_RELATIVE) {
9a8e35e9 98 unsigned long diff;
9a8e35e9
LT
99 struct timeval now;
100 gettimeofday(&now, NULL);
da8f070c 101 if (now.tv_sec < time)
9a8e35e9 102 return "in the future";
da8f070c 103 diff = now.tv_sec - time;
9a8e35e9
LT
104 if (diff < 90) {
105 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
106 return timebuf;
107 }
108 /* Turn it into minutes */
109 diff = (diff + 30) / 60;
110 if (diff < 90) {
111 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
112 return timebuf;
113 }
114 /* Turn it into hours */
115 diff = (diff + 30) / 60;
116 if (diff < 36) {
117 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
118 return timebuf;
119 }
120 /* We deal with number of days from here on */
121 diff = (diff + 12) / 24;
122 if (diff < 14) {
123 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
124 return timebuf;
125 }
126 /* Say weeks for the past 10 weeks or so */
127 if (diff < 70) {
128 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
129 return timebuf;
130 }
131 /* Say months for the past 12 months or so */
132 if (diff < 360) {
133 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
134 return timebuf;
135 }
136 /* Else fall back on absolute format.. */
137 }
138
a7b02ccf
JH
139 if (mode == DATE_LOCAL)
140 tz = local_tzoffset(time);
141
2a387043 142 tm = time_to_tm(time, tz);
f80cd783
LT
143 if (!tm)
144 return NULL;
f8493ec0
JS
145 if (mode == DATE_SHORT)
146 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
147 tm->tm_mon + 1, tm->tm_mday);
ee8f838e
RR
148 else if (mode == DATE_ISO8601)
149 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
150 tm->tm_year + 1900,
151 tm->tm_mon + 1,
152 tm->tm_mday,
153 tm->tm_hour, tm->tm_min, tm->tm_sec,
154 tz);
73013afd
JH
155 else if (mode == DATE_RFC2822)
156 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
157 weekday_names[tm->tm_wday], tm->tm_mday,
158 month_names[tm->tm_mon], tm->tm_year + 1900,
159 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
f8493ec0 160 else
a7b02ccf 161 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
f8493ec0
JS
162 weekday_names[tm->tm_wday],
163 month_names[tm->tm_mon],
164 tm->tm_mday,
165 tm->tm_hour, tm->tm_min, tm->tm_sec,
a7b02ccf
JH
166 tm->tm_year + 1900,
167 (mode == DATE_LOCAL) ? 0 : ' ',
168 tz);
f80cd783
LT
169 return timebuf;
170}
171
89967023
LT
172/*
173 * Check these. And note how it doesn't do the summer-time conversion.
174 *
175 * In my world, it's always summer, and things are probably a bit off
176 * in other ways too.
177 */
178static const struct {
179 const char *name;
180 int offset;
5e2a78a4 181 int dst;
89967023 182} timezone_names[] = {
5e2a78a4
LT
183 { "IDLW", -12, 0, }, /* International Date Line West */
184 { "NT", -11, 0, }, /* Nome */
185 { "CAT", -10, 0, }, /* Central Alaska */
186 { "HST", -10, 0, }, /* Hawaii Standard */
187 { "HDT", -10, 1, }, /* Hawaii Daylight */
188 { "YST", -9, 0, }, /* Yukon Standard */
189 { "YDT", -9, 1, }, /* Yukon Daylight */
190 { "PST", -8, 0, }, /* Pacific Standard */
191 { "PDT", -8, 1, }, /* Pacific Daylight */
192 { "MST", -7, 0, }, /* Mountain Standard */
193 { "MDT", -7, 1, }, /* Mountain Daylight */
194 { "CST", -6, 0, }, /* Central Standard */
195 { "CDT", -6, 1, }, /* Central Daylight */
196 { "EST", -5, 0, }, /* Eastern Standard */
197 { "EDT", -5, 1, }, /* Eastern Daylight */
198 { "AST", -3, 0, }, /* Atlantic Standard */
199 { "ADT", -3, 1, }, /* Atlantic Daylight */
200 { "WAT", -1, 0, }, /* West Africa */
201
202 { "GMT", 0, 0, }, /* Greenwich Mean */
203 { "UTC", 0, 0, }, /* Universal (Coordinated) */
204
205 { "WET", 0, 0, }, /* Western European */
206 { "BST", 0, 1, }, /* British Summer */
207 { "CET", +1, 0, }, /* Central European */
208 { "MET", +1, 0, }, /* Middle European */
209 { "MEWT", +1, 0, }, /* Middle European Winter */
210 { "MEST", +1, 1, }, /* Middle European Summer */
211 { "CEST", +1, 1, }, /* Central European Summer */
212 { "MESZ", +1, 1, }, /* Middle European Summer */
213 { "FWT", +1, 0, }, /* French Winter */
214 { "FST", +1, 1, }, /* French Summer */
215 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
92e2311b 216 { "EEST", +2, 1, }, /* Eastern European Daylight */
5e2a78a4
LT
217 { "WAST", +7, 0, }, /* West Australian Standard */
218 { "WADT", +7, 1, }, /* West Australian Daylight */
219 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
220 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
221 { "EAST", +10, 0, }, /* Eastern Australian Standard */
222 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
223 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
695ed472
SD
224 { "NZT", +12, 0, }, /* New Zealand */
225 { "NZST", +12, 0, }, /* New Zealand Standard */
226 { "NZDT", +12, 1, }, /* New Zealand Daylight */
5e2a78a4 227 { "IDLE", +12, 0, }, /* International Date Line East */
89967023 228};
ecee9d9e 229
89967023 230static int match_string(const char *date, const char *str)
ecee9d9e 231{
89967023
LT
232 int i = 0;
233
234 for (i = 0; *date; date++, str++, i++) {
235 if (*date == *str)
236 continue;
237 if (toupper(*date) == toupper(*str))
238 continue;
239 if (!isalnum(*date))
240 break;
241 return 0;
242 }
243 return i;
ecee9d9e
ET
244}
245
68849b54
LT
246static int skip_alpha(const char *date)
247{
248 int i = 0;
249 do {
250 i++;
251 } while (isalpha(date[i]));
252 return i;
253}
254
89967023
LT
255/*
256* Parse month, weekday, or timezone name
257*/
258static int match_alpha(const char *date, struct tm *tm, int *offset)
ecee9d9e 259{
89967023 260 int i;
ecee9d9e 261
89967023
LT
262 for (i = 0; i < 12; i++) {
263 int match = match_string(date, month_names[i]);
264 if (match >= 3) {
265 tm->tm_mon = i;
266 return match;
ecee9d9e 267 }
89967023 268 }
ecee9d9e 269
89967023
LT
270 for (i = 0; i < 7; i++) {
271 int match = match_string(date, weekday_names[i]);
272 if (match >= 3) {
273 tm->tm_wday = i;
274 return match;
275 }
276 }
ecee9d9e 277
b4f2a6ac 278 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
89967023
LT
279 int match = match_string(date, timezone_names[i].name);
280 if (match >= 3) {
5e2a78a4
LT
281 int off = timezone_names[i].offset;
282
283 /* This is bogus, but we like summer */
284 off += timezone_names[i].dst;
285
92e2311b
LT
286 /* Only use the tz name offset if we don't have anything better */
287 if (*offset == -1)
288 *offset = 60*off;
289
89967023 290 return match;
ecee9d9e
ET
291 }
292 }
ecee9d9e 293
68849b54 294 if (match_string(date, "PM") == 2) {
18b633ca
LT
295 tm->tm_hour = (tm->tm_hour % 12) + 12;
296 return 2;
297 }
298
299 if (match_string(date, "AM") == 2) {
300 tm->tm_hour = (tm->tm_hour % 12) + 0;
68849b54
LT
301 return 2;
302 }
303
89967023 304 /* BAD CRAP */
68849b54 305 return skip_alpha(date);
89967023 306}
ecee9d9e 307
38035cf4 308static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
89967023 309{
198b0fb6 310 if (month > 0 && month < 13 && day > 0 && day < 32) {
38035cf4
JH
311 struct tm check = *tm;
312 struct tm *r = (now_tm ? &check : tm);
313 time_t specified;
314
315 r->tm_mon = month - 1;
316 r->tm_mday = day;
198b0fb6 317 if (year == -1) {
38035cf4
JH
318 if (!now_tm)
319 return 1;
320 r->tm_year = now_tm->tm_year;
89967023 321 }
38035cf4
JH
322 else if (year >= 1970 && year < 2100)
323 r->tm_year = year - 1900;
324 else if (year > 70 && year < 100)
325 r->tm_year = year;
326 else if (year < 38)
327 r->tm_year = year + 100;
328 else
198b0fb6 329 return 0;
38035cf4
JH
330 if (!now_tm)
331 return 1;
332
bb5799d6 333 specified = tm_to_time_t(r);
198b0fb6 334
38035cf4
JH
335 /* Be it commit time or author time, it does not make
336 * sense to specify timestamp way into the future. Make
337 * sure it is not later than ten days from now...
338 */
339 if (now + 10*24*3600 < specified)
340 return 0;
341 tm->tm_mon = r->tm_mon;
342 tm->tm_mday = r->tm_mday;
343 if (year != -1)
344 tm->tm_year = r->tm_year;
198b0fb6 345 return 1;
89967023 346 }
198b0fb6
LT
347 return 0;
348}
ecee9d9e 349
26a2d8ae 350static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
198b0fb6 351{
38035cf4
JH
352 time_t now;
353 struct tm now_tm;
354 struct tm *refuse_future;
198b0fb6
LT
355 long num2, num3;
356
357 num2 = strtol(end+1, &end, 10);
358 num3 = -1;
359 if (*end == c && isdigit(end[1]))
360 num3 = strtol(end+1, &end, 10);
361
362 /* Time? Date? */
89967023 363 switch (c) {
198b0fb6
LT
364 case ':':
365 if (num3 < 0)
366 num3 = 0;
367 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
368 tm->tm_hour = num;
369 tm->tm_min = num2;
370 tm->tm_sec = num3;
89967023
LT
371 break;
372 }
198b0fb6 373 return 0;
89967023
LT
374
375 case '-':
376 case '/':
38035cf4
JH
377 case '.':
378 now = time(NULL);
379 refuse_future = NULL;
380 if (gmtime_r(&now, &now_tm))
381 refuse_future = &now_tm;
382
198b0fb6
LT
383 if (num > 70) {
384 /* yyyy-mm-dd? */
38035cf4 385 if (is_date(num, num2, num3, refuse_future, now, tm))
198b0fb6
LT
386 break;
387 /* yyyy-dd-mm? */
38035cf4 388 if (is_date(num, num3, num2, refuse_future, now, tm))
89967023 389 break;
198b0fb6 390 }
38035cf4
JH
391 /* Our eastern European friends say dd.mm.yy[yy]
392 * is the norm there, so giving precedence to
393 * mm/dd/yy[yy] form only when separator is not '.'
394 */
395 if (c != '.' &&
396 is_date(num3, num, num2, refuse_future, now, tm))
397 break;
398 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
399 if (is_date(num3, num2, num, refuse_future, now, tm))
89967023 400 break;
38035cf4
JH
401 /* Funny European mm.dd.yy */
402 if (c == '.' &&
403 is_date(num3, num, num2, refuse_future, now, tm))
198b0fb6
LT
404 break;
405 return 0;
406 }
407 return end - date;
408}
409
9f2b6d29
LT
410/* Have we filled in any part of the time/date yet? */
411static inline int nodate(struct tm *tm)
412{
413 return tm->tm_year < 0 &&
414 tm->tm_mon < 0 &&
415 tm->tm_mday < 0 &&
416 !(tm->tm_hour | tm->tm_min | tm->tm_sec);
417}
418
198b0fb6 419/*
a6080a0a 420 * We've seen a digit. Time? Year? Date?
198b0fb6 421 */
26a2d8ae 422static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
198b0fb6
LT
423{
424 int n;
425 char *end;
426 unsigned long num;
427
428 num = strtoul(date, &end, 10);
429
430 /*
a1a5a634
JS
431 * Seconds since 1970? We trigger on that for any numbers with
432 * more than 8 digits. This is because we don't want to rule out
433 * numbers like 20070606 as a YYYYMMDD date.
198b0fb6 434 */
9f2b6d29 435 if (num >= 100000000 && nodate(tm)) {
198b0fb6 436 time_t time = num;
9cb480f2
JH
437 if (gmtime_r(&time, tm)) {
438 *tm_gmt = 1;
198b0fb6 439 return end - date;
9cb480f2 440 }
198b0fb6
LT
441 }
442
443 /*
38035cf4 444 * Check for special formats: num[-.:/]num[same]num
198b0fb6
LT
445 */
446 switch (*end) {
447 case ':':
38035cf4 448 case '.':
198b0fb6
LT
449 case '/':
450 case '-':
451 if (isdigit(end[1])) {
452 int match = match_multi_number(num, *end, date, end, tm);
453 if (match)
454 return match;
89967023
LT
455 }
456 }
198b0fb6
LT
457
458 /*
459 * None of the special formats? Try to guess what
460 * the number meant. We use the number of digits
461 * to make a more educated guess..
462 */
463 n = 0;
464 do {
465 n++;
466 } while (isdigit(date[n]));
467
468 /* Four-digit year or a timezone? */
469 if (n == 4) {
7122f82f 470 if (num <= 1400 && *offset == -1) {
198b0fb6
LT
471 unsigned int minutes = num % 100;
472 unsigned int hours = num / 100;
473 *offset = hours*60 + minutes;
474 } else if (num > 1900 && num < 2100)
475 tm->tm_year = num - 1900;
476 return n;
477 }
478
9f2b6d29
LT
479 /*
480 * Ignore lots of numerals. We took care of 4-digit years above.
481 * Days or months must be one or two digits.
482 */
483 if (n > 2)
484 return n;
485
198b0fb6
LT
486 /*
487 * NOTE! We will give precedence to day-of-month over month or
82f9d58a 488 * year numbers in the 1-12 range. So 05 is always "mday 5",
198b0fb6
LT
489 * unless we already have a mday..
490 *
491 * IOW, 01 Apr 05 parses as "April 1st, 2005".
492 */
493 if (num > 0 && num < 32 && tm->tm_mday < 0) {
494 tm->tm_mday = num;
495 return n;
496 }
497
498 /* Two-digit year? */
499 if (n == 2 && tm->tm_year < 0) {
500 if (num < 10 && tm->tm_mday >= 0) {
501 tm->tm_year = num + 100;
502 return n;
503 }
504 if (num >= 70) {
505 tm->tm_year = num;
506 return n;
507 }
508 }
509
510 if (num > 0 && num < 32) {
511 tm->tm_mday = num;
198b0fb6
LT
512 } else if (num > 0 && num < 13) {
513 tm->tm_mon = num-1;
514 }
a6080a0a 515
198b0fb6 516 return n;
89967023 517}
ecee9d9e 518
26a2d8ae 519static int match_tz(const char *date, int *offp)
89967023
LT
520{
521 char *end;
522 int offset = strtoul(date+1, &end, 10);
523 int min, hour;
198b0fb6 524 int n = end - date - 1;
ecee9d9e 525
89967023
LT
526 min = offset % 100;
527 hour = offset / 100;
ecee9d9e 528
198b0fb6
LT
529 /*
530 * Don't accept any random crap.. At least 3 digits, and
531 * a valid minute. We might want to check that the minutes
532 * are divisible by 30 or something too.
533 */
68849b54
LT
534 if (min < 60 && n > 2) {
535 offset = hour*60+min;
536 if (*date == '-')
537 offset = -offset;
ecee9d9e 538
68849b54
LT
539 *offp = offset;
540 }
89967023
LT
541 return end - date;
542}
ecee9d9e 543
01c6ad29
LT
544static int date_string(unsigned long date, int offset, char *buf, int len)
545{
546 int sign = '+';
547
548 if (offset < 0) {
549 offset = -offset;
550 sign = '-';
551 }
552 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
553}
554
89967023
LT
555/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
556 (i.e. English) day/month names, and it doesn't work correctly with %z. */
2a39064c 557int parse_date(const char *date, char *result, int maxlen)
89967023
LT
558{
559 struct tm tm;
01c6ad29 560 int offset, tm_gmt;
89967023 561 time_t then;
ecee9d9e 562
89967023
LT
563 memset(&tm, 0, sizeof(tm));
564 tm.tm_year = -1;
565 tm.tm_mon = -1;
566 tm.tm_mday = -1;
eaa85129
LT
567 tm.tm_isdst = -1;
568 offset = -1;
9cb480f2 569 tm_gmt = 0;
89967023
LT
570
571 for (;;) {
572 int match = 0;
573 unsigned char c = *date;
574
575 /* Stop at end of string or newline */
576 if (!c || c == '\n')
577 break;
578
579 if (isalpha(c))
580 match = match_alpha(date, &tm, &offset);
581 else if (isdigit(c))
9cb480f2 582 match = match_digit(date, &tm, &offset, &tm_gmt);
89967023
LT
583 else if ((c == '-' || c == '+') && isdigit(date[1]))
584 match = match_tz(date, &offset);
585
586 if (!match) {
587 /* BAD CRAP */
588 match = 1;
a6080a0a 589 }
89967023
LT
590
591 date += match;
592 }
ecee9d9e 593
eaa85129 594 /* mktime uses local timezone */
bb5799d6 595 then = tm_to_time_t(&tm);
eaa85129
LT
596 if (offset == -1)
597 offset = (then - mktime(&tm)) / 60;
598
ecee9d9e 599 if (then == -1)
2a39064c 600 return -1;
ecee9d9e 601
9cb480f2
JH
602 if (!tm_gmt)
603 then -= offset * 60;
01c6ad29 604 return date_string(then, offset, result, maxlen);
ecee9d9e
ET
605}
606
856665f8
AP
607enum date_mode parse_date_format(const char *format)
608{
609 if (!strcmp(format, "relative"))
610 return DATE_RELATIVE;
611 else if (!strcmp(format, "iso8601") ||
612 !strcmp(format, "iso"))
613 return DATE_ISO8601;
614 else if (!strcmp(format, "rfc2822") ||
615 !strcmp(format, "rfc"))
616 return DATE_RFC2822;
617 else if (!strcmp(format, "short"))
618 return DATE_SHORT;
619 else if (!strcmp(format, "local"))
620 return DATE_LOCAL;
621 else if (!strcmp(format, "default"))
622 return DATE_NORMAL;
7dff9b30
LT
623 else if (!strcmp(format, "raw"))
624 return DATE_RAW;
856665f8
AP
625 else
626 die("unknown date format %s", format);
627}
628
ecee9d9e
ET
629void datestamp(char *buf, int bufsize)
630{
631 time_t now;
632 int offset;
633
634 time(&now);
635
bb5799d6 636 offset = tm_to_time_t(localtime(&now)) - now;
ecee9d9e
ET
637 offset /= 60;
638
01c6ad29 639 date_string(now, offset, buf, bufsize);
ecee9d9e 640}
3c07b1d1
LT
641
642static void update_tm(struct tm *tm, unsigned long sec)
643{
644 time_t n = mktime(tm) - sec;
645 localtime_r(&n, tm);
646}
647
a8aca418
LT
648static void date_yesterday(struct tm *tm, int *num)
649{
650 update_tm(tm, 24*60*60);
651}
652
653static void date_time(struct tm *tm, int hour)
654{
655 if (tm->tm_hour < hour)
656 date_yesterday(tm, NULL);
657 tm->tm_hour = hour;
658 tm->tm_min = 0;
659 tm->tm_sec = 0;
660}
661
662static void date_midnight(struct tm *tm, int *num)
663{
664 date_time(tm, 0);
665}
666
667static void date_noon(struct tm *tm, int *num)
668{
669 date_time(tm, 12);
670}
671
672static void date_tea(struct tm *tm, int *num)
673{
674 date_time(tm, 17);
675}
676
393d340e
LT
677static void date_pm(struct tm *tm, int *num)
678{
18b633ca 679 int hour, n = *num;
393d340e
LT
680 *num = 0;
681
18b633ca
LT
682 hour = tm->tm_hour;
683 if (n) {
684 hour = n;
393d340e
LT
685 tm->tm_min = 0;
686 tm->tm_sec = 0;
687 }
18b633ca 688 tm->tm_hour = (hour % 12) + 12;
393d340e
LT
689}
690
691static void date_am(struct tm *tm, int *num)
692{
18b633ca 693 int hour, n = *num;
393d340e
LT
694 *num = 0;
695
18b633ca
LT
696 hour = tm->tm_hour;
697 if (n) {
698 hour = n;
393d340e
LT
699 tm->tm_min = 0;
700 tm->tm_sec = 0;
701 }
18b633ca 702 tm->tm_hour = (hour % 12);
393d340e
LT
703}
704
af66366a
JS
705static void date_never(struct tm *tm, int *num)
706{
8c6b5786
OM
707 time_t n = 0;
708 localtime_r(&n, tm);
af66366a
JS
709}
710
a8aca418
LT
711static const struct special {
712 const char *name;
713 void (*fn)(struct tm *, int *);
714} special[] = {
715 { "yesterday", date_yesterday },
716 { "noon", date_noon },
717 { "midnight", date_midnight },
718 { "tea", date_tea },
393d340e
LT
719 { "PM", date_pm },
720 { "AM", date_am },
af66366a 721 { "never", date_never },
a8aca418
LT
722 { NULL }
723};
724
3c07b1d1
LT
725static const char *number_name[] = {
726 "zero", "one", "two", "three", "four",
727 "five", "six", "seven", "eight", "nine", "ten",
728};
729
a8aca418 730static const struct typelen {
3c07b1d1
LT
731 const char *type;
732 int length;
733} typelen[] = {
734 { "seconds", 1 },
735 { "minutes", 60 },
736 { "hours", 60*60 },
737 { "days", 24*60*60 },
738 { "weeks", 7*24*60*60 },
739 { NULL }
a6080a0a 740};
3c07b1d1
LT
741
742static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
743{
a8aca418
LT
744 const struct typelen *tl;
745 const struct special *s;
3c07b1d1 746 const char *end = date;
5df7dbba 747 int i;
3c07b1d1 748
5df7dbba
PH
749 while (isalpha(*++end));
750 ;
3c07b1d1
LT
751
752 for (i = 0; i < 12; i++) {
753 int match = match_string(date, month_names[i]);
754 if (match >= 3) {
755 tm->tm_mon = i;
756 return end;
757 }
758 }
759
a8aca418
LT
760 for (s = special; s->name; s++) {
761 int len = strlen(s->name);
762 if (match_string(date, s->name) == len) {
763 s->fn(tm, num);
764 return end;
765 }
3c07b1d1
LT
766 }
767
768 if (!*num) {
769 for (i = 1; i < 11; i++) {
770 int len = strlen(number_name[i]);
771 if (match_string(date, number_name[i]) == len) {
772 *num = i;
773 return end;
774 }
775 }
776 if (match_string(date, "last") == 4)
777 *num = 1;
778 return end;
779 }
780
781 tl = typelen;
782 while (tl->type) {
783 int len = strlen(tl->type);
784 if (match_string(date, tl->type) >= len-1) {
785 update_tm(tm, tl->length * *num);
786 *num = 0;
787 return end;
788 }
789 tl++;
790 }
791
6b7b0427
LT
792 for (i = 0; i < 7; i++) {
793 int match = match_string(date, weekday_names[i]);
794 if (match >= 3) {
795 int diff, n = *num -1;
796 *num = 0;
797
798 diff = tm->tm_wday - i;
799 if (diff <= 0)
800 n++;
801 diff += 7*n;
802
803 update_tm(tm, diff * 24 * 60 * 60);
804 return end;
805 }
806 }
807
3c07b1d1
LT
808 if (match_string(date, "months") >= 5) {
809 int n = tm->tm_mon - *num;
810 *num = 0;
811 while (n < 0) {
812 n += 12;
813 tm->tm_year--;
814 }
815 tm->tm_mon = n;
816 return end;
817 }
818
819 if (match_string(date, "years") >= 4) {
820 tm->tm_year -= *num;
821 *num = 0;
822 return end;
823 }
824
825 return end;
826}
827
e92a54d9
LT
828static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
829{
830 char *end;
831 unsigned long number = strtoul(date, &end, 10);
832
393d340e
LT
833 switch (*end) {
834 case ':':
835 case '.':
836 case '/':
837 case '-':
838 if (isdigit(end[1])) {
839 int match = match_multi_number(number, *end, date, end, tm);
840 if (match)
841 return date + match;
842 }
843 }
844
9f2b6d29
LT
845 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
846 if (date[0] != '0' || end - date <= 2)
847 *num = number;
e92a54d9
LT
848 return end;
849}
850
3c07b1d1
LT
851unsigned long approxidate(const char *date)
852{
853 int number = 0;
854 struct tm tm, now;
855 struct timeval tv;
856 char buffer[50];
857
858 if (parse_date(date, buffer, sizeof(buffer)) > 0)
859 return strtoul(buffer, NULL, 10);
860
861 gettimeofday(&tv, NULL);
862 localtime_r(&tv.tv_sec, &tm);
863 now = tm;
864 for (;;) {
865 unsigned char c = *date;
866 if (!c)
867 break;
868 date++;
869 if (isdigit(c)) {
e92a54d9 870 date = approxidate_digit(date-1, &tm, &number);
3c07b1d1
LT
871 continue;
872 }
873 if (isalpha(c))
874 date = approxidate_alpha(date-1, &tm, &number);
875 }
876 if (number > 0 && number < 32)
877 tm.tm_mday = number;
b73cebf4 878 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
3c07b1d1
LT
879 tm.tm_year--;
880 return mktime(&tm);
881}