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