2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #include "git-compat-util.h"
14 * This is like mktime, but without normalization of tm_wday and tm_yday.
16 time_t tm_to_time_t(const struct tm
*tm
)
18 static const int mdays
[] = {
19 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
21 int year
= tm
->tm_year
- 70;
22 int month
= tm
->tm_mon
;
23 int day
= tm
->tm_mday
;
25 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
27 if (month
< 0 || month
> 11) /* array bounds */
29 if (month
< 2 || (year
+ 2) % 4)
31 if (tm
->tm_hour
< 0 || tm
->tm_min
< 0 || tm
->tm_sec
< 0)
33 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
34 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
37 static const char *month_names
[] = {
38 "January", "February", "March", "April", "May", "June",
39 "July", "August", "September", "October", "November", "December"
42 static const char *weekday_names
[] = {
43 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
46 static time_t gm_time_t(timestamp_t time
, int tz
)
50 minutes
= tz
< 0 ? -tz
: tz
;
51 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
52 minutes
= tz
< 0 ? -minutes
: minutes
;
55 if (unsigned_add_overflows(time
, minutes
* 60))
56 die("Timestamp+tz too large: %"PRItime
" +%04d",
58 } else if (time
< -minutes
* 60)
59 die("Timestamp before Unix epoch: %"PRItime
" %04d", time
, tz
);
61 if (date_overflows(time
))
62 die("Timestamp too large for this system: %"PRItime
, time
);
67 * The "tz" thing is passed in as this strange "decimal parse of tz"
68 * thing, which means that tz -0100 is passed in as the integer -100,
69 * even though it means "sixty minutes off"
71 static struct tm
*time_to_tm(timestamp_t time
, int tz
, struct tm
*tm
)
73 time_t t
= gm_time_t(time
, tz
);
74 return gmtime_r(&t
, tm
);
77 static struct tm
*time_to_tm_local(timestamp_t time
, struct tm
*tm
)
80 return localtime_r(&t
, tm
);
84 * Fill in the localtime 'struct tm' for the supplied time,
85 * and return the local tz.
87 static int local_time_tzoffset(time_t t
, struct tm
*tm
)
93 t_local
= tm_to_time_t(tm
);
95 return 0; /* error; just use +0000 */
101 offset
= t_local
- t
;
103 offset
/= 60; /* in minutes */
104 offset
= (offset
% 60) + ((offset
/ 60) * 100);
105 return offset
* eastwest
;
109 * What value of "tz" was in effect back then at "time" in the
112 static int local_tzoffset(timestamp_t time
)
116 if (date_overflows(time
))
117 die("Timestamp too large for this system: %"PRItime
, time
);
119 return local_time_tzoffset((time_t)time
, &tm
);
122 static void get_time(struct timeval
*now
)
126 x
= getenv("GIT_TEST_DATE_NOW");
128 now
->tv_sec
= atoi(x
);
132 gettimeofday(now
, NULL
);
135 void show_date_relative(timestamp_t time
, struct strbuf
*timebuf
)
141 if (now
.tv_sec
< time
) {
142 strbuf_addstr(timebuf
, _("in the future"));
145 diff
= now
.tv_sec
- time
;
148 Q_("%"PRItime
" second ago", "%"PRItime
" seconds ago", diff
), diff
);
151 /* Turn it into minutes */
152 diff
= (diff
+ 30) / 60;
155 Q_("%"PRItime
" minute ago", "%"PRItime
" minutes ago", diff
), diff
);
158 /* Turn it into hours */
159 diff
= (diff
+ 30) / 60;
162 Q_("%"PRItime
" hour ago", "%"PRItime
" hours ago", diff
), diff
);
165 /* We deal with number of days from here on */
166 diff
= (diff
+ 12) / 24;
169 Q_("%"PRItime
" day ago", "%"PRItime
" days ago", diff
), diff
);
172 /* Say weeks for the past 10 weeks or so */
175 Q_("%"PRItime
" week ago", "%"PRItime
" weeks ago", (diff
+ 3) / 7),
179 /* Say months for the past 12 months or so */
182 Q_("%"PRItime
" month ago", "%"PRItime
" months ago", (diff
+ 15) / 30),
186 /* Give years and months for 5 years or so */
188 timestamp_t totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
189 timestamp_t years
= totalmonths
/ 12;
190 timestamp_t months
= totalmonths
% 12;
192 struct strbuf sb
= STRBUF_INIT
;
193 strbuf_addf(&sb
, Q_("%"PRItime
" year", "%"PRItime
" years", years
), years
);
195 /* TRANSLATORS: "%s" is "<n> years" */
196 Q_("%s, %"PRItime
" month ago", "%s, %"PRItime
" months ago", months
),
201 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", years
), years
);
204 /* Otherwise, just years. Centuries is probably overkill. */
206 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", (diff
+ 183) / 365),
210 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
212 static struct date_mode mode
= DATE_MODE_INIT
;
213 if (type
== DATE_STRFTIME
)
214 BUG("cannot create anonymous strftime date_mode struct");
219 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
)
230 hide
.tz
= local
|| tz
== human_tz
;
231 hide
.year
= tm
->tm_year
== human_tm
->tm_year
;
233 if (tm
->tm_mon
== human_tm
->tm_mon
) {
234 if (tm
->tm_mday
> human_tm
->tm_mday
) {
235 /* Future date: think timezones */
236 } else if (tm
->tm_mday
== human_tm
->tm_mday
) {
237 hide
.date
= hide
.wday
= 1;
238 } else if (tm
->tm_mday
+ 5 > human_tm
->tm_mday
) {
239 /* Leave just weekday if it was a few days ago */
245 /* Show "today" times as just relative times */
247 show_date_relative(time
, buf
);
252 * Always hide seconds for human-readable.
253 * Hide timezone if showing date.
254 * Hide weekday and time if showing year.
256 * The logic here is two-fold:
257 * (a) only show details when recent enough to matter
258 * (b) keep the maximum length "similar", and in check
260 if (human_tm
->tm_year
) {
262 hide
.tz
|= !hide
.date
;
263 hide
.wday
= hide
.time
= !hide
.year
;
267 strbuf_addf(buf
, "%.3s ", weekday_names
[tm
->tm_wday
]);
269 strbuf_addf(buf
, "%.3s %d ", month_names
[tm
->tm_mon
], tm
->tm_mday
);
271 /* Do we want AM/PM depending on locale? */
273 strbuf_addf(buf
, "%02d:%02d", tm
->tm_hour
, tm
->tm_min
);
275 strbuf_addf(buf
, ":%02d", tm
->tm_sec
);
280 strbuf_addf(buf
, " %d", tm
->tm_year
+ 1900);
283 strbuf_addf(buf
, " %+05d", tz
);
286 const char *show_date(timestamp_t time
, int tz
, const struct date_mode
*mode
)
289 struct tm tmbuf
= { 0 };
290 struct tm human_tm
= { 0 };
292 static struct strbuf timebuf
= STRBUF_INIT
;
294 if (mode
->type
== DATE_UNIX
) {
295 strbuf_reset(&timebuf
);
296 strbuf_addf(&timebuf
, "%"PRItime
, time
);
300 if (mode
->type
== DATE_HUMAN
) {
305 /* Fill in the data for "current time" in human_tz and human_tm */
306 human_tz
= local_time_tzoffset(now
.tv_sec
, &human_tm
);
310 tz
= local_tzoffset(time
);
312 if (mode
->type
== DATE_RAW
) {
313 strbuf_reset(&timebuf
);
314 strbuf_addf(&timebuf
, "%"PRItime
" %+05d", time
, tz
);
318 if (mode
->type
== DATE_RELATIVE
) {
319 strbuf_reset(&timebuf
);
320 show_date_relative(time
, &timebuf
);
325 tm
= time_to_tm_local(time
, &tmbuf
);
327 tm
= time_to_tm(time
, tz
, &tmbuf
);
329 tm
= time_to_tm(0, 0, &tmbuf
);
333 strbuf_reset(&timebuf
);
334 if (mode
->type
== DATE_SHORT
)
335 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
336 tm
->tm_mon
+ 1, tm
->tm_mday
);
337 else if (mode
->type
== DATE_ISO8601
)
338 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
342 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
344 else if (mode
->type
== DATE_ISO8601_STRICT
) {
345 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d",
349 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
351 strbuf_addch(&timebuf
, 'Z');
353 strbuf_addch(&timebuf
, tz
>= 0 ? '+' : '-');
355 strbuf_addf(&timebuf
, "%02d:%02d", tz
/ 100, tz
% 100);
357 } else if (mode
->type
== DATE_RFC2822
)
358 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
359 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
360 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
361 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
362 else if (mode
->type
== DATE_STRFTIME
)
363 strbuf_addftime(&timebuf
, mode
->strftime_fmt
, tm
, tz
,
366 show_date_normal(&timebuf
, time
, tm
, tz
, &human_tm
, human_tz
, mode
->local
);
371 * Check these. And note how it doesn't do the summer-time conversion.
373 * In my world, it's always summer, and things are probably a bit off
376 static const struct {
380 } timezone_names
[] = {
381 { "IDLW", -12, 0, }, /* International Date Line West */
382 { "NT", -11, 0, }, /* Nome */
383 { "CAT", -10, 0, }, /* Central Alaska */
384 { "HST", -10, 0, }, /* Hawaii Standard */
385 { "HDT", -10, 1, }, /* Hawaii Daylight */
386 { "YST", -9, 0, }, /* Yukon Standard */
387 { "YDT", -9, 1, }, /* Yukon Daylight */
388 { "PST", -8, 0, }, /* Pacific Standard */
389 { "PDT", -8, 1, }, /* Pacific Daylight */
390 { "MST", -7, 0, }, /* Mountain Standard */
391 { "MDT", -7, 1, }, /* Mountain Daylight */
392 { "CST", -6, 0, }, /* Central Standard */
393 { "CDT", -6, 1, }, /* Central Daylight */
394 { "EST", -5, 0, }, /* Eastern Standard */
395 { "EDT", -5, 1, }, /* Eastern Daylight */
396 { "AST", -3, 0, }, /* Atlantic Standard */
397 { "ADT", -3, 1, }, /* Atlantic Daylight */
398 { "WAT", -1, 0, }, /* West Africa */
400 { "GMT", 0, 0, }, /* Greenwich Mean */
401 { "UTC", 0, 0, }, /* Universal (Coordinated) */
402 { "Z", 0, 0, }, /* Zulu, alias for UTC */
404 { "WET", 0, 0, }, /* Western European */
405 { "BST", 0, 1, }, /* British Summer */
406 { "CET", +1, 0, }, /* Central European */
407 { "MET", +1, 0, }, /* Middle European */
408 { "MEWT", +1, 0, }, /* Middle European Winter */
409 { "MEST", +1, 1, }, /* Middle European Summer */
410 { "CEST", +1, 1, }, /* Central European Summer */
411 { "MESZ", +1, 1, }, /* Middle European Summer */
412 { "FWT", +1, 0, }, /* French Winter */
413 { "FST", +1, 1, }, /* French Summer */
414 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
415 { "EEST", +2, 1, }, /* Eastern European Daylight */
416 { "WAST", +7, 0, }, /* West Australian Standard */
417 { "WADT", +7, 1, }, /* West Australian Daylight */
418 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
419 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
420 { "EAST", +10, 0, }, /* Eastern Australian Standard */
421 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
422 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
423 { "NZT", +12, 0, }, /* New Zealand */
424 { "NZST", +12, 0, }, /* New Zealand Standard */
425 { "NZDT", +12, 1, }, /* New Zealand Daylight */
426 { "IDLE", +12, 0, }, /* International Date Line East */
429 static int match_string(const char *date
, const char *str
)
433 for (i
= 0; *date
; date
++, str
++, i
++) {
436 if (toupper(*date
) == toupper(*str
))
445 static int skip_alpha(const char *date
)
450 } while (isalpha(date
[i
]));
455 * Parse month, weekday, or timezone name
457 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
461 for (i
= 0; i
< 12; i
++) {
462 int match
= match_string(date
, month_names
[i
]);
469 for (i
= 0; i
< 7; i
++) {
470 int match
= match_string(date
, weekday_names
[i
]);
477 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
478 int match
= match_string(date
, timezone_names
[i
].name
);
479 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
480 int off
= timezone_names
[i
].offset
;
482 /* This is bogus, but we like summer */
483 off
+= timezone_names
[i
].dst
;
485 /* Only use the tz name offset if we don't have anything better */
493 if (match_string(date
, "PM") == 2) {
494 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
498 if (match_string(date
, "AM") == 2) {
499 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
503 /* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */
504 if (*date
== 'T' && isdigit(date
[1]) && tm
->tm_hour
== -1) {
505 tm
->tm_min
= tm
->tm_sec
= 0;
510 return skip_alpha(date
);
513 static int set_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
515 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
516 struct tm check
= *tm
;
517 struct tm
*r
= (now_tm
? &check
: tm
);
520 r
->tm_mon
= month
- 1;
525 r
->tm_year
= now_tm
->tm_year
;
527 else if (year
>= 1970 && year
< 2100)
528 r
->tm_year
= year
- 1900;
529 else if (year
> 70 && year
< 100)
532 r
->tm_year
= year
+ 100;
538 specified
= tm_to_time_t(r
);
540 /* Be it commit time or author time, it does not make
541 * sense to specify timestamp way into the future. Make
542 * sure it is not later than ten days from now...
544 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
546 tm
->tm_mon
= r
->tm_mon
;
547 tm
->tm_mday
= r
->tm_mday
;
549 tm
->tm_year
= r
->tm_year
;
555 static int set_time(long hour
, long minute
, long second
, struct tm
*tm
)
557 /* We accept 61st second because of leap second */
558 if (0 <= hour
&& hour
<= 24 &&
559 0 <= minute
&& minute
< 60 &&
560 0 <= second
&& second
<= 60) {
569 static int is_date_known(struct tm
*tm
)
571 return tm
->tm_year
!= -1 && tm
->tm_mon
!= -1 && tm
->tm_mday
!= -1;
574 static int match_multi_number(timestamp_t num
, char c
, const char *date
,
575 char *end
, struct tm
*tm
, time_t now
)
578 struct tm
*refuse_future
;
581 num2
= strtol(end
+1, &end
, 10);
583 if (*end
== c
&& isdigit(end
[1]))
584 num3
= strtol(end
+1, &end
, 10);
591 if (set_time(num
, num2
, num3
, tm
) == 0) {
593 * If %H:%M:%S was just parsed followed by: .<num4>
594 * Consider (& discard) it as fractional second
595 * if %Y%m%d is parsed before.
597 if (*end
== '.' && isdigit(end
[1]) && is_date_known(tm
))
598 strtol(end
+ 1, &end
, 10);
608 refuse_future
= NULL
;
609 if (gmtime_r(&now
, &now_tm
))
610 refuse_future
= &now_tm
;
614 if (set_date(num
, num2
, num3
, NULL
, now
, tm
) == 0)
617 if (set_date(num
, num3
, num2
, NULL
, now
, tm
) == 0)
620 /* Our eastern European friends say dd.mm.yy[yy]
621 * is the norm there, so giving precedence to
622 * mm/dd/yy[yy] form only when separator is not '.'
625 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
627 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
628 if (set_date(num3
, num2
, num
, refuse_future
, now
, tm
) == 0)
630 /* Funny European mm.dd.yy */
632 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
640 * Have we filled in any part of the time/date yet?
641 * We just do a binary 'and' to see if the sign bit
642 * is set in all the values.
644 static inline int nodate(struct tm
*tm
)
646 return (tm
->tm_year
&
655 * Have we seen an ISO-8601-alike date, i.e. 20220101T0,
656 * In which, hour is still unset,
657 * and minutes and second has been set to 0.
659 static inline int maybeiso8601(struct tm
*tm
)
661 return tm
->tm_hour
== -1 &&
667 * We've seen a digit. Time? Year? Date?
669 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
675 num
= parse_timestamp(date
, &end
, 10);
678 * Seconds since 1970? We trigger on that for any numbers with
679 * more than 8 digits. This is because we don't want to rule out
680 * numbers like 20070606 as a YYYYMMDD date.
682 if (num
>= 100000000 && nodate(tm
)) {
684 if (gmtime_r(&time
, tm
)) {
691 * Check for special formats: num[-.:/]num[same]num
698 if (isdigit(end
[1])) {
699 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
706 * None of the special formats? Try to guess what
707 * the number meant. We use the number of digits
708 * to make a more educated guess..
713 } while (isdigit(date
[n
]));
715 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
716 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
717 if (n
== 8 || n
== 6) {
718 unsigned int num1
= num
/ 10000;
719 unsigned int num2
= (num
% 10000) / 100;
720 unsigned int num3
= num
% 100;
722 set_date(num1
, num2
, num3
, NULL
, time(NULL
), tm
);
723 else if (n
== 6 && set_time(num1
, num2
, num3
, tm
) == 0 &&
724 *end
== '.' && isdigit(end
[1]))
725 strtoul(end
+ 1, &end
, 10);
729 /* reduced precision of ISO-8601's time: HHMM or HH */
730 if (maybeiso8601(tm
)) {
731 unsigned int num1
= num
;
732 unsigned int num2
= 0;
737 if ((n
== 4 || n
== 2) && !nodate(tm
) &&
738 set_time(num1
, num2
, 0, tm
) == 0)
741 * We thought this is an ISO-8601 time string,
742 * we set minutes and seconds to 0,
743 * turn out it isn't, rollback the change.
745 tm
->tm_min
= tm
->tm_sec
= -1;
748 /* Four-digit year or a timezone? */
750 if (num
<= 1400 && *offset
== -1) {
751 unsigned int minutes
= num
% 100;
752 unsigned int hours
= num
/ 100;
753 *offset
= hours
*60 + minutes
;
754 } else if (num
> 1900 && num
< 2100)
755 tm
->tm_year
= num
- 1900;
760 * Ignore lots of numerals. We took care of 4-digit years above.
761 * Days or months must be one or two digits.
767 * NOTE! We will give precedence to day-of-month over month or
768 * year numbers in the 1-12 range. So 05 is always "mday 5",
769 * unless we already have a mday..
771 * IOW, 01 Apr 05 parses as "April 1st, 2005".
773 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
778 /* Two-digit year? */
779 if (n
== 2 && tm
->tm_year
< 0) {
780 if (num
< 10 && tm
->tm_mday
>= 0) {
781 tm
->tm_year
= num
+ 100;
790 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
796 static int match_tz(const char *date
, int *offp
)
799 int hour
= strtoul(date
+ 1, &end
, 10);
800 int n
= end
- (date
+ 1);
808 min
= 99; /* random crap */
809 } else if (*end
== ':') {
811 min
= strtoul(end
+ 1, &end
, 10);
812 if (end
- (date
+ 1) != 5)
813 min
= 99; /* random crap */
814 } /* otherwise we parsed "hh" */
817 * Don't accept any random crap. Even though some places have
818 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
819 * UTC+14), there is something wrong if hour part is much
820 * larger than that. We might also want to check that the
821 * minutes are divisible by 15 or something too. (Offset of
822 * Kathmandu, Nepal is UTC+5:45)
824 if (min
< 60 && hour
< 24) {
825 int offset
= hour
* 60 + min
;
833 static void date_string(timestamp_t date
, int offset
, struct strbuf
*buf
)
841 strbuf_addf(buf
, "%"PRItime
" %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
845 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
846 * only when it appears not as part of any other string.
848 static int match_object_header_date(const char *date
, timestamp_t
*timestamp
, int *offset
)
854 if (*date
< '0' || '9' < *date
)
856 stamp
= parse_timestamp(date
, &end
, 10);
857 if (*end
!= ' ' || stamp
== TIME_MAX
|| (end
[1] != '+' && end
[1] != '-'))
860 ofs
= strtol(date
, &end
, 10);
861 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
863 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
871 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
872 (i.e. English) day/month names, and it doesn't work correctly with %z. */
873 int parse_date_basic(const char *date
, timestamp_t
*timestamp
, int *offset
)
877 timestamp_t dummy_timestamp
;
881 timestamp
= &dummy_timestamp
;
883 offset
= &dummy_offset
;
885 memset(&tm
, 0, sizeof(tm
));
897 !match_object_header_date(date
+ 1, timestamp
, offset
))
898 return 0; /* success */
901 unsigned char c
= *date
;
903 /* Stop at end of string or newline */
908 match
= match_alpha(date
, &tm
, offset
);
910 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
911 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
912 match
= match_tz(date
, offset
);
922 /* do not use mktime(), which uses local timezone, here */
923 *timestamp
= tm_to_time_t(&tm
);
924 if (*timestamp
== -1)
930 /* gmtime_r() in match_digit() may have clobbered it */
932 temp_time
= mktime(&tm
);
933 if ((time_t)*timestamp
> temp_time
) {
934 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
936 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
941 *timestamp
-= *offset
* 60;
942 return 0; /* success */
945 int parse_expiry_date(const char *date
, timestamp_t
*timestamp
)
949 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
951 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
953 * We take over "now" here, which usually translates
954 * to the current timestamp. This is because the user
955 * really means to expire everything that was done in
956 * the past, and by definition reflogs are the record
957 * of the past, and there is nothing from the future
960 *timestamp
= TIME_MAX
;
962 *timestamp
= approxidate_careful(date
, &errors
);
967 int parse_date(const char *date
, struct strbuf
*result
)
969 timestamp_t timestamp
;
971 if (parse_date_basic(date
, ×tamp
, &offset
))
973 date_string(timestamp
, offset
, result
);
977 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
979 if (skip_prefix(format
, "relative", end
))
980 return DATE_RELATIVE
;
981 if (skip_prefix(format
, "iso8601-strict", end
) ||
982 skip_prefix(format
, "iso-strict", end
))
983 return DATE_ISO8601_STRICT
;
984 if (skip_prefix(format
, "iso8601", end
) ||
985 skip_prefix(format
, "iso", end
))
987 if (skip_prefix(format
, "rfc2822", end
) ||
988 skip_prefix(format
, "rfc", end
))
990 if (skip_prefix(format
, "short", end
))
992 if (skip_prefix(format
, "default", end
))
994 if (skip_prefix(format
, "human", end
))
996 if (skip_prefix(format
, "raw", end
))
998 if (skip_prefix(format
, "unix", end
))
1000 if (skip_prefix(format
, "format", end
))
1001 return DATE_STRFTIME
;
1003 * Please update $__git_log_date_formats in
1004 * git-completion.bash when you add new formats.
1007 die("unknown date format %s", format
);
1010 void parse_date_format(const char *format
, struct date_mode
*mode
)
1014 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
1015 if (skip_prefix(format
, "auto:", &p
)) {
1016 if (isatty(1) || pager_in_use())
1022 /* historical alias */
1023 if (!strcmp(format
, "local"))
1024 format
= "default-local";
1026 mode
->type
= parse_date_type(format
, &p
);
1029 if (skip_prefix(p
, "-local", &p
))
1032 if (mode
->type
== DATE_STRFTIME
) {
1033 if (!skip_prefix(p
, ":", &p
))
1034 die("date format missing colon separator: %s", format
);
1035 mode
->strftime_fmt
= xstrdup(p
);
1037 die("unknown date format %s", format
);
1040 void date_mode_release(struct date_mode
*mode
)
1042 free((char *)mode
->strftime_fmt
);
1045 void datestamp(struct strbuf
*out
)
1049 struct tm tm
= { 0 };
1053 offset
= tm_to_time_t(localtime_r(&now
, &tm
)) - now
;
1056 date_string(now
, offset
, out
);
1060 * Relative time update (eg "2 days ago"). If we haven't set the time
1061 * yet, we need to set it from current time.
1063 static time_t update_tm(struct tm
*tm
, struct tm
*now
, time_t sec
)
1067 if (tm
->tm_mday
< 0)
1068 tm
->tm_mday
= now
->tm_mday
;
1070 tm
->tm_mon
= now
->tm_mon
;
1071 if (tm
->tm_year
< 0) {
1072 tm
->tm_year
= now
->tm_year
;
1073 if (tm
->tm_mon
> now
->tm_mon
)
1077 n
= mktime(tm
) - sec
;
1078 localtime_r(&n
, tm
);
1083 * Do we have a pending number at the end, or when
1084 * we see a new one? Let's assume it's a month day,
1085 * as in "Dec 6, 1992"
1087 static void pending_number(struct tm
*tm
, int *num
)
1093 if (tm
->tm_mday
< 0 && number
< 32)
1094 tm
->tm_mday
= number
;
1095 else if (tm
->tm_mon
< 0 && number
< 13)
1096 tm
->tm_mon
= number
-1;
1097 else if (tm
->tm_year
< 0) {
1098 if (number
> 1969 && number
< 2100)
1099 tm
->tm_year
= number
- 1900;
1100 else if (number
> 69 && number
< 100)
1101 tm
->tm_year
= number
;
1102 else if (number
< 38)
1103 tm
->tm_year
= 100 + number
;
1104 /* We screw up for number = 00 ? */
1109 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
1112 update_tm(tm
, now
, 0);
1115 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
1118 update_tm(tm
, now
, 24*60*60);
1121 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
1123 if (tm
->tm_hour
< hour
)
1124 update_tm(tm
, now
, 24*60*60);
1130 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
1132 pending_number(tm
, num
);
1133 date_time(tm
, now
, 0);
1136 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
1138 pending_number(tm
, num
);
1139 date_time(tm
, now
, 12);
1142 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
1144 pending_number(tm
, num
);
1145 date_time(tm
, now
, 17);
1148 static void date_pm(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1159 tm
->tm_hour
= (hour
% 12) + 12;
1162 static void date_am(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1173 tm
->tm_hour
= (hour
% 12);
1176 static void date_never(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1179 localtime_r(&n
, tm
);
1183 static const struct special
{
1185 void (*fn
)(struct tm
*, struct tm
*, int *);
1187 { "yesterday", date_yesterday
},
1188 { "noon", date_noon
},
1189 { "midnight", date_midnight
},
1190 { "tea", date_tea
},
1193 { "never", date_never
},
1194 { "now", date_now
},
1198 static const char *number_name
[] = {
1199 "zero", "one", "two", "three", "four",
1200 "five", "six", "seven", "eight", "nine", "ten",
1203 static const struct typelen
{
1210 { "days", 24*60*60 },
1211 { "weeks", 7*24*60*60 },
1215 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
1217 const struct typelen
*tl
;
1218 const struct special
*s
;
1219 const char *end
= date
;
1222 while (isalpha(*++end
))
1225 for (i
= 0; i
< 12; i
++) {
1226 int match
= match_string(date
, month_names
[i
]);
1234 for (s
= special
; s
->name
; s
++) {
1235 int len
= strlen(s
->name
);
1236 if (match_string(date
, s
->name
) == len
) {
1237 s
->fn(tm
, now
, num
);
1244 for (i
= 1; i
< 11; i
++) {
1245 int len
= strlen(number_name
[i
]);
1246 if (match_string(date
, number_name
[i
]) == len
) {
1252 if (match_string(date
, "last") == 4) {
1261 int len
= strlen(tl
->type
);
1262 if (match_string(date
, tl
->type
) >= len
-1) {
1263 update_tm(tm
, now
, tl
->length
* *num
);
1271 for (i
= 0; i
< 7; i
++) {
1272 int match
= match_string(date
, weekday_names
[i
]);
1274 int diff
, n
= *num
-1;
1277 diff
= tm
->tm_wday
- i
;
1282 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1288 if (match_string(date
, "months") >= 5) {
1290 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1291 n
= tm
->tm_mon
- *num
;
1302 if (match_string(date
, "years") >= 4) {
1303 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1304 tm
->tm_year
-= *num
;
1313 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1317 timestamp_t number
= parse_timestamp(date
, &end
, 10);
1324 if (isdigit(end
[1])) {
1325 int match
= match_multi_number(number
, *end
, date
, end
,
1328 return date
+ match
;
1332 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1333 if (date
[0] != '0' || end
- date
<= 2)
1338 static timestamp_t
approxidate_str(const char *date
,
1339 const struct timeval
*tv
,
1347 time_sec
= tv
->tv_sec
;
1348 localtime_r(&time_sec
, &tm
);
1356 unsigned char c
= *date
;
1361 pending_number(&tm
, &number
);
1362 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1367 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1369 pending_number(&tm
, &number
);
1372 return (timestamp_t
)update_tm(&tm
, &now
, 0);
1375 timestamp_t
approxidate_careful(const char *date
, int *error_ret
)
1378 timestamp_t timestamp
;
1384 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1390 return approxidate_str(date
, &tv
, error_ret
);
1393 int date_overflows(timestamp_t t
)
1397 /* If we overflowed our timestamp data type, that's bad... */
1398 if ((uintmax_t)t
>= TIME_MAX
)
1402 * ...but we also are going to feed the result to system
1403 * functions that expect time_t, which is often "signed long".
1404 * Make sure that we fit into time_t, as well.
1407 return t
!= sys
|| (t
< 1) != (sys
< 1);