2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #define DISABLE_SIGN_COMPARE_WARNINGS
9 #include "git-compat-util.h"
16 * This is like mktime, but without normalization of tm_wday and tm_yday.
18 time_t tm_to_time_t(const struct tm
*tm
)
20 static const int mdays
[] = {
21 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
23 int year
= tm
->tm_year
- 70;
24 int month
= tm
->tm_mon
;
25 int day
= tm
->tm_mday
;
27 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
29 if (month
< 0 || month
> 11) /* array bounds */
31 if (month
< 2 || (year
+ 2) % 4)
33 if (tm
->tm_hour
< 0 || tm
->tm_min
< 0 || tm
->tm_sec
< 0)
35 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
36 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
39 static const char *month_names
[] = {
40 "January", "February", "March", "April", "May", "June",
41 "July", "August", "September", "October", "November", "December"
44 static const char *weekday_names
[] = {
45 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
48 static time_t gm_time_t(timestamp_t time
, int tz
)
52 minutes
= tz
< 0 ? -tz
: tz
;
53 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
54 minutes
= tz
< 0 ? -minutes
: minutes
;
57 if (unsigned_add_overflows(time
, minutes
* 60))
58 die("Timestamp+tz too large: %"PRItime
" +%04d",
60 } else if (time
< -minutes
* 60)
61 die("Timestamp before Unix epoch: %"PRItime
" %04d", time
, tz
);
63 if (date_overflows(time
))
64 die("Timestamp too large for this system: %"PRItime
, time
);
69 * The "tz" thing is passed in as this strange "decimal parse of tz"
70 * thing, which means that tz -0100 is passed in as the integer -100,
71 * even though it means "sixty minutes off"
73 static struct tm
*time_to_tm(timestamp_t time
, int tz
, struct tm
*tm
)
75 time_t t
= gm_time_t(time
, tz
);
76 return gmtime_r(&t
, tm
);
79 static struct tm
*time_to_tm_local(timestamp_t time
, struct tm
*tm
)
82 return localtime_r(&t
, tm
);
86 * Fill in the localtime 'struct tm' for the supplied time,
87 * and return the local tz.
89 static int local_time_tzoffset(time_t t
, struct tm
*tm
)
95 t_local
= tm_to_time_t(tm
);
97 return 0; /* error; just use +0000 */
100 offset
= t
- t_local
;
103 offset
= t_local
- t
;
105 offset
/= 60; /* in minutes */
106 offset
= (offset
% 60) + ((offset
/ 60) * 100);
107 return offset
* eastwest
;
111 * What value of "tz" was in effect back then at "time" in the
114 static int local_tzoffset(timestamp_t time
)
118 if (date_overflows(time
))
119 die("Timestamp too large for this system: %"PRItime
, time
);
121 return local_time_tzoffset((time_t)time
, &tm
);
124 static void get_time(struct timeval
*now
)
128 x
= getenv("GIT_TEST_DATE_NOW");
130 now
->tv_sec
= atoi(x
);
134 gettimeofday(now
, NULL
);
137 void show_date_relative(timestamp_t time
, struct strbuf
*timebuf
)
143 if (now
.tv_sec
< time
) {
144 strbuf_addstr(timebuf
, _("in the future"));
147 diff
= now
.tv_sec
- time
;
150 Q_("%"PRItime
" second ago", "%"PRItime
" seconds ago", diff
), diff
);
153 /* Turn it into minutes */
154 diff
= (diff
+ 30) / 60;
157 Q_("%"PRItime
" minute ago", "%"PRItime
" minutes ago", diff
), diff
);
160 /* Turn it into hours */
161 diff
= (diff
+ 30) / 60;
164 Q_("%"PRItime
" hour ago", "%"PRItime
" hours ago", diff
), diff
);
167 /* We deal with number of days from here on */
168 diff
= (diff
+ 12) / 24;
171 Q_("%"PRItime
" day ago", "%"PRItime
" days ago", diff
), diff
);
174 /* Say weeks for the past 10 weeks or so */
177 Q_("%"PRItime
" week ago", "%"PRItime
" weeks ago", (diff
+ 3) / 7),
181 /* Say months for the past 12 months or so */
184 Q_("%"PRItime
" month ago", "%"PRItime
" months ago", (diff
+ 15) / 30),
188 /* Give years and months for 5 years or so */
190 timestamp_t totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
191 timestamp_t years
= totalmonths
/ 12;
192 timestamp_t months
= totalmonths
% 12;
194 struct strbuf sb
= STRBUF_INIT
;
195 strbuf_addf(&sb
, Q_("%"PRItime
" year", "%"PRItime
" years", years
), years
);
197 /* TRANSLATORS: "%s" is "<n> years" */
198 Q_("%s, %"PRItime
" month ago", "%s, %"PRItime
" months ago", months
),
203 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", years
), years
);
206 /* Otherwise, just years. Centuries is probably overkill. */
208 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", (diff
+ 183) / 365),
212 struct date_mode
date_mode_from_type(enum date_mode_type type
)
214 struct date_mode mode
= DATE_MODE_INIT
;
215 if (type
== DATE_STRFTIME
)
216 BUG("cannot create anonymous strftime date_mode struct");
221 static void show_date_normal(struct strbuf
*buf
, timestamp_t time
, struct tm
*tm
, int tz
, struct tm
*human_tm
, int human_tz
, int local
)
232 hide
.tz
= local
|| tz
== human_tz
;
233 hide
.year
= tm
->tm_year
== human_tm
->tm_year
;
235 if (tm
->tm_mon
== human_tm
->tm_mon
) {
236 if (tm
->tm_mday
> human_tm
->tm_mday
) {
237 /* Future date: think timezones */
238 } else if (tm
->tm_mday
== human_tm
->tm_mday
) {
239 hide
.date
= hide
.wday
= 1;
240 } else if (tm
->tm_mday
+ 5 > human_tm
->tm_mday
) {
241 /* Leave just weekday if it was a few days ago */
247 /* Show "today" times as just relative times */
249 show_date_relative(time
, buf
);
254 * Always hide seconds for human-readable.
255 * Hide timezone if showing date.
256 * Hide weekday and time if showing year.
258 * The logic here is two-fold:
259 * (a) only show details when recent enough to matter
260 * (b) keep the maximum length "similar", and in check
262 if (human_tm
->tm_year
) {
264 hide
.tz
|= !hide
.date
;
265 hide
.wday
= hide
.time
= !hide
.year
;
269 strbuf_addf(buf
, "%.3s ", weekday_names
[tm
->tm_wday
]);
271 strbuf_addf(buf
, "%.3s %d ", month_names
[tm
->tm_mon
], tm
->tm_mday
);
273 /* Do we want AM/PM depending on locale? */
275 strbuf_addf(buf
, "%02d:%02d", tm
->tm_hour
, tm
->tm_min
);
277 strbuf_addf(buf
, ":%02d", tm
->tm_sec
);
282 strbuf_addf(buf
, " %d", tm
->tm_year
+ 1900);
285 strbuf_addf(buf
, " %+05d", tz
);
288 const char *show_date(timestamp_t time
, int tz
, struct date_mode mode
)
291 struct tm tmbuf
= { 0 };
292 struct tm human_tm
= { 0 };
294 static struct strbuf timebuf
= STRBUF_INIT
;
296 if (mode
.type
== DATE_UNIX
) {
297 strbuf_reset(&timebuf
);
298 strbuf_addf(&timebuf
, "%"PRItime
, time
);
302 if (mode
.type
== DATE_HUMAN
) {
307 /* Fill in the data for "current time" in human_tz and human_tm */
308 human_tz
= local_time_tzoffset(now
.tv_sec
, &human_tm
);
312 tz
= local_tzoffset(time
);
314 if (mode
.type
== DATE_RAW
) {
315 strbuf_reset(&timebuf
);
316 strbuf_addf(&timebuf
, "%"PRItime
" %+05d", time
, tz
);
320 if (mode
.type
== DATE_RELATIVE
) {
321 strbuf_reset(&timebuf
);
322 show_date_relative(time
, &timebuf
);
327 tm
= time_to_tm_local(time
, &tmbuf
);
329 tm
= time_to_tm(time
, tz
, &tmbuf
);
331 tm
= time_to_tm(0, 0, &tmbuf
);
335 strbuf_reset(&timebuf
);
336 if (mode
.type
== DATE_SHORT
)
337 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
338 tm
->tm_mon
+ 1, tm
->tm_mday
);
339 else if (mode
.type
== DATE_ISO8601
)
340 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
344 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
346 else if (mode
.type
== DATE_ISO8601_STRICT
) {
347 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d",
351 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
353 strbuf_addch(&timebuf
, 'Z');
355 strbuf_addch(&timebuf
, tz
>= 0 ? '+' : '-');
357 strbuf_addf(&timebuf
, "%02d:%02d", tz
/ 100, tz
% 100);
359 } else if (mode
.type
== DATE_RFC2822
)
360 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
361 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
362 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
363 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
364 else if (mode
.type
== DATE_STRFTIME
)
365 strbuf_addftime(&timebuf
, mode
.strftime_fmt
, tm
, tz
,
368 show_date_normal(&timebuf
, time
, tm
, tz
, &human_tm
, human_tz
, mode
.local
);
373 * Check these. And note how it doesn't do the summer-time conversion.
375 * In my world, it's always summer, and things are probably a bit off
378 static const struct {
382 } timezone_names
[] = {
383 { "IDLW", -12, 0, }, /* International Date Line West */
384 { "NT", -11, 0, }, /* Nome */
385 { "CAT", -10, 0, }, /* Central Alaska */
386 { "HST", -10, 0, }, /* Hawaii Standard */
387 { "HDT", -10, 1, }, /* Hawaii Daylight */
388 { "YST", -9, 0, }, /* Yukon Standard */
389 { "YDT", -9, 1, }, /* Yukon Daylight */
390 { "PST", -8, 0, }, /* Pacific Standard */
391 { "PDT", -8, 1, }, /* Pacific Daylight */
392 { "MST", -7, 0, }, /* Mountain Standard */
393 { "MDT", -7, 1, }, /* Mountain Daylight */
394 { "CST", -6, 0, }, /* Central Standard */
395 { "CDT", -6, 1, }, /* Central Daylight */
396 { "EST", -5, 0, }, /* Eastern Standard */
397 { "EDT", -5, 1, }, /* Eastern Daylight */
398 { "AST", -3, 0, }, /* Atlantic Standard */
399 { "ADT", -3, 1, }, /* Atlantic Daylight */
400 { "WAT", -1, 0, }, /* West Africa */
402 { "GMT", 0, 0, }, /* Greenwich Mean */
403 { "UTC", 0, 0, }, /* Universal (Coordinated) */
404 { "Z", 0, 0, }, /* Zulu, alias for UTC */
406 { "WET", 0, 0, }, /* Western European */
407 { "BST", 0, 1, }, /* British Summer */
408 { "CET", +1, 0, }, /* Central European */
409 { "MET", +1, 0, }, /* Middle European */
410 { "MEWT", +1, 0, }, /* Middle European Winter */
411 { "MEST", +1, 1, }, /* Middle European Summer */
412 { "CEST", +1, 1, }, /* Central European Summer */
413 { "MESZ", +1, 1, }, /* Middle European Summer */
414 { "FWT", +1, 0, }, /* French Winter */
415 { "FST", +1, 1, }, /* French Summer */
416 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
417 { "EEST", +2, 1, }, /* Eastern European Daylight */
418 { "WAST", +7, 0, }, /* West Australian Standard */
419 { "WADT", +7, 1, }, /* West Australian Daylight */
420 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
421 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
422 { "EAST", +10, 0, }, /* Eastern Australian Standard */
423 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
424 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
425 { "NZT", +12, 0, }, /* New Zealand */
426 { "NZST", +12, 0, }, /* New Zealand Standard */
427 { "NZDT", +12, 1, }, /* New Zealand Daylight */
428 { "IDLE", +12, 0, }, /* International Date Line East */
431 static int match_string(const char *date
, const char *str
)
435 for (i
= 0; *date
; date
++, str
++, i
++) {
438 if (toupper(*date
) == toupper(*str
))
447 static int skip_alpha(const char *date
)
452 } while (isalpha(date
[i
]));
457 * Parse month, weekday, or timezone name
459 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
463 for (i
= 0; i
< 12; i
++) {
464 int match
= match_string(date
, month_names
[i
]);
471 for (i
= 0; i
< 7; i
++) {
472 int match
= match_string(date
, weekday_names
[i
]);
479 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
480 int match
= match_string(date
, timezone_names
[i
].name
);
481 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
482 int off
= timezone_names
[i
].offset
;
484 /* This is bogus, but we like summer */
485 off
+= timezone_names
[i
].dst
;
487 /* Only use the tz name offset if we don't have anything better */
495 if (match_string(date
, "PM") == 2) {
496 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
500 if (match_string(date
, "AM") == 2) {
501 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
505 /* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
506 if (*date
== 'T' && isdigit(date
[1]) && tm
->tm_hour
== -1) {
507 tm
->tm_min
= tm
->tm_sec
= 0;
512 return skip_alpha(date
);
515 static int set_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
517 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
518 struct tm check
= *tm
;
519 struct tm
*r
= (now_tm
? &check
: tm
);
522 r
->tm_mon
= month
- 1;
527 r
->tm_year
= now_tm
->tm_year
;
529 else if (year
>= 1970 && year
< 2100)
530 r
->tm_year
= year
- 1900;
531 else if (year
> 70 && year
< 100)
534 r
->tm_year
= year
+ 100;
540 specified
= tm_to_time_t(r
);
542 /* Be it commit time or author time, it does not make
543 * sense to specify timestamp way into the future. Make
544 * sure it is not later than ten days from now...
546 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
548 tm
->tm_mon
= r
->tm_mon
;
549 tm
->tm_mday
= r
->tm_mday
;
551 tm
->tm_year
= r
->tm_year
;
557 static int set_time(long hour
, long minute
, long second
, struct tm
*tm
)
559 /* We accept 61st second because of leap second */
560 if (0 <= hour
&& hour
<= 24 &&
561 0 <= minute
&& minute
< 60 &&
562 0 <= second
&& second
<= 60) {
571 static int is_date_known(struct tm
*tm
)
573 return tm
->tm_year
!= -1 && tm
->tm_mon
!= -1 && tm
->tm_mday
!= -1;
576 static int match_multi_number(timestamp_t num
, char c
, const char *date
,
577 char *end
, struct tm
*tm
, time_t now
)
580 struct tm
*refuse_future
;
583 num2
= strtol(end
+1, &end
, 10);
585 if (*end
== c
&& isdigit(end
[1]))
586 num3
= strtol(end
+1, &end
, 10);
593 if (set_time(num
, num2
, num3
, tm
) == 0) {
595 * If %H:%M:%S was just parsed followed by: .<num4>
596 * Consider (& discard) it as fractional second
597 * if %Y%m%d is parsed before.
599 if (*end
== '.' && isdigit(end
[1]) && is_date_known(tm
))
600 strtol(end
+ 1, &end
, 10);
610 refuse_future
= NULL
;
611 if (gmtime_r(&now
, &now_tm
))
612 refuse_future
= &now_tm
;
616 if (set_date(num
, num2
, num3
, NULL
, now
, tm
) == 0)
619 if (set_date(num
, num3
, num2
, NULL
, now
, tm
) == 0)
622 /* Our eastern European friends say dd.mm.yy[yy]
623 * is the norm there, so giving precedence to
624 * mm/dd/yy[yy] form only when separator is not '.'
627 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
629 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
630 if (set_date(num3
, num2
, num
, refuse_future
, now
, tm
) == 0)
632 /* Funny European mm.dd.yy */
634 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
642 * Have we filled in any part of the time/date yet?
643 * We just do a binary 'and' to see if the sign bit
644 * is set in all the values.
646 static inline int nodate(struct tm
*tm
)
648 return (tm
->tm_year
&
657 * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
658 * In which, hour is still unset,
659 * and minutes and second has been set to 0.
661 static inline int maybeiso8601(struct tm
*tm
)
663 return tm
->tm_hour
== -1 &&
669 * We've seen a digit. Time? Year? Date?
671 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
677 num
= parse_timestamp(date
, &end
, 10);
680 * Seconds since 1970? We trigger on that for any numbers with
681 * more than 8 digits. This is because we don't want to rule out
682 * numbers like 20070606 as a YYYYMMDD date.
684 if (num
>= 100000000 && nodate(tm
)) {
686 if (gmtime_r(&time
, tm
)) {
693 * Check for special formats: num[-.:/]num[same]num
700 if (isdigit(end
[1])) {
701 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
708 * None of the special formats? Try to guess what
709 * the number meant. We use the number of digits
710 * to make a more educated guess..
715 } while (isdigit(date
[n
]));
717 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
718 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
719 if (n
== 8 || n
== 6) {
720 unsigned int num1
= num
/ 10000;
721 unsigned int num2
= (num
% 10000) / 100;
722 unsigned int num3
= num
% 100;
724 set_date(num1
, num2
, num3
, NULL
, time(NULL
), tm
);
725 else if (n
== 6 && set_time(num1
, num2
, num3
, tm
) == 0 &&
726 *end
== '.' && isdigit(end
[1]))
727 strtoul(end
+ 1, &end
, 10);
731 /* reduced precision of ISO-8601's time: HHMM or HH */
732 if (maybeiso8601(tm
)) {
733 unsigned int num1
= num
;
734 unsigned int num2
= 0;
739 if ((n
== 4 || n
== 2) && !nodate(tm
) &&
740 set_time(num1
, num2
, 0, tm
) == 0)
743 * We thought this is an ISO-8601 time string,
744 * we set minutes and seconds to 0,
745 * turn out it isn't, rollback the change.
747 tm
->tm_min
= tm
->tm_sec
= -1;
750 /* Four-digit year or a timezone? */
752 if (num
<= 1400 && *offset
== -1) {
753 unsigned int minutes
= num
% 100;
754 unsigned int hours
= num
/ 100;
755 *offset
= hours
*60 + minutes
;
756 } else if (num
> 1900 && num
< 2100)
757 tm
->tm_year
= num
- 1900;
762 * Ignore lots of numerals. We took care of 4-digit years above.
763 * Days or months must be one or two digits.
769 * NOTE! We will give precedence to day-of-month over month or
770 * year numbers in the 1-12 range. So 05 is always "mday 5",
771 * unless we already have a mday..
773 * IOW, 01 Apr 05 parses as "April 1st, 2005".
775 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
780 /* Two-digit year? */
781 if (n
== 2 && tm
->tm_year
< 0) {
782 if (num
< 10 && tm
->tm_mday
>= 0) {
783 tm
->tm_year
= num
+ 100;
792 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
798 static int match_tz(const char *date
, int *offp
)
801 int hour
= strtoul(date
+ 1, &end
, 10);
802 int n
= end
- (date
+ 1);
810 min
= 99; /* random crap */
811 } else if (*end
== ':') {
813 min
= strtoul(end
+ 1, &end
, 10);
814 if (end
- (date
+ 1) != 5)
815 min
= 99; /* random crap */
816 } /* otherwise we parsed "hh" */
819 * Don't accept any random crap. Even though some places have
820 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
821 * UTC+14), there is something wrong if hour part is much
822 * larger than that. We might also want to check that the
823 * minutes are divisible by 15 or something too. (Offset of
824 * Kathmandu, Nepal is UTC+5:45)
826 if (min
< 60 && hour
< 24) {
827 int offset
= hour
* 60 + min
;
835 static void date_string(timestamp_t date
, int offset
, struct strbuf
*buf
)
843 strbuf_addf(buf
, "%"PRItime
" %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
847 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
848 * only when it appears not as part of any other string.
850 static int match_object_header_date(const char *date
, timestamp_t
*timestamp
, int *offset
)
856 if (*date
< '0' || '9' < *date
)
858 stamp
= parse_timestamp(date
, &end
, 10);
859 if (*end
!= ' ' || stamp
== TIME_MAX
|| (end
[1] != '+' && end
[1] != '-'))
862 ofs
= strtol(date
, &end
, 10);
863 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
865 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
874 /* timestamp of 2099-12-31T23:59:59Z, including 32 leap days */
875 static const timestamp_t timestamp_max
= (((timestamp_t
)2100 - 1970) * 365 + 32) * 24 * 60 * 60 - 1;
877 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
878 (i.e. English) day/month names, and it doesn't work correctly with %z. */
879 int parse_date_basic(const char *date
, timestamp_t
*timestamp
, int *offset
)
883 timestamp_t dummy_timestamp
;
887 timestamp
= &dummy_timestamp
;
889 offset
= &dummy_offset
;
891 memset(&tm
, 0, sizeof(tm
));
903 !match_object_header_date(date
+ 1, timestamp
, offset
))
904 return 0; /* success */
907 unsigned char c
= *date
;
909 /* Stop at end of string or newline */
914 match
= match_alpha(date
, &tm
, offset
);
916 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
917 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
918 match
= match_tz(date
, offset
);
928 /* do not use mktime(), which uses local timezone, here */
929 *timestamp
= tm_to_time_t(&tm
);
930 if (*timestamp
== -1)
936 /* gmtime_r() in match_digit() may have clobbered it */
938 temp_time
= mktime(&tm
);
939 if ((time_t)*timestamp
> temp_time
) {
940 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
942 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
947 if (*offset
> 0 && *offset
* 60 > *timestamp
)
949 if (*offset
< 0 && -*offset
* 60 > timestamp_max
- *timestamp
)
951 *timestamp
-= *offset
* 60;
954 return 0; /* success */
957 int parse_expiry_date(const char *date
, timestamp_t
*timestamp
)
961 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
963 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
965 * We take over "now" here, which usually translates
966 * to the current timestamp. This is because the user
967 * really means to expire everything that was done in
968 * the past, and by definition reflogs are the record
969 * of the past, and there is nothing from the future
972 *timestamp
= TIME_MAX
;
974 *timestamp
= approxidate_careful(date
, &errors
);
979 int parse_date(const char *date
, struct strbuf
*result
)
981 timestamp_t timestamp
;
983 if (parse_date_basic(date
, ×tamp
, &offset
))
985 date_string(timestamp
, offset
, result
);
989 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
991 if (skip_prefix(format
, "relative", end
))
992 return DATE_RELATIVE
;
993 if (skip_prefix(format
, "iso8601-strict", end
) ||
994 skip_prefix(format
, "iso-strict", end
))
995 return DATE_ISO8601_STRICT
;
996 if (skip_prefix(format
, "iso8601", end
) ||
997 skip_prefix(format
, "iso", end
))
999 if (skip_prefix(format
, "rfc2822", end
) ||
1000 skip_prefix(format
, "rfc", end
))
1001 return DATE_RFC2822
;
1002 if (skip_prefix(format
, "short", end
))
1004 if (skip_prefix(format
, "default", end
))
1006 if (skip_prefix(format
, "human", end
))
1008 if (skip_prefix(format
, "raw", end
))
1010 if (skip_prefix(format
, "unix", end
))
1012 if (skip_prefix(format
, "format", end
))
1013 return DATE_STRFTIME
;
1015 * Please update $__git_log_date_formats in
1016 * git-completion.bash when you add new formats.
1019 die("unknown date format %s", format
);
1022 void parse_date_format(const char *format
, struct date_mode
*mode
)
1026 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
1027 if (skip_prefix(format
, "auto:", &p
)) {
1028 if (isatty(1) || pager_in_use())
1034 /* historical alias */
1035 if (!strcmp(format
, "local"))
1036 format
= "default-local";
1038 mode
->type
= parse_date_type(format
, &p
);
1041 if (skip_prefix(p
, "-local", &p
))
1044 if (mode
->type
== DATE_STRFTIME
) {
1045 if (!skip_prefix(p
, ":", &p
))
1046 die("date format missing colon separator: %s", format
);
1047 mode
->strftime_fmt
= xstrdup(p
);
1049 die("unknown date format %s", format
);
1052 void date_mode_release(struct date_mode
*mode
)
1054 free((char *)mode
->strftime_fmt
);
1057 void datestamp(struct strbuf
*out
)
1061 struct tm tm
= { 0 };
1065 offset
= tm_to_time_t(localtime_r(&now
, &tm
)) - now
;
1068 date_string(now
, offset
, out
);
1072 * Relative time update (eg "2 days ago"). If we haven't set the time
1073 * yet, we need to set it from current time.
1075 static time_t update_tm(struct tm
*tm
, struct tm
*now
, time_t sec
)
1079 if (tm
->tm_mday
< 0)
1080 tm
->tm_mday
= now
->tm_mday
;
1082 tm
->tm_mon
= now
->tm_mon
;
1083 if (tm
->tm_year
< 0) {
1084 tm
->tm_year
= now
->tm_year
;
1085 if (tm
->tm_mon
> now
->tm_mon
)
1089 n
= mktime(tm
) - sec
;
1090 localtime_r(&n
, tm
);
1095 * Do we have a pending number at the end, or when
1096 * we see a new one? Let's assume it's a month day,
1097 * as in "Dec 6, 1992"
1099 static void pending_number(struct tm
*tm
, int *num
)
1105 if (tm
->tm_mday
< 0 && number
< 32)
1106 tm
->tm_mday
= number
;
1107 else if (tm
->tm_mon
< 0 && number
< 13)
1108 tm
->tm_mon
= number
-1;
1109 else if (tm
->tm_year
< 0) {
1110 if (number
> 1969 && number
< 2100)
1111 tm
->tm_year
= number
- 1900;
1112 else if (number
> 69 && number
< 100)
1113 tm
->tm_year
= number
;
1114 else if (number
< 38)
1115 tm
->tm_year
= 100 + number
;
1116 /* We screw up for number = 00 ? */
1121 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
1124 update_tm(tm
, now
, 0);
1127 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
1130 update_tm(tm
, now
, 24*60*60);
1133 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
1135 if (tm
->tm_hour
< hour
)
1136 update_tm(tm
, now
, 24*60*60);
1142 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
1144 pending_number(tm
, num
);
1145 date_time(tm
, now
, 0);
1148 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
1150 pending_number(tm
, num
);
1151 date_time(tm
, now
, 12);
1154 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
1156 pending_number(tm
, num
);
1157 date_time(tm
, now
, 17);
1160 static void date_pm(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1171 tm
->tm_hour
= (hour
% 12) + 12;
1174 static void date_am(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1185 tm
->tm_hour
= (hour
% 12);
1188 static void date_never(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1191 localtime_r(&n
, tm
);
1195 static const struct special
{
1197 void (*fn
)(struct tm
*, struct tm
*, int *);
1199 { "yesterday", date_yesterday
},
1200 { "noon", date_noon
},
1201 { "midnight", date_midnight
},
1202 { "tea", date_tea
},
1205 { "never", date_never
},
1206 { "now", date_now
},
1210 static const char *number_name
[] = {
1211 "zero", "one", "two", "three", "four",
1212 "five", "six", "seven", "eight", "nine", "ten",
1215 static const struct typelen
{
1222 { "days", 24*60*60 },
1223 { "weeks", 7*24*60*60 },
1227 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
1229 const struct typelen
*tl
;
1230 const struct special
*s
;
1231 const char *end
= date
;
1234 while (isalpha(*++end
))
1237 for (i
= 0; i
< 12; i
++) {
1238 int match
= match_string(date
, month_names
[i
]);
1246 for (s
= special
; s
->name
; s
++) {
1247 size_t len
= strlen(s
->name
);
1248 if (match_string(date
, s
->name
) == len
) {
1249 s
->fn(tm
, now
, num
);
1256 for (i
= 1; i
< 11; i
++) {
1257 size_t len
= strlen(number_name
[i
]);
1258 if (match_string(date
, number_name
[i
]) == len
) {
1264 if (match_string(date
, "last") == 4) {
1273 size_t len
= strlen(tl
->type
);
1274 if (match_string(date
, tl
->type
) >= len
-1) {
1275 update_tm(tm
, now
, tl
->length
* *num
);
1283 for (i
= 0; i
< 7; i
++) {
1284 int match
= match_string(date
, weekday_names
[i
]);
1286 int diff
, n
= *num
-1;
1289 diff
= tm
->tm_wday
- i
;
1294 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1300 if (match_string(date
, "months") >= 5) {
1302 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1303 n
= tm
->tm_mon
- *num
;
1314 if (match_string(date
, "years") >= 4) {
1315 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1316 tm
->tm_year
-= *num
;
1325 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1329 timestamp_t number
= parse_timestamp(date
, &end
, 10);
1336 if (isdigit(end
[1])) {
1337 int match
= match_multi_number(number
, *end
, date
, end
,
1340 return date
+ match
;
1344 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1345 if (date
[0] != '0' || end
- date
<= 2)
1350 static timestamp_t
approxidate_str(const char *date
,
1351 const struct timeval
*tv
,
1359 time_sec
= tv
->tv_sec
;
1360 localtime_r(&time_sec
, &tm
);
1368 unsigned char c
= *date
;
1373 pending_number(&tm
, &number
);
1374 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1379 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1381 pending_number(&tm
, &number
);
1384 return (timestamp_t
)update_tm(&tm
, &now
, 0);
1387 timestamp_t
approxidate_careful(const char *date
, int *error_ret
)
1390 timestamp_t timestamp
;
1396 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1402 return approxidate_str(date
, &tv
, error_ret
);
1405 int date_overflows(timestamp_t t
)
1409 /* If we overflowed our timestamp data type, that's bad... */
1410 if ((uintmax_t)t
>= TIME_MAX
)
1414 * ...but we also are going to feed the result to system
1415 * functions that expect time_t, which is often "signed long".
1416 * Make sure that we fit into time_t, as well.
1419 return t
!= sys
|| (t
< 1) != (sys
< 1);