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