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