2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
31 * This is like mktime, but without normalization of tm_wday and tm_yday.
33 static git_time_t
tm_to_time_t(const struct tm
*tm
)
35 static const int mdays
[] = {
36 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
38 int year
= tm
->tm_year
- 70;
39 int month
= tm
->tm_mon
;
40 int day
= tm
->tm_mday
;
42 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
44 if (month
< 0 || month
> 11) /* array bounds */
46 if (month
< 2 || (year
+ 2) % 4)
48 if (tm
->tm_hour
< 0 || tm
->tm_min
< 0 || tm
->tm_sec
< 0)
50 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
51 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
54 static const char *month_names
[] = {
55 "January", "February", "March", "April", "May", "June",
56 "July", "August", "September", "October", "November", "December"
59 static const char *weekday_names
[] = {
60 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
66 * Check these. And note how it doesn't do the summer-time conversion.
68 * In my world, it's always summer, and things are probably a bit off
75 } timezone_names
[] = {
76 { "IDLW", -12, 0, }, /* International Date Line West */
77 { "NT", -11, 0, }, /* Nome */
78 { "CAT", -10, 0, }, /* Central Alaska */
79 { "HST", -10, 0, }, /* Hawaii Standard */
80 { "HDT", -10, 1, }, /* Hawaii Daylight */
81 { "YST", -9, 0, }, /* Yukon Standard */
82 { "YDT", -9, 1, }, /* Yukon Daylight */
83 { "PST", -8, 0, }, /* Pacific Standard */
84 { "PDT", -8, 1, }, /* Pacific Daylight */
85 { "MST", -7, 0, }, /* Mountain Standard */
86 { "MDT", -7, 1, }, /* Mountain Daylight */
87 { "CST", -6, 0, }, /* Central Standard */
88 { "CDT", -6, 1, }, /* Central Daylight */
89 { "EST", -5, 0, }, /* Eastern Standard */
90 { "EDT", -5, 1, }, /* Eastern Daylight */
91 { "AST", -3, 0, }, /* Atlantic Standard */
92 { "ADT", -3, 1, }, /* Atlantic Daylight */
93 { "WAT", -1, 0, }, /* West Africa */
95 { "GMT", 0, 0, }, /* Greenwich Mean */
96 { "UTC", 0, 0, }, /* Universal (Coordinated) */
97 { "Z", 0, 0, }, /* Zulu, alias for UTC */
99 { "WET", 0, 0, }, /* Western European */
100 { "BST", 0, 1, }, /* British Summer */
101 { "CET", +1, 0, }, /* Central European */
102 { "MET", +1, 0, }, /* Middle European */
103 { "MEWT", +1, 0, }, /* Middle European Winter */
104 { "MEST", +1, 1, }, /* Middle European Summer */
105 { "CEST", +1, 1, }, /* Central European Summer */
106 { "MESZ", +1, 1, }, /* Middle European Summer */
107 { "FWT", +1, 0, }, /* French Winter */
108 { "FST", +1, 1, }, /* French Summer */
109 { "EET", +2, 0, }, /* Eastern Europe */
110 { "EEST", +2, 1, }, /* Eastern European Daylight */
111 { "WAST", +7, 0, }, /* West Australian Standard */
112 { "WADT", +7, 1, }, /* West Australian Daylight */
113 { "CCT", +8, 0, }, /* China Coast */
114 { "JST", +9, 0, }, /* Japan Standard */
115 { "EAST", +10, 0, }, /* Eastern Australian Standard */
116 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
117 { "GST", +10, 0, }, /* Guam Standard */
118 { "NZT", +12, 0, }, /* New Zealand */
119 { "NZST", +12, 0, }, /* New Zealand Standard */
120 { "NZDT", +12, 1, }, /* New Zealand Daylight */
121 { "IDLE", +12, 0, }, /* International Date Line East */
124 static size_t match_string(const char *date
, const char *str
)
128 for (i
= 0; *date
; date
++, str
++, i
++) {
131 if (toupper(*date
) == toupper(*str
))
140 static int skip_alpha(const char *date
)
145 } while (isalpha(date
[i
]));
150 * Parse month, weekday, or timezone name
152 static size_t match_alpha(const char *date
, struct tm
*tm
, int *offset
)
156 for (i
= 0; i
< 12; i
++) {
157 size_t match
= match_string(date
, month_names
[i
]);
164 for (i
= 0; i
< 7; i
++) {
165 size_t match
= match_string(date
, weekday_names
[i
]);
172 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
173 size_t match
= match_string(date
, timezone_names
[i
].name
);
174 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
175 int off
= timezone_names
[i
].offset
;
177 /* This is bogus, but we like summer */
178 off
+= timezone_names
[i
].dst
;
180 /* Only use the tz name offset if we don't have anything better */
188 if (match_string(date
, "PM") == 2) {
189 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
193 if (match_string(date
, "AM") == 2) {
194 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
199 return skip_alpha(date
);
202 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
204 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
205 struct tm check
= *tm
;
206 struct tm
*r
= (now_tm
? &check
: tm
);
209 r
->tm_mon
= month
- 1;
214 r
->tm_year
= now_tm
->tm_year
;
216 else if (year
>= 1970 && year
< 2100)
217 r
->tm_year
= year
- 1900;
218 else if (year
> 70 && year
< 100)
221 r
->tm_year
= year
+ 100;
227 specified
= tm_to_time_t(r
);
229 /* Be it commit time or author time, it does not make
230 * sense to specify timestamp way into the future. Make
231 * sure it is not later than ten days from now...
233 if (now
+ 10*24*3600 < specified
)
235 tm
->tm_mon
= r
->tm_mon
;
236 tm
->tm_mday
= r
->tm_mday
;
238 tm
->tm_year
= r
->tm_year
;
244 static size_t match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
248 struct tm
*refuse_future
;
251 num2
= strtol(end
+1, &end
, 10);
253 if (*end
== c
&& isdigit(end
[1]))
254 num3
= strtol(end
+1, &end
, 10);
261 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
273 refuse_future
= NULL
;
274 if (p_gmtime_r(&now
, &now_tm
))
275 refuse_future
= &now_tm
;
279 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
282 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
285 /* Our eastern European friends say dd.mm.yy[yy]
286 * is the norm there, so giving precedence to
287 * mm/dd/yy[yy] form only when separator is not '.'
290 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
292 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
293 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
295 /* Funny European mm.dd.yy */
297 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
305 * Have we filled in any part of the time/date yet?
306 * We just do a binary 'and' to see if the sign bit
307 * is set in all the values.
309 static int nodate(struct tm
*tm
)
311 return (tm
->tm_year
&
320 * We've seen a digit. Time? Year? Date?
322 static size_t match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
328 num
= strtoul(date
, &end
, 10);
331 * Seconds since 1970? We trigger on that for any numbers with
332 * more than 8 digits. This is because we don't want to rule out
333 * numbers like 20070606 as a YYYYMMDD date.
335 if (num
>= 100000000 && nodate(tm
)) {
337 if (p_gmtime_r(&time
, tm
)) {
344 * Check for special formats: num[-.:/]num[same]num
351 if (isdigit(end
[1])) {
352 size_t match
= match_multi_number(num
, *end
, date
, end
, tm
);
359 * None of the special formats? Try to guess what
360 * the number meant. We use the number of digits
361 * to make a more educated guess..
366 } while (isdigit(date
[n
]));
368 /* Four-digit year or a timezone? */
370 if (num
<= 1400 && *offset
== -1) {
371 unsigned int minutes
= num
% 100;
372 unsigned int hours
= num
/ 100;
373 *offset
= hours
*60 + minutes
;
374 } else if (num
> 1900 && num
< 2100)
375 tm
->tm_year
= num
- 1900;
380 * Ignore lots of numerals. We took care of 4-digit years above.
381 * Days or months must be one or two digits.
387 * NOTE! We will give precedence to day-of-month over month or
388 * year numbers in the 1-12 range. So 05 is always "mday 5",
389 * unless we already have a mday..
391 * IOW, 01 Apr 05 parses as "April 1st, 2005".
393 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
398 /* Two-digit year? */
399 if (n
== 2 && tm
->tm_year
< 0) {
400 if (num
< 10 && tm
->tm_mday
>= 0) {
401 tm
->tm_year
= num
+ 100;
410 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
416 static size_t match_tz(const char *date
, int *offp
)
419 int hour
= strtoul(date
+ 1, &end
, 10);
420 size_t n
= end
- (date
+ 1);
428 min
= 99; /* random stuff */
429 } else if (*end
== ':') {
431 min
= strtoul(end
+ 1, &end
, 10);
432 if (end
- (date
+ 1) != 5)
433 min
= 99; /* random stuff */
434 } /* otherwise we parsed "hh" */
437 * Don't accept any random stuff. Even though some places have
438 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
439 * UTC+14), there is something wrong if hour part is much
440 * larger than that. We might also want to check that the
441 * minutes are divisible by 15 or something too. (Offset of
442 * Kathmandu, Nepal is UTC+5:45)
444 if (min
< 60 && hour
< 24) {
445 int offset
= hour
* 60 + min
;
454 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
455 * only when it appears not as part of any other string.
457 static int match_object_header_date(const char *date
, git_time_t
*timestamp
, int *offset
)
463 if (*date
< '0' || '9' <= *date
)
465 stamp
= strtoul(date
, &end
, 10);
466 if (*end
!= ' ' || stamp
== ULONG_MAX
|| (end
[1] != '+' && end
[1] != '-'))
469 ofs
= strtol(date
, &end
, 10);
470 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
472 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
480 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
481 (i.e. English) day/month names, and it doesn't work correctly with %z. */
482 static int parse_date_basic(const char *date
, git_time_t
*timestamp
, int *offset
)
486 git_time_t dummy_timestamp
;
490 timestamp
= &dummy_timestamp
;
492 offset
= &dummy_offset
;
494 memset(&tm
, 0, sizeof(tm
));
506 !match_object_header_date(date
+ 1, timestamp
, offset
))
507 return 0; /* success */
510 unsigned char c
= *date
;
512 /* Stop at end of string or newline */
517 match
= match_alpha(date
, &tm
, offset
);
519 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
520 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
521 match
= match_tz(date
, offset
);
531 /* mktime uses local timezone */
532 *timestamp
= tm_to_time_t(&tm
);
534 *offset
= (int)((time_t)*timestamp
- mktime(&tm
)) / 60;
536 if (*timestamp
== (git_time_t
)-1)
540 *timestamp
-= *offset
* 60;
541 return 0; /* success */
546 * Relative time update (eg "2 days ago"). If we haven't set the time
547 * yet, we need to set it from current time.
549 static git_time_t
update_tm(struct tm
*tm
, struct tm
*now
, unsigned long sec
)
554 tm
->tm_mday
= now
->tm_mday
;
556 tm
->tm_mon
= now
->tm_mon
;
557 if (tm
->tm_year
< 0) {
558 tm
->tm_year
= now
->tm_year
;
559 if (tm
->tm_mon
> now
->tm_mon
)
563 n
= mktime(tm
) - sec
;
564 p_localtime_r(&n
, tm
);
568 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
571 update_tm(tm
, now
, 0);
574 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
577 update_tm(tm
, now
, 24*60*60);
580 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
582 if (tm
->tm_hour
< hour
)
583 date_yesterday(tm
, now
, NULL
);
589 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
592 date_time(tm
, now
, 0);
595 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
598 date_time(tm
, now
, 12);
601 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
604 date_time(tm
, now
, 17);
607 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
619 tm
->tm_hour
= (hour
% 12) + 12;
622 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
634 tm
->tm_hour
= (hour
% 12);
637 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
642 p_localtime_r(&n
, tm
);
645 static const struct special
{
647 void (*fn
)(struct tm
*, struct tm
*, int *);
649 { "yesterday", date_yesterday
},
650 { "noon", date_noon
},
651 { "midnight", date_midnight
},
655 { "never", date_never
},
660 static const char *number_name
[] = {
661 "zero", "one", "two", "three", "four",
662 "five", "six", "seven", "eight", "nine", "ten",
665 static const struct typelen
{
672 { "days", 24*60*60 },
673 { "weeks", 7*24*60*60 },
677 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
679 const struct typelen
*tl
;
680 const struct special
*s
;
681 const char *end
= date
;
684 while (isalpha(*++end
))
685 /* scan to non-alpha */;
687 for (i
= 0; i
< 12; i
++) {
688 size_t match
= match_string(date
, month_names
[i
]);
696 for (s
= special
; s
->name
; s
++) {
697 size_t len
= strlen(s
->name
);
698 if (match_string(date
, s
->name
) == len
) {
706 for (i
= 1; i
< 11; i
++) {
707 size_t len
= strlen(number_name
[i
]);
708 if (match_string(date
, number_name
[i
]) == len
) {
714 if (match_string(date
, "last") == 4) {
723 size_t len
= strlen(tl
->type
);
724 if (match_string(date
, tl
->type
) >= len
-1) {
725 update_tm(tm
, now
, tl
->length
* *num
);
733 for (i
= 0; i
< 7; i
++) {
734 size_t match
= match_string(date
, weekday_names
[i
]);
736 int diff
, n
= *num
-1;
739 diff
= tm
->tm_wday
- i
;
744 update_tm(tm
, now
, diff
* 24 * 60 * 60);
750 if (match_string(date
, "months") >= 5) {
752 update_tm(tm
, now
, 0); /* fill in date fields if needed */
753 n
= tm
->tm_mon
- *num
;
764 if (match_string(date
, "years") >= 4) {
765 update_tm(tm
, now
, 0); /* fill in date fields if needed */
775 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
)
778 unsigned long number
= strtoul(date
, &end
, 10);
785 if (isdigit(end
[1])) {
786 size_t match
= match_multi_number(number
, *end
, date
, end
, tm
);
792 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
793 if (date
[0] != '0' || end
- date
<= 2)
799 * Do we have a pending number at the end, or when
800 * we see a new one? Let's assume it's a month day,
801 * as in "Dec 6, 1992"
803 static void pending_number(struct tm
*tm
, int *num
)
809 if (tm
->tm_mday
< 0 && number
< 32)
810 tm
->tm_mday
= number
;
811 else if (tm
->tm_mon
< 0 && number
< 13)
812 tm
->tm_mon
= number
-1;
813 else if (tm
->tm_year
< 0) {
814 if (number
> 1969 && number
< 2100)
815 tm
->tm_year
= number
- 1900;
816 else if (number
> 69 && number
< 100)
817 tm
->tm_year
= number
;
818 else if (number
< 38)
819 tm
->tm_year
= 100 + number
;
820 /* We mess up for number = 00 ? */
825 static git_time_t
approxidate_str(const char *date
,
831 struct tm tm
= {0}, now
;
833 p_localtime_r(&time_sec
, &tm
);
841 unsigned char c
= *date
;
846 pending_number(&tm
, &number
);
847 date
= approxidate_digit(date
-1, &tm
, &number
);
852 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
854 pending_number(&tm
, &number
);
857 return update_tm(&tm
, &now
, 0);
860 int git__date_parse(git_time_t
*out
, const char *date
)
863 git_time_t timestamp
;
864 int offset
, error_ret
=0;
866 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
871 if (time(&time_sec
) == -1)
874 *out
= approxidate_str(date
, time_sec
, &error_ret
);