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