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