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