2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
12 static time_t tm_to_time_t(const struct tm
*tm
)
14 static const int mdays
[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 int year
= tm
->tm_year
- 70;
18 int month
= tm
->tm_mon
;
19 int day
= tm
->tm_mday
;
21 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
23 if (month
< 0 || month
> 11) /* array bounds */
25 if (month
< 2 || (year
+ 2) % 4)
27 if (tm
->tm_hour
< 0 || tm
->tm_min
< 0 || tm
->tm_sec
< 0)
29 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
30 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
33 static const char *month_names
[] = {
34 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
38 static const char *weekday_names
[] = {
39 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
42 static time_t gm_time_t(unsigned long time
, int tz
)
46 minutes
= tz
< 0 ? -tz
: tz
;
47 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
48 minutes
= tz
< 0 ? -minutes
: minutes
;
49 return time
+ minutes
* 60;
53 * The "tz" thing is passed in as this strange "decimal parse of tz"
54 * thing, which means that tz -0100 is passed in as the integer -100,
55 * even though it means "sixty minutes off"
57 static struct tm
*time_to_tm(unsigned long time
, int tz
)
59 time_t t
= gm_time_t(time
, tz
);
64 * What value of "tz" was in effect back then at "time" in the
67 static int local_tzoffset(unsigned long time
)
75 t_local
= tm_to_time_t(&tm
);
84 offset
/= 60; /* in minutes */
85 offset
= (offset
% 60) + ((offset
/ 60) * 100);
86 return offset
* eastwest
;
89 const char *show_date_relative(unsigned long time
, int tz
,
90 const struct timeval
*now
,
95 if (now
->tv_sec
< time
)
96 return "in the future";
97 diff
= now
->tv_sec
- time
;
99 snprintf(timebuf
, timebuf_size
, "%lu seconds ago", diff
);
102 /* Turn it into minutes */
103 diff
= (diff
+ 30) / 60;
105 snprintf(timebuf
, timebuf_size
, "%lu minutes ago", diff
);
108 /* Turn it into hours */
109 diff
= (diff
+ 30) / 60;
111 snprintf(timebuf
, timebuf_size
, "%lu hours ago", diff
);
114 /* We deal with number of days from here on */
115 diff
= (diff
+ 12) / 24;
117 snprintf(timebuf
, timebuf_size
, "%lu days ago", diff
);
120 /* Say weeks for the past 10 weeks or so */
122 snprintf(timebuf
, timebuf_size
, "%lu weeks ago", (diff
+ 3) / 7);
125 /* Say months for the past 12 months or so */
127 snprintf(timebuf
, timebuf_size
, "%lu months ago", (diff
+ 15) / 30);
130 /* Give years and months for 5 years or so */
132 unsigned long years
= diff
/ 365;
133 unsigned long months
= (diff
% 365 + 15) / 30;
135 n
= snprintf(timebuf
, timebuf_size
, "%lu year%s",
136 years
, (years
> 1 ? "s" : ""));
138 snprintf(timebuf
+ n
, timebuf_size
- n
,
140 months
, (months
> 1 ? "s" : ""));
142 snprintf(timebuf
+ n
, timebuf_size
- n
, " ago");
145 /* Otherwise, just years. Centuries is probably overkill. */
146 snprintf(timebuf
, timebuf_size
, "%lu years ago", (diff
+ 183) / 365);
150 const char *show_date(unsigned long time
, int tz
, enum date_mode mode
)
153 static char timebuf
[200];
155 if (mode
== DATE_RAW
) {
156 snprintf(timebuf
, sizeof(timebuf
), "%lu %+05d", time
, tz
);
160 if (mode
== DATE_RELATIVE
) {
162 gettimeofday(&now
, NULL
);
163 return show_date_relative(time
, tz
, &now
,
164 timebuf
, sizeof(timebuf
));
167 if (mode
== DATE_LOCAL
)
168 tz
= local_tzoffset(time
);
170 tm
= time_to_tm(time
, tz
);
173 if (mode
== DATE_SHORT
)
174 sprintf(timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
175 tm
->tm_mon
+ 1, tm
->tm_mday
);
176 else if (mode
== DATE_ISO8601
)
177 sprintf(timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
181 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
183 else if (mode
== DATE_RFC2822
)
184 sprintf(timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
185 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
186 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
187 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
189 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
190 weekday_names
[tm
->tm_wday
],
191 month_names
[tm
->tm_mon
],
193 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
195 (mode
== DATE_LOCAL
) ? 0 : ' ',
201 * Check these. And note how it doesn't do the summer-time conversion.
203 * In my world, it's always summer, and things are probably a bit off
206 static const struct {
210 } timezone_names
[] = {
211 { "IDLW", -12, 0, }, /* International Date Line West */
212 { "NT", -11, 0, }, /* Nome */
213 { "CAT", -10, 0, }, /* Central Alaska */
214 { "HST", -10, 0, }, /* Hawaii Standard */
215 { "HDT", -10, 1, }, /* Hawaii Daylight */
216 { "YST", -9, 0, }, /* Yukon Standard */
217 { "YDT", -9, 1, }, /* Yukon Daylight */
218 { "PST", -8, 0, }, /* Pacific Standard */
219 { "PDT", -8, 1, }, /* Pacific Daylight */
220 { "MST", -7, 0, }, /* Mountain Standard */
221 { "MDT", -7, 1, }, /* Mountain Daylight */
222 { "CST", -6, 0, }, /* Central Standard */
223 { "CDT", -6, 1, }, /* Central Daylight */
224 { "EST", -5, 0, }, /* Eastern Standard */
225 { "EDT", -5, 1, }, /* Eastern Daylight */
226 { "AST", -3, 0, }, /* Atlantic Standard */
227 { "ADT", -3, 1, }, /* Atlantic Daylight */
228 { "WAT", -1, 0, }, /* West Africa */
230 { "GMT", 0, 0, }, /* Greenwich Mean */
231 { "UTC", 0, 0, }, /* Universal (Coordinated) */
232 { "Z", 0, 0, }, /* Zulu, alias for UTC */
234 { "WET", 0, 0, }, /* Western European */
235 { "BST", 0, 1, }, /* British Summer */
236 { "CET", +1, 0, }, /* Central European */
237 { "MET", +1, 0, }, /* Middle European */
238 { "MEWT", +1, 0, }, /* Middle European Winter */
239 { "MEST", +1, 1, }, /* Middle European Summer */
240 { "CEST", +1, 1, }, /* Central European Summer */
241 { "MESZ", +1, 1, }, /* Middle European Summer */
242 { "FWT", +1, 0, }, /* French Winter */
243 { "FST", +1, 1, }, /* French Summer */
244 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
245 { "EEST", +2, 1, }, /* Eastern European Daylight */
246 { "WAST", +7, 0, }, /* West Australian Standard */
247 { "WADT", +7, 1, }, /* West Australian Daylight */
248 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
249 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
250 { "EAST", +10, 0, }, /* Eastern Australian Standard */
251 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
252 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
253 { "NZT", +12, 0, }, /* New Zealand */
254 { "NZST", +12, 0, }, /* New Zealand Standard */
255 { "NZDT", +12, 1, }, /* New Zealand Daylight */
256 { "IDLE", +12, 0, }, /* International Date Line East */
259 static int match_string(const char *date
, const char *str
)
263 for (i
= 0; *date
; date
++, str
++, i
++) {
266 if (toupper(*date
) == toupper(*str
))
275 static int skip_alpha(const char *date
)
280 } while (isalpha(date
[i
]));
285 * Parse month, weekday, or timezone name
287 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
291 for (i
= 0; i
< 12; i
++) {
292 int match
= match_string(date
, month_names
[i
]);
299 for (i
= 0; i
< 7; i
++) {
300 int match
= match_string(date
, weekday_names
[i
]);
307 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
308 int match
= match_string(date
, timezone_names
[i
].name
);
309 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
310 int off
= timezone_names
[i
].offset
;
312 /* This is bogus, but we like summer */
313 off
+= timezone_names
[i
].dst
;
315 /* Only use the tz name offset if we don't have anything better */
323 if (match_string(date
, "PM") == 2) {
324 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
328 if (match_string(date
, "AM") == 2) {
329 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
334 return skip_alpha(date
);
337 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
339 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
340 struct tm check
= *tm
;
341 struct tm
*r
= (now_tm
? &check
: tm
);
344 r
->tm_mon
= month
- 1;
349 r
->tm_year
= now_tm
->tm_year
;
351 else if (year
>= 1970 && year
< 2100)
352 r
->tm_year
= year
- 1900;
353 else if (year
> 70 && year
< 100)
356 r
->tm_year
= year
+ 100;
362 specified
= tm_to_time_t(r
);
364 /* Be it commit time or author time, it does not make
365 * sense to specify timestamp way into the future. Make
366 * sure it is not later than ten days from now...
368 if (now
+ 10*24*3600 < specified
)
370 tm
->tm_mon
= r
->tm_mon
;
371 tm
->tm_mday
= r
->tm_mday
;
373 tm
->tm_year
= r
->tm_year
;
379 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
383 struct tm
*refuse_future
;
386 num2
= strtol(end
+1, &end
, 10);
388 if (*end
== c
&& isdigit(end
[1]))
389 num3
= strtol(end
+1, &end
, 10);
396 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
408 refuse_future
= NULL
;
409 if (gmtime_r(&now
, &now_tm
))
410 refuse_future
= &now_tm
;
414 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
417 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
420 /* Our eastern European friends say dd.mm.yy[yy]
421 * is the norm there, so giving precedence to
422 * mm/dd/yy[yy] form only when separator is not '.'
425 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
427 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
428 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
430 /* Funny European mm.dd.yy */
432 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
440 * Have we filled in any part of the time/date yet?
441 * We just do a binary 'and' to see if the sign bit
442 * is set in all the values.
444 static inline int nodate(struct tm
*tm
)
446 return (tm
->tm_year
&
455 * We've seen a digit. Time? Year? Date?
457 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
463 num
= strtoul(date
, &end
, 10);
466 * Seconds since 1970? We trigger on that for any numbers with
467 * more than 8 digits. This is because we don't want to rule out
468 * numbers like 20070606 as a YYYYMMDD date.
470 if (num
>= 100000000 && nodate(tm
)) {
472 if (gmtime_r(&time
, tm
)) {
479 * Check for special formats: num[-.:/]num[same]num
486 if (isdigit(end
[1])) {
487 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
494 * None of the special formats? Try to guess what
495 * the number meant. We use the number of digits
496 * to make a more educated guess..
501 } while (isdigit(date
[n
]));
503 /* Four-digit year or a timezone? */
505 if (num
<= 1400 && *offset
== -1) {
506 unsigned int minutes
= num
% 100;
507 unsigned int hours
= num
/ 100;
508 *offset
= hours
*60 + minutes
;
509 } else if (num
> 1900 && num
< 2100)
510 tm
->tm_year
= num
- 1900;
515 * Ignore lots of numerals. We took care of 4-digit years above.
516 * Days or months must be one or two digits.
522 * NOTE! We will give precedence to day-of-month over month or
523 * year numbers in the 1-12 range. So 05 is always "mday 5",
524 * unless we already have a mday..
526 * IOW, 01 Apr 05 parses as "April 1st, 2005".
528 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
533 /* Two-digit year? */
534 if (n
== 2 && tm
->tm_year
< 0) {
535 if (num
< 10 && tm
->tm_mday
>= 0) {
536 tm
->tm_year
= num
+ 100;
545 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
551 static int match_tz(const char *date
, int *offp
)
554 int offset
= strtoul(date
+1, &end
, 10);
556 int n
= end
- date
- 1;
562 * Don't accept any random crap.. At least 3 digits, and
563 * a valid minute. We might want to check that the minutes
564 * are divisible by 30 or something too.
566 if (min
< 60 && n
> 2) {
567 offset
= hour
*60+min
;
576 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
584 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
587 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
588 (i.e. English) day/month names, and it doesn't work correctly with %z. */
589 int parse_date_basic(const char *date
, unsigned long *timestamp
, int *offset
)
593 unsigned long dummy_timestamp
;
597 timestamp
= &dummy_timestamp
;
599 offset
= &dummy_offset
;
601 memset(&tm
, 0, sizeof(tm
));
614 unsigned char c
= *date
;
616 /* Stop at end of string or newline */
621 match
= match_alpha(date
, &tm
, offset
);
623 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
624 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
625 match
= match_tz(date
, offset
);
635 /* mktime uses local timezone */
636 *timestamp
= tm_to_time_t(&tm
);
638 *offset
= ((time_t)*timestamp
- mktime(&tm
)) / 60;
640 if (*timestamp
== -1)
644 *timestamp
-= *offset
* 60;
645 return 0; /* success */
648 int parse_date(const char *date
, char *result
, int maxlen
)
650 unsigned long timestamp
;
652 if (parse_date_basic(date
, ×tamp
, &offset
))
654 return date_string(timestamp
, offset
, result
, maxlen
);
657 enum date_mode
parse_date_format(const char *format
)
659 if (!strcmp(format
, "relative"))
660 return DATE_RELATIVE
;
661 else if (!strcmp(format
, "iso8601") ||
662 !strcmp(format
, "iso"))
664 else if (!strcmp(format
, "rfc2822") ||
665 !strcmp(format
, "rfc"))
667 else if (!strcmp(format
, "short"))
669 else if (!strcmp(format
, "local"))
671 else if (!strcmp(format
, "default"))
673 else if (!strcmp(format
, "raw"))
676 die("unknown date format %s", format
);
679 void datestamp(char *buf
, int bufsize
)
686 offset
= tm_to_time_t(localtime(&now
)) - now
;
689 date_string(now
, offset
, buf
, bufsize
);
693 * Relative time update (eg "2 days ago"). If we haven't set the time
694 * yet, we need to set it from current time.
696 static unsigned long update_tm(struct tm
*tm
, struct tm
*now
, unsigned long sec
)
701 tm
->tm_mday
= now
->tm_mday
;
703 tm
->tm_mon
= now
->tm_mon
;
704 if (tm
->tm_year
< 0) {
705 tm
->tm_year
= now
->tm_year
;
706 if (tm
->tm_mon
> now
->tm_mon
)
710 n
= mktime(tm
) - sec
;
715 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
717 update_tm(tm
, now
, 0);
720 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
722 update_tm(tm
, now
, 24*60*60);
725 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
727 if (tm
->tm_hour
< hour
)
728 date_yesterday(tm
, now
, NULL
);
734 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
736 date_time(tm
, now
, 0);
739 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
741 date_time(tm
, now
, 12);
744 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
746 date_time(tm
, now
, 17);
749 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
760 tm
->tm_hour
= (hour
% 12) + 12;
763 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
774 tm
->tm_hour
= (hour
% 12);
777 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
783 static const struct special
{
785 void (*fn
)(struct tm
*, struct tm
*, int *);
787 { "yesterday", date_yesterday
},
788 { "noon", date_noon
},
789 { "midnight", date_midnight
},
793 { "never", date_never
},
798 static const char *number_name
[] = {
799 "zero", "one", "two", "three", "four",
800 "five", "six", "seven", "eight", "nine", "ten",
803 static const struct typelen
{
810 { "days", 24*60*60 },
811 { "weeks", 7*24*60*60 },
815 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
817 const struct typelen
*tl
;
818 const struct special
*s
;
819 const char *end
= date
;
822 while (isalpha(*++end
));
825 for (i
= 0; i
< 12; i
++) {
826 int match
= match_string(date
, month_names
[i
]);
834 for (s
= special
; s
->name
; s
++) {
835 int len
= strlen(s
->name
);
836 if (match_string(date
, s
->name
) == len
) {
844 for (i
= 1; i
< 11; i
++) {
845 int len
= strlen(number_name
[i
]);
846 if (match_string(date
, number_name
[i
]) == len
) {
852 if (match_string(date
, "last") == 4) {
861 int len
= strlen(tl
->type
);
862 if (match_string(date
, tl
->type
) >= len
-1) {
863 update_tm(tm
, now
, tl
->length
* *num
);
871 for (i
= 0; i
< 7; i
++) {
872 int match
= match_string(date
, weekday_names
[i
]);
874 int diff
, n
= *num
-1;
877 diff
= tm
->tm_wday
- i
;
882 update_tm(tm
, now
, diff
* 24 * 60 * 60);
888 if (match_string(date
, "months") >= 5) {
890 update_tm(tm
, now
, 0); /* fill in date fields if needed */
891 n
= tm
->tm_mon
- *num
;
902 if (match_string(date
, "years") >= 4) {
903 update_tm(tm
, now
, 0); /* fill in date fields if needed */
913 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
)
916 unsigned long number
= strtoul(date
, &end
, 10);
923 if (isdigit(end
[1])) {
924 int match
= match_multi_number(number
, *end
, date
, end
, tm
);
930 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
931 if (date
[0] != '0' || end
- date
<= 2)
937 * Do we have a pending number at the end, or when
938 * we see a new one? Let's assume it's a month day,
939 * as in "Dec 6, 1992"
941 static void pending_number(struct tm
*tm
, int *num
)
947 if (tm
->tm_mday
< 0 && number
< 32)
948 tm
->tm_mday
= number
;
949 else if (tm
->tm_mon
< 0 && number
< 13)
950 tm
->tm_mon
= number
-1;
951 else if (tm
->tm_year
< 0) {
952 if (number
> 1969 && number
< 2100)
953 tm
->tm_year
= number
- 1900;
954 else if (number
> 69 && number
< 100)
955 tm
->tm_year
= number
;
956 else if (number
< 38)
957 tm
->tm_year
= 100 + number
;
958 /* We screw up for number = 00 ? */
963 static unsigned long approxidate_str(const char *date
,
964 const struct timeval
*tv
,
972 time_sec
= tv
->tv_sec
;
973 localtime_r(&time_sec
, &tm
);
981 unsigned char c
= *date
;
986 pending_number(&tm
, &number
);
987 date
= approxidate_digit(date
-1, &tm
, &number
);
992 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
994 pending_number(&tm
, &number
);
997 return update_tm(&tm
, &now
, 0);
1000 unsigned long approxidate_relative(const char *date
, const struct timeval
*tv
)
1002 unsigned long timestamp
;
1006 if (!parse_date_basic(date
, ×tamp
, &offset
))
1008 return approxidate_str(date
, tv
, &errors
);
1011 unsigned long approxidate_careful(const char *date
, int *error_ret
)
1014 unsigned long timestamp
;
1020 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1025 gettimeofday(&tv
, NULL
);
1026 return approxidate_str(date
, &tv
, error_ret
);