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