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