2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 * This is like mktime, but without normalization of tm_wday and tm_yday.
13 time_t tm_to_time_t(const struct tm
*tm
)
15 static const int mdays
[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
18 int year
= tm
->tm_year
- 70;
19 int month
= tm
->tm_mon
;
20 int day
= tm
->tm_mday
;
22 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
24 if (month
< 0 || month
> 11) /* array bounds */
26 if (month
< 2 || (year
+ 2) % 4)
28 if (tm
->tm_hour
< 0 || tm
->tm_min
< 0 || tm
->tm_sec
< 0)
30 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
31 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
34 static const char *month_names
[] = {
35 "January", "February", "March", "April", "May", "June",
36 "July", "August", "September", "October", "November", "December"
39 static const char *weekday_names
[] = {
40 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
43 static time_t gm_time_t(timestamp_t time
, int tz
)
47 minutes
= tz
< 0 ? -tz
: tz
;
48 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
49 minutes
= tz
< 0 ? -minutes
: minutes
;
52 if (unsigned_add_overflows(time
, minutes
* 60))
53 die("Timestamp+tz too large: %"PRItime
" +%04d",
55 } else if (time
< -minutes
* 60)
56 die("Timestamp before Unix epoch: %"PRItime
" %04d", time
, tz
);
58 if (date_overflows(time
))
59 die("Timestamp too large for this system: %"PRItime
, time
);
64 * The "tz" thing is passed in as this strange "decimal parse of tz"
65 * thing, which means that tz -0100 is passed in as the integer -100,
66 * even though it means "sixty minutes off"
68 static struct tm
*time_to_tm(timestamp_t time
, int tz
, struct tm
*tm
)
70 time_t t
= gm_time_t(time
, tz
);
71 return gmtime_r(&t
, tm
);
74 static struct tm
*time_to_tm_local(timestamp_t time
, struct tm
*tm
)
77 return localtime_r(&t
, tm
);
81 * Fill in the localtime 'struct tm' for the supplied time,
82 * and return the local tz.
84 static int local_time_tzoffset(time_t t
, struct tm
*tm
)
90 t_local
= tm_to_time_t(tm
);
92 return 0; /* error; just use +0000 */
100 offset
/= 60; /* in minutes */
101 offset
= (offset
% 60) + ((offset
/ 60) * 100);
102 return offset
* eastwest
;
106 * What value of "tz" was in effect back then at "time" in the
109 static int local_tzoffset(timestamp_t time
)
113 if (date_overflows(time
))
114 die("Timestamp too large for this system: %"PRItime
, time
);
116 return local_time_tzoffset((time_t)time
, &tm
);
119 static void get_time(struct timeval
*now
)
123 x
= getenv("GIT_TEST_DATE_NOW");
125 now
->tv_sec
= atoi(x
);
129 gettimeofday(now
, NULL
);
132 void show_date_relative(timestamp_t time
, struct strbuf
*timebuf
)
138 if (now
.tv_sec
< time
) {
139 strbuf_addstr(timebuf
, _("in the future"));
142 diff
= now
.tv_sec
- time
;
145 Q_("%"PRItime
" second ago", "%"PRItime
" seconds ago", diff
), diff
);
148 /* Turn it into minutes */
149 diff
= (diff
+ 30) / 60;
152 Q_("%"PRItime
" minute ago", "%"PRItime
" minutes ago", diff
), diff
);
155 /* Turn it into hours */
156 diff
= (diff
+ 30) / 60;
159 Q_("%"PRItime
" hour ago", "%"PRItime
" hours ago", diff
), diff
);
162 /* We deal with number of days from here on */
163 diff
= (diff
+ 12) / 24;
166 Q_("%"PRItime
" day ago", "%"PRItime
" days ago", diff
), diff
);
169 /* Say weeks for the past 10 weeks or so */
172 Q_("%"PRItime
" week ago", "%"PRItime
" weeks ago", (diff
+ 3) / 7),
176 /* Say months for the past 12 months or so */
179 Q_("%"PRItime
" month ago", "%"PRItime
" months ago", (diff
+ 15) / 30),
183 /* Give years and months for 5 years or so */
185 timestamp_t totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
186 timestamp_t years
= totalmonths
/ 12;
187 timestamp_t months
= totalmonths
% 12;
189 struct strbuf sb
= STRBUF_INIT
;
190 strbuf_addf(&sb
, Q_("%"PRItime
" year", "%"PRItime
" years", years
), years
);
192 /* TRANSLATORS: "%s" is "<n> years" */
193 Q_("%s, %"PRItime
" month ago", "%s, %"PRItime
" months ago", months
),
198 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", years
), years
);
201 /* Otherwise, just years. Centuries is probably overkill. */
203 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", (diff
+ 183) / 365),
207 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
209 static struct date_mode mode
= DATE_MODE_INIT
;
210 if (type
== DATE_STRFTIME
)
211 BUG("cannot create anonymous strftime date_mode struct");
216 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
)
227 hide
.tz
= local
|| tz
== human_tz
;
228 hide
.year
= tm
->tm_year
== human_tm
->tm_year
;
230 if (tm
->tm_mon
== human_tm
->tm_mon
) {
231 if (tm
->tm_mday
> human_tm
->tm_mday
) {
232 /* Future date: think timezones */
233 } else if (tm
->tm_mday
== human_tm
->tm_mday
) {
234 hide
.date
= hide
.wday
= 1;
235 } else if (tm
->tm_mday
+ 5 > human_tm
->tm_mday
) {
236 /* Leave just weekday if it was a few days ago */
242 /* Show "today" times as just relative times */
244 show_date_relative(time
, buf
);
249 * Always hide seconds for human-readable.
250 * Hide timezone if showing date.
251 * Hide weekday and time if showing year.
253 * The logic here is two-fold:
254 * (a) only show details when recent enough to matter
255 * (b) keep the maximum length "similar", and in check
257 if (human_tm
->tm_year
) {
259 hide
.tz
|= !hide
.date
;
260 hide
.wday
= hide
.time
= !hide
.year
;
264 strbuf_addf(buf
, "%.3s ", weekday_names
[tm
->tm_wday
]);
266 strbuf_addf(buf
, "%.3s %d ", month_names
[tm
->tm_mon
], tm
->tm_mday
);
268 /* Do we want AM/PM depending on locale? */
270 strbuf_addf(buf
, "%02d:%02d", tm
->tm_hour
, tm
->tm_min
);
272 strbuf_addf(buf
, ":%02d", tm
->tm_sec
);
277 strbuf_addf(buf
, " %d", tm
->tm_year
+ 1900);
280 strbuf_addf(buf
, " %+05d", tz
);
283 const char *show_date(timestamp_t time
, int tz
, const struct date_mode
*mode
)
286 struct tm tmbuf
= { 0 };
287 struct tm human_tm
= { 0 };
289 static struct strbuf timebuf
= STRBUF_INIT
;
291 if (mode
->type
== DATE_UNIX
) {
292 strbuf_reset(&timebuf
);
293 strbuf_addf(&timebuf
, "%"PRItime
, time
);
297 if (mode
->type
== DATE_HUMAN
) {
302 /* Fill in the data for "current time" in human_tz and human_tm */
303 human_tz
= local_time_tzoffset(now
.tv_sec
, &human_tm
);
307 tz
= local_tzoffset(time
);
309 if (mode
->type
== DATE_RAW
) {
310 strbuf_reset(&timebuf
);
311 strbuf_addf(&timebuf
, "%"PRItime
" %+05d", time
, tz
);
315 if (mode
->type
== DATE_RELATIVE
) {
316 strbuf_reset(&timebuf
);
317 show_date_relative(time
, &timebuf
);
322 tm
= time_to_tm_local(time
, &tmbuf
);
324 tm
= time_to_tm(time
, tz
, &tmbuf
);
326 tm
= time_to_tm(0, 0, &tmbuf
);
330 strbuf_reset(&timebuf
);
331 if (mode
->type
== DATE_SHORT
)
332 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
333 tm
->tm_mon
+ 1, tm
->tm_mday
);
334 else if (mode
->type
== DATE_ISO8601
)
335 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
339 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
341 else if (mode
->type
== DATE_ISO8601_STRICT
) {
342 char sign
= (tz
>= 0) ? '+' : '-';
344 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
348 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
349 sign
, tz
/ 100, tz
% 100);
350 } else if (mode
->type
== DATE_RFC2822
)
351 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
352 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
353 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
354 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
355 else if (mode
->type
== DATE_STRFTIME
)
356 strbuf_addftime(&timebuf
, mode
->strftime_fmt
, tm
, tz
,
359 show_date_normal(&timebuf
, time
, tm
, tz
, &human_tm
, human_tz
, mode
->local
);
364 * Check these. And note how it doesn't do the summer-time conversion.
366 * In my world, it's always summer, and things are probably a bit off
369 static const struct {
373 } timezone_names
[] = {
374 { "IDLW", -12, 0, }, /* International Date Line West */
375 { "NT", -11, 0, }, /* Nome */
376 { "CAT", -10, 0, }, /* Central Alaska */
377 { "HST", -10, 0, }, /* Hawaii Standard */
378 { "HDT", -10, 1, }, /* Hawaii Daylight */
379 { "YST", -9, 0, }, /* Yukon Standard */
380 { "YDT", -9, 1, }, /* Yukon Daylight */
381 { "PST", -8, 0, }, /* Pacific Standard */
382 { "PDT", -8, 1, }, /* Pacific Daylight */
383 { "MST", -7, 0, }, /* Mountain Standard */
384 { "MDT", -7, 1, }, /* Mountain Daylight */
385 { "CST", -6, 0, }, /* Central Standard */
386 { "CDT", -6, 1, }, /* Central Daylight */
387 { "EST", -5, 0, }, /* Eastern Standard */
388 { "EDT", -5, 1, }, /* Eastern Daylight */
389 { "AST", -3, 0, }, /* Atlantic Standard */
390 { "ADT", -3, 1, }, /* Atlantic Daylight */
391 { "WAT", -1, 0, }, /* West Africa */
393 { "GMT", 0, 0, }, /* Greenwich Mean */
394 { "UTC", 0, 0, }, /* Universal (Coordinated) */
395 { "Z", 0, 0, }, /* Zulu, alias for UTC */
397 { "WET", 0, 0, }, /* Western European */
398 { "BST", 0, 1, }, /* British Summer */
399 { "CET", +1, 0, }, /* Central European */
400 { "MET", +1, 0, }, /* Middle European */
401 { "MEWT", +1, 0, }, /* Middle European Winter */
402 { "MEST", +1, 1, }, /* Middle European Summer */
403 { "CEST", +1, 1, }, /* Central European Summer */
404 { "MESZ", +1, 1, }, /* Middle European Summer */
405 { "FWT", +1, 0, }, /* French Winter */
406 { "FST", +1, 1, }, /* French Summer */
407 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
408 { "EEST", +2, 1, }, /* Eastern European Daylight */
409 { "WAST", +7, 0, }, /* West Australian Standard */
410 { "WADT", +7, 1, }, /* West Australian Daylight */
411 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
412 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
413 { "EAST", +10, 0, }, /* Eastern Australian Standard */
414 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
415 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
416 { "NZT", +12, 0, }, /* New Zealand */
417 { "NZST", +12, 0, }, /* New Zealand Standard */
418 { "NZDT", +12, 1, }, /* New Zealand Daylight */
419 { "IDLE", +12, 0, }, /* International Date Line East */
422 static int match_string(const char *date
, const char *str
)
426 for (i
= 0; *date
; date
++, str
++, i
++) {
429 if (toupper(*date
) == toupper(*str
))
438 static int skip_alpha(const char *date
)
443 } while (isalpha(date
[i
]));
448 * Parse month, weekday, or timezone name
450 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
454 for (i
= 0; i
< 12; i
++) {
455 int match
= match_string(date
, month_names
[i
]);
462 for (i
= 0; i
< 7; i
++) {
463 int match
= match_string(date
, weekday_names
[i
]);
470 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
471 int match
= match_string(date
, timezone_names
[i
].name
);
472 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
473 int off
= timezone_names
[i
].offset
;
475 /* This is bogus, but we like summer */
476 off
+= timezone_names
[i
].dst
;
478 /* Only use the tz name offset if we don't have anything better */
486 if (match_string(date
, "PM") == 2) {
487 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
491 if (match_string(date
, "AM") == 2) {
492 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
497 return skip_alpha(date
);
500 static int set_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
502 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
503 struct tm check
= *tm
;
504 struct tm
*r
= (now_tm
? &check
: tm
);
507 r
->tm_mon
= month
- 1;
512 r
->tm_year
= now_tm
->tm_year
;
514 else if (year
>= 1970 && year
< 2100)
515 r
->tm_year
= year
- 1900;
516 else if (year
> 70 && year
< 100)
519 r
->tm_year
= year
+ 100;
525 specified
= tm_to_time_t(r
);
527 /* Be it commit time or author time, it does not make
528 * sense to specify timestamp way into the future. Make
529 * sure it is not later than ten days from now...
531 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
533 tm
->tm_mon
= r
->tm_mon
;
534 tm
->tm_mday
= r
->tm_mday
;
536 tm
->tm_year
= r
->tm_year
;
542 static int set_time(long hour
, long minute
, long second
, struct tm
*tm
)
544 /* We accept 61st second because of leap second */
545 if (0 <= hour
&& hour
<= 24 &&
546 0 <= minute
&& minute
< 60 &&
547 0 <= second
&& second
<= 60) {
556 static int is_date_known(struct tm
*tm
)
558 return tm
->tm_year
!= -1 && tm
->tm_mon
!= -1 && tm
->tm_mday
!= -1;
561 static int match_multi_number(timestamp_t num
, char c
, const char *date
,
562 char *end
, struct tm
*tm
, time_t now
)
565 struct tm
*refuse_future
;
568 num2
= strtol(end
+1, &end
, 10);
570 if (*end
== c
&& isdigit(end
[1]))
571 num3
= strtol(end
+1, &end
, 10);
578 if (set_time(num
, num2
, num3
, tm
) == 0) {
580 * If %H:%M:%S was just parsed followed by: .<num4>
581 * Consider (& discard) it as fractional second
582 * if %Y%m%d is parsed before.
584 if (*end
== '.' && isdigit(end
[1]) && is_date_known(tm
))
585 strtol(end
+ 1, &end
, 10);
595 refuse_future
= NULL
;
596 if (gmtime_r(&now
, &now_tm
))
597 refuse_future
= &now_tm
;
601 if (set_date(num
, num2
, num3
, NULL
, now
, tm
) == 0)
604 if (set_date(num
, num3
, num2
, NULL
, now
, tm
) == 0)
607 /* Our eastern European friends say dd.mm.yy[yy]
608 * is the norm there, so giving precedence to
609 * mm/dd/yy[yy] form only when separator is not '.'
612 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
614 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
615 if (set_date(num3
, num2
, num
, refuse_future
, now
, tm
) == 0)
617 /* Funny European mm.dd.yy */
619 set_date(num3
, num
, num2
, refuse_future
, now
, tm
) == 0)
627 * Have we filled in any part of the time/date yet?
628 * We just do a binary 'and' to see if the sign bit
629 * is set in all the values.
631 static inline int nodate(struct tm
*tm
)
633 return (tm
->tm_year
&
642 * We've seen a digit. Time? Year? Date?
644 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
650 num
= parse_timestamp(date
, &end
, 10);
653 * Seconds since 1970? We trigger on that for any numbers with
654 * more than 8 digits. This is because we don't want to rule out
655 * numbers like 20070606 as a YYYYMMDD date.
657 if (num
>= 100000000 && nodate(tm
)) {
659 if (gmtime_r(&time
, tm
)) {
666 * Check for special formats: num[-.:/]num[same]num
673 if (isdigit(end
[1])) {
674 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
681 * None of the special formats? Try to guess what
682 * the number meant. We use the number of digits
683 * to make a more educated guess..
688 } while (isdigit(date
[n
]));
690 /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
691 /* 6 digits, compact style of ISO-8601's time: HHMMSS */
692 if (n
== 8 || n
== 6) {
693 unsigned int num1
= num
/ 10000;
694 unsigned int num2
= (num
% 10000) / 100;
695 unsigned int num3
= num
% 100;
697 set_date(num1
, num2
, num3
, NULL
, time(NULL
), tm
);
698 else if (n
== 6 && set_time(num1
, num2
, num3
, tm
) == 0 &&
699 *end
== '.' && isdigit(end
[1]))
700 strtoul(end
+ 1, &end
, 10);
704 /* Four-digit year or a timezone? */
706 if (num
<= 1400 && *offset
== -1) {
707 unsigned int minutes
= num
% 100;
708 unsigned int hours
= num
/ 100;
709 *offset
= hours
*60 + minutes
;
710 } else if (num
> 1900 && num
< 2100)
711 tm
->tm_year
= num
- 1900;
716 * Ignore lots of numerals. We took care of 4-digit years above.
717 * Days or months must be one or two digits.
723 * NOTE! We will give precedence to day-of-month over month or
724 * year numbers in the 1-12 range. So 05 is always "mday 5",
725 * unless we already have a mday..
727 * IOW, 01 Apr 05 parses as "April 1st, 2005".
729 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
734 /* Two-digit year? */
735 if (n
== 2 && tm
->tm_year
< 0) {
736 if (num
< 10 && tm
->tm_mday
>= 0) {
737 tm
->tm_year
= num
+ 100;
746 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
752 static int match_tz(const char *date
, int *offp
)
755 int hour
= strtoul(date
+ 1, &end
, 10);
756 int n
= end
- (date
+ 1);
764 min
= 99; /* random crap */
765 } else if (*end
== ':') {
767 min
= strtoul(end
+ 1, &end
, 10);
768 if (end
- (date
+ 1) != 5)
769 min
= 99; /* random crap */
770 } /* otherwise we parsed "hh" */
773 * Don't accept any random crap. Even though some places have
774 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
775 * UTC+14), there is something wrong if hour part is much
776 * larger than that. We might also want to check that the
777 * minutes are divisible by 15 or something too. (Offset of
778 * Kathmandu, Nepal is UTC+5:45)
780 if (min
< 60 && hour
< 24) {
781 int offset
= hour
* 60 + min
;
789 static void date_string(timestamp_t date
, int offset
, struct strbuf
*buf
)
797 strbuf_addf(buf
, "%"PRItime
" %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
801 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
802 * only when it appears not as part of any other string.
804 static int match_object_header_date(const char *date
, timestamp_t
*timestamp
, int *offset
)
810 if (*date
< '0' || '9' < *date
)
812 stamp
= parse_timestamp(date
, &end
, 10);
813 if (*end
!= ' ' || stamp
== TIME_MAX
|| (end
[1] != '+' && end
[1] != '-'))
816 ofs
= strtol(date
, &end
, 10);
817 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
819 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
827 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
828 (i.e. English) day/month names, and it doesn't work correctly with %z. */
829 int parse_date_basic(const char *date
, timestamp_t
*timestamp
, int *offset
)
833 timestamp_t dummy_timestamp
;
837 timestamp
= &dummy_timestamp
;
839 offset
= &dummy_offset
;
841 memset(&tm
, 0, sizeof(tm
));
853 !match_object_header_date(date
+ 1, timestamp
, offset
))
854 return 0; /* success */
857 unsigned char c
= *date
;
859 /* Stop at end of string or newline */
864 match
= match_alpha(date
, &tm
, offset
);
866 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
867 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
868 match
= match_tz(date
, offset
);
878 /* do not use mktime(), which uses local timezone, here */
879 *timestamp
= tm_to_time_t(&tm
);
880 if (*timestamp
== -1)
886 /* gmtime_r() in match_digit() may have clobbered it */
888 temp_time
= mktime(&tm
);
889 if ((time_t)*timestamp
> temp_time
) {
890 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
892 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
897 *timestamp
-= *offset
* 60;
898 return 0; /* success */
901 int parse_expiry_date(const char *date
, timestamp_t
*timestamp
)
905 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
907 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
909 * We take over "now" here, which usually translates
910 * to the current timestamp. This is because the user
911 * really means to expire everything that was done in
912 * the past, and by definition reflogs are the record
913 * of the past, and there is nothing from the future
916 *timestamp
= TIME_MAX
;
918 *timestamp
= approxidate_careful(date
, &errors
);
923 int parse_date(const char *date
, struct strbuf
*result
)
925 timestamp_t timestamp
;
927 if (parse_date_basic(date
, ×tamp
, &offset
))
929 date_string(timestamp
, offset
, result
);
933 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
935 if (skip_prefix(format
, "relative", end
))
936 return DATE_RELATIVE
;
937 if (skip_prefix(format
, "iso8601-strict", end
) ||
938 skip_prefix(format
, "iso-strict", end
))
939 return DATE_ISO8601_STRICT
;
940 if (skip_prefix(format
, "iso8601", end
) ||
941 skip_prefix(format
, "iso", end
))
943 if (skip_prefix(format
, "rfc2822", end
) ||
944 skip_prefix(format
, "rfc", end
))
946 if (skip_prefix(format
, "short", end
))
948 if (skip_prefix(format
, "default", end
))
950 if (skip_prefix(format
, "human", end
))
952 if (skip_prefix(format
, "raw", end
))
954 if (skip_prefix(format
, "unix", end
))
956 if (skip_prefix(format
, "format", end
))
957 return DATE_STRFTIME
;
959 * Please update $__git_log_date_formats in
960 * git-completion.bash when you add new formats.
963 die("unknown date format %s", format
);
966 void parse_date_format(const char *format
, struct date_mode
*mode
)
970 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
971 if (skip_prefix(format
, "auto:", &p
)) {
972 if (isatty(1) || pager_in_use())
978 /* historical alias */
979 if (!strcmp(format
, "local"))
980 format
= "default-local";
982 mode
->type
= parse_date_type(format
, &p
);
985 if (skip_prefix(p
, "-local", &p
))
988 if (mode
->type
== DATE_STRFTIME
) {
989 if (!skip_prefix(p
, ":", &p
))
990 die("date format missing colon separator: %s", format
);
991 mode
->strftime_fmt
= xstrdup(p
);
993 die("unknown date format %s", format
);
996 void date_mode_release(struct date_mode
*mode
)
998 free((char *)mode
->strftime_fmt
);
1001 void datestamp(struct strbuf
*out
)
1005 struct tm tm
= { 0 };
1009 offset
= tm_to_time_t(localtime_r(&now
, &tm
)) - now
;
1012 date_string(now
, offset
, out
);
1016 * Relative time update (eg "2 days ago"). If we haven't set the time
1017 * yet, we need to set it from current time.
1019 static time_t update_tm(struct tm
*tm
, struct tm
*now
, time_t sec
)
1023 if (tm
->tm_mday
< 0)
1024 tm
->tm_mday
= now
->tm_mday
;
1026 tm
->tm_mon
= now
->tm_mon
;
1027 if (tm
->tm_year
< 0) {
1028 tm
->tm_year
= now
->tm_year
;
1029 if (tm
->tm_mon
> now
->tm_mon
)
1033 n
= mktime(tm
) - sec
;
1034 localtime_r(&n
, tm
);
1039 * Do we have a pending number at the end, or when
1040 * we see a new one? Let's assume it's a month day,
1041 * as in "Dec 6, 1992"
1043 static void pending_number(struct tm
*tm
, int *num
)
1049 if (tm
->tm_mday
< 0 && number
< 32)
1050 tm
->tm_mday
= number
;
1051 else if (tm
->tm_mon
< 0 && number
< 13)
1052 tm
->tm_mon
= number
-1;
1053 else if (tm
->tm_year
< 0) {
1054 if (number
> 1969 && number
< 2100)
1055 tm
->tm_year
= number
- 1900;
1056 else if (number
> 69 && number
< 100)
1057 tm
->tm_year
= number
;
1058 else if (number
< 38)
1059 tm
->tm_year
= 100 + number
;
1060 /* We screw up for number = 00 ? */
1065 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
1068 update_tm(tm
, now
, 0);
1071 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
1074 update_tm(tm
, now
, 24*60*60);
1077 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
1079 if (tm
->tm_hour
< hour
)
1080 update_tm(tm
, now
, 24*60*60);
1086 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
1088 pending_number(tm
, num
);
1089 date_time(tm
, now
, 0);
1092 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
1094 pending_number(tm
, num
);
1095 date_time(tm
, now
, 12);
1098 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
1100 pending_number(tm
, num
);
1101 date_time(tm
, now
, 17);
1104 static void date_pm(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1115 tm
->tm_hour
= (hour
% 12) + 12;
1118 static void date_am(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1129 tm
->tm_hour
= (hour
% 12);
1132 static void date_never(struct tm
*tm
, struct tm
*now UNUSED
, int *num
)
1135 localtime_r(&n
, tm
);
1139 static const struct special
{
1141 void (*fn
)(struct tm
*, struct tm
*, int *);
1143 { "yesterday", date_yesterday
},
1144 { "noon", date_noon
},
1145 { "midnight", date_midnight
},
1146 { "tea", date_tea
},
1149 { "never", date_never
},
1150 { "now", date_now
},
1154 static const char *number_name
[] = {
1155 "zero", "one", "two", "three", "four",
1156 "five", "six", "seven", "eight", "nine", "ten",
1159 static const struct typelen
{
1166 { "days", 24*60*60 },
1167 { "weeks", 7*24*60*60 },
1171 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
1173 const struct typelen
*tl
;
1174 const struct special
*s
;
1175 const char *end
= date
;
1178 while (isalpha(*++end
))
1181 for (i
= 0; i
< 12; i
++) {
1182 int match
= match_string(date
, month_names
[i
]);
1190 for (s
= special
; s
->name
; s
++) {
1191 int len
= strlen(s
->name
);
1192 if (match_string(date
, s
->name
) == len
) {
1193 s
->fn(tm
, now
, num
);
1200 for (i
= 1; i
< 11; i
++) {
1201 int len
= strlen(number_name
[i
]);
1202 if (match_string(date
, number_name
[i
]) == len
) {
1208 if (match_string(date
, "last") == 4) {
1217 int len
= strlen(tl
->type
);
1218 if (match_string(date
, tl
->type
) >= len
-1) {
1219 update_tm(tm
, now
, tl
->length
* *num
);
1227 for (i
= 0; i
< 7; i
++) {
1228 int match
= match_string(date
, weekday_names
[i
]);
1230 int diff
, n
= *num
-1;
1233 diff
= tm
->tm_wday
- i
;
1238 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1244 if (match_string(date
, "months") >= 5) {
1246 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1247 n
= tm
->tm_mon
- *num
;
1258 if (match_string(date
, "years") >= 4) {
1259 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1260 tm
->tm_year
-= *num
;
1269 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1273 timestamp_t number
= parse_timestamp(date
, &end
, 10);
1280 if (isdigit(end
[1])) {
1281 int match
= match_multi_number(number
, *end
, date
, end
,
1284 return date
+ match
;
1288 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1289 if (date
[0] != '0' || end
- date
<= 2)
1294 static timestamp_t
approxidate_str(const char *date
,
1295 const struct timeval
*tv
,
1303 time_sec
= tv
->tv_sec
;
1304 localtime_r(&time_sec
, &tm
);
1312 unsigned char c
= *date
;
1317 pending_number(&tm
, &number
);
1318 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1323 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1325 pending_number(&tm
, &number
);
1328 return (timestamp_t
)update_tm(&tm
, &now
, 0);
1331 timestamp_t
approxidate_relative(const char *date
)
1334 timestamp_t timestamp
;
1338 if (!parse_date_basic(date
, ×tamp
, &offset
))
1342 return approxidate_str(date
, (const struct timeval
*) &tv
, &errors
);
1345 timestamp_t
approxidate_careful(const char *date
, int *error_ret
)
1348 timestamp_t timestamp
;
1354 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1360 return approxidate_str(date
, &tv
, error_ret
);
1363 int date_overflows(timestamp_t t
)
1367 /* If we overflowed our timestamp data type, that's bad... */
1368 if ((uintmax_t)t
>= TIME_MAX
)
1372 * ...but we also are going to feed the result to system
1373 * functions that expect time_t, which is often "signed long".
1374 * Make sure that we fit into time_t, as well.
1377 return t
!= sys
|| (t
< 1) != (sys
< 1);