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 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 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
28 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
31 static const char *month_names
[] = {
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
36 static const char *weekday_names
[] = {
37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
40 static time_t gm_time_t(unsigned long time
, int tz
)
44 minutes
= tz
< 0 ? -tz
: tz
;
45 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
46 minutes
= tz
< 0 ? -minutes
: minutes
;
47 return time
+ minutes
* 60;
51 * The "tz" thing is passed in as this strange "decimal parse of tz"
52 * thing, which means that tz -0100 is passed in as the integer -100,
53 * even though it means "sixty minutes off"
55 static struct tm
*time_to_tm(unsigned long time
, int tz
)
57 time_t t
= gm_time_t(time
, tz
);
62 * What value of "tz" was in effect back then at "time" in the
65 static int local_tzoffset(unsigned long time
)
73 t_local
= tm_to_time_t(&tm
);
82 offset
/= 60; /* in minutes */
83 offset
= (offset
% 60) + ((offset
/ 60) * 100);
84 return offset
* eastwest
;
87 const char *show_date(unsigned long time
, int tz
, enum date_mode mode
)
90 static char timebuf
[200];
92 if (mode
== DATE_RAW
) {
93 snprintf(timebuf
, sizeof(timebuf
), "%lu %+05d", time
, tz
);
97 if (mode
== DATE_RELATIVE
) {
100 gettimeofday(&now
, NULL
);
101 if (now
.tv_sec
< time
)
102 return "in the future";
103 diff
= now
.tv_sec
- time
;
105 snprintf(timebuf
, sizeof(timebuf
), "%lu seconds ago", diff
);
108 /* Turn it into minutes */
109 diff
= (diff
+ 30) / 60;
111 snprintf(timebuf
, sizeof(timebuf
), "%lu minutes ago", diff
);
114 /* Turn it into hours */
115 diff
= (diff
+ 30) / 60;
117 snprintf(timebuf
, sizeof(timebuf
), "%lu hours ago", diff
);
120 /* We deal with number of days from here on */
121 diff
= (diff
+ 12) / 24;
123 snprintf(timebuf
, sizeof(timebuf
), "%lu days ago", diff
);
126 /* Say weeks for the past 10 weeks or so */
128 snprintf(timebuf
, sizeof(timebuf
), "%lu weeks ago", (diff
+ 3) / 7);
131 /* Say months for the past 12 months or so */
133 snprintf(timebuf
, sizeof(timebuf
), "%lu months ago", (diff
+ 15) / 30);
136 /* Else fall back on absolute format.. */
139 if (mode
== DATE_LOCAL
)
140 tz
= local_tzoffset(time
);
142 tm
= time_to_tm(time
, tz
);
145 if (mode
== DATE_SHORT
)
146 sprintf(timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
147 tm
->tm_mon
+ 1, tm
->tm_mday
);
148 else if (mode
== DATE_ISO8601
)
149 sprintf(timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
153 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
155 else if (mode
== DATE_RFC2822
)
156 sprintf(timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
157 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
158 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
159 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
161 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
162 weekday_names
[tm
->tm_wday
],
163 month_names
[tm
->tm_mon
],
165 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
167 (mode
== DATE_LOCAL
) ? 0 : ' ',
173 * Check these. And note how it doesn't do the summer-time conversion.
175 * In my world, it's always summer, and things are probably a bit off
178 static const struct {
182 } timezone_names
[] = {
183 { "IDLW", -12, 0, }, /* International Date Line West */
184 { "NT", -11, 0, }, /* Nome */
185 { "CAT", -10, 0, }, /* Central Alaska */
186 { "HST", -10, 0, }, /* Hawaii Standard */
187 { "HDT", -10, 1, }, /* Hawaii Daylight */
188 { "YST", -9, 0, }, /* Yukon Standard */
189 { "YDT", -9, 1, }, /* Yukon Daylight */
190 { "PST", -8, 0, }, /* Pacific Standard */
191 { "PDT", -8, 1, }, /* Pacific Daylight */
192 { "MST", -7, 0, }, /* Mountain Standard */
193 { "MDT", -7, 1, }, /* Mountain Daylight */
194 { "CST", -6, 0, }, /* Central Standard */
195 { "CDT", -6, 1, }, /* Central Daylight */
196 { "EST", -5, 0, }, /* Eastern Standard */
197 { "EDT", -5, 1, }, /* Eastern Daylight */
198 { "AST", -3, 0, }, /* Atlantic Standard */
199 { "ADT", -3, 1, }, /* Atlantic Daylight */
200 { "WAT", -1, 0, }, /* West Africa */
202 { "GMT", 0, 0, }, /* Greenwich Mean */
203 { "UTC", 0, 0, }, /* Universal (Coordinated) */
205 { "WET", 0, 0, }, /* Western European */
206 { "BST", 0, 1, }, /* British Summer */
207 { "CET", +1, 0, }, /* Central European */
208 { "MET", +1, 0, }, /* Middle European */
209 { "MEWT", +1, 0, }, /* Middle European Winter */
210 { "MEST", +1, 1, }, /* Middle European Summer */
211 { "CEST", +1, 1, }, /* Central European Summer */
212 { "MESZ", +1, 1, }, /* Middle European Summer */
213 { "FWT", +1, 0, }, /* French Winter */
214 { "FST", +1, 1, }, /* French Summer */
215 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
216 { "EEST", +2, 1, }, /* Eastern European Daylight */
217 { "WAST", +7, 0, }, /* West Australian Standard */
218 { "WADT", +7, 1, }, /* West Australian Daylight */
219 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
220 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
221 { "EAST", +10, 0, }, /* Eastern Australian Standard */
222 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
223 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
224 { "NZT", +12, 0, }, /* New Zealand */
225 { "NZST", +12, 0, }, /* New Zealand Standard */
226 { "NZDT", +12, 1, }, /* New Zealand Daylight */
227 { "IDLE", +12, 0, }, /* International Date Line East */
230 static int match_string(const char *date
, const char *str
)
234 for (i
= 0; *date
; date
++, str
++, i
++) {
237 if (toupper(*date
) == toupper(*str
))
246 static int skip_alpha(const char *date
)
251 } while (isalpha(date
[i
]));
256 * Parse month, weekday, or timezone name
258 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
262 for (i
= 0; i
< 12; i
++) {
263 int match
= match_string(date
, month_names
[i
]);
270 for (i
= 0; i
< 7; i
++) {
271 int match
= match_string(date
, weekday_names
[i
]);
278 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
279 int match
= match_string(date
, timezone_names
[i
].name
);
281 int off
= timezone_names
[i
].offset
;
283 /* This is bogus, but we like summer */
284 off
+= timezone_names
[i
].dst
;
286 /* Only use the tz name offset if we don't have anything better */
294 if (match_string(date
, "PM") == 2) {
295 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
299 if (match_string(date
, "AM") == 2) {
300 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
305 return skip_alpha(date
);
308 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
310 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
311 struct tm check
= *tm
;
312 struct tm
*r
= (now_tm
? &check
: tm
);
315 r
->tm_mon
= month
- 1;
320 r
->tm_year
= now_tm
->tm_year
;
322 else if (year
>= 1970 && year
< 2100)
323 r
->tm_year
= year
- 1900;
324 else if (year
> 70 && year
< 100)
327 r
->tm_year
= year
+ 100;
333 specified
= tm_to_time_t(r
);
335 /* Be it commit time or author time, it does not make
336 * sense to specify timestamp way into the future. Make
337 * sure it is not later than ten days from now...
339 if (now
+ 10*24*3600 < specified
)
341 tm
->tm_mon
= r
->tm_mon
;
342 tm
->tm_mday
= r
->tm_mday
;
344 tm
->tm_year
= r
->tm_year
;
350 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
354 struct tm
*refuse_future
;
357 num2
= strtol(end
+1, &end
, 10);
359 if (*end
== c
&& isdigit(end
[1]))
360 num3
= strtol(end
+1, &end
, 10);
367 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
379 refuse_future
= NULL
;
380 if (gmtime_r(&now
, &now_tm
))
381 refuse_future
= &now_tm
;
385 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
388 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
391 /* Our eastern European friends say dd.mm.yy[yy]
392 * is the norm there, so giving precedence to
393 * mm/dd/yy[yy] form only when separator is not '.'
396 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
398 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
399 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
401 /* Funny European mm.dd.yy */
403 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
410 /* Have we filled in any part of the time/date yet? */
411 static inline int nodate(struct tm
*tm
)
413 return tm
->tm_year
< 0 &&
416 !(tm
->tm_hour
| tm
->tm_min
| tm
->tm_sec
);
420 * We've seen a digit. Time? Year? Date?
422 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
428 num
= strtoul(date
, &end
, 10);
431 * Seconds since 1970? We trigger on that for any numbers with
432 * more than 8 digits. This is because we don't want to rule out
433 * numbers like 20070606 as a YYYYMMDD date.
435 if (num
>= 100000000 && nodate(tm
)) {
437 if (gmtime_r(&time
, tm
)) {
444 * Check for special formats: num[-.:/]num[same]num
451 if (isdigit(end
[1])) {
452 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
459 * None of the special formats? Try to guess what
460 * the number meant. We use the number of digits
461 * to make a more educated guess..
466 } while (isdigit(date
[n
]));
468 /* Four-digit year or a timezone? */
470 if (num
<= 1400 && *offset
== -1) {
471 unsigned int minutes
= num
% 100;
472 unsigned int hours
= num
/ 100;
473 *offset
= hours
*60 + minutes
;
474 } else if (num
> 1900 && num
< 2100)
475 tm
->tm_year
= num
- 1900;
480 * Ignore lots of numerals. We took care of 4-digit years above.
481 * Days or months must be one or two digits.
487 * NOTE! We will give precedence to day-of-month over month or
488 * year numbers in the 1-12 range. So 05 is always "mday 5",
489 * unless we already have a mday..
491 * IOW, 01 Apr 05 parses as "April 1st, 2005".
493 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
498 /* Two-digit year? */
499 if (n
== 2 && tm
->tm_year
< 0) {
500 if (num
< 10 && tm
->tm_mday
>= 0) {
501 tm
->tm_year
= num
+ 100;
510 if (num
> 0 && num
< 32) {
512 } else if (num
> 0 && num
< 13) {
519 static int match_tz(const char *date
, int *offp
)
522 int offset
= strtoul(date
+1, &end
, 10);
524 int n
= end
- date
- 1;
530 * Don't accept any random crap.. At least 3 digits, and
531 * a valid minute. We might want to check that the minutes
532 * are divisible by 30 or something too.
534 if (min
< 60 && n
> 2) {
535 offset
= hour
*60+min
;
544 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
552 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
555 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
556 (i.e. English) day/month names, and it doesn't work correctly with %z. */
557 int parse_date(const char *date
, char *result
, int maxlen
)
563 memset(&tm
, 0, sizeof(tm
));
573 unsigned char c
= *date
;
575 /* Stop at end of string or newline */
580 match
= match_alpha(date
, &tm
, &offset
);
582 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
583 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
584 match
= match_tz(date
, &offset
);
594 /* mktime uses local timezone */
595 then
= tm_to_time_t(&tm
);
597 offset
= (then
- mktime(&tm
)) / 60;
604 return date_string(then
, offset
, result
, maxlen
);
607 enum date_mode
parse_date_format(const char *format
)
609 if (!strcmp(format
, "relative"))
610 return DATE_RELATIVE
;
611 else if (!strcmp(format
, "iso8601") ||
612 !strcmp(format
, "iso"))
614 else if (!strcmp(format
, "rfc2822") ||
615 !strcmp(format
, "rfc"))
617 else if (!strcmp(format
, "short"))
619 else if (!strcmp(format
, "local"))
621 else if (!strcmp(format
, "default"))
623 else if (!strcmp(format
, "raw"))
626 die("unknown date format %s", format
);
629 void datestamp(char *buf
, int bufsize
)
636 offset
= tm_to_time_t(localtime(&now
)) - now
;
639 date_string(now
, offset
, buf
, bufsize
);
642 static void update_tm(struct tm
*tm
, unsigned long sec
)
644 time_t n
= mktime(tm
) - sec
;
648 static void date_yesterday(struct tm
*tm
, int *num
)
650 update_tm(tm
, 24*60*60);
653 static void date_time(struct tm
*tm
, int hour
)
655 if (tm
->tm_hour
< hour
)
656 date_yesterday(tm
, NULL
);
662 static void date_midnight(struct tm
*tm
, int *num
)
667 static void date_noon(struct tm
*tm
, int *num
)
672 static void date_tea(struct tm
*tm
, int *num
)
677 static void date_pm(struct tm
*tm
, int *num
)
688 tm
->tm_hour
= (hour
% 12) + 12;
691 static void date_am(struct tm
*tm
, int *num
)
702 tm
->tm_hour
= (hour
% 12);
705 static void date_never(struct tm
*tm
, int *num
)
711 static const struct special
{
713 void (*fn
)(struct tm
*, int *);
715 { "yesterday", date_yesterday
},
716 { "noon", date_noon
},
717 { "midnight", date_midnight
},
721 { "never", date_never
},
725 static const char *number_name
[] = {
726 "zero", "one", "two", "three", "four",
727 "five", "six", "seven", "eight", "nine", "ten",
730 static const struct typelen
{
737 { "days", 24*60*60 },
738 { "weeks", 7*24*60*60 },
742 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
744 const struct typelen
*tl
;
745 const struct special
*s
;
746 const char *end
= date
;
749 while (isalpha(*++end
));
752 for (i
= 0; i
< 12; i
++) {
753 int match
= match_string(date
, month_names
[i
]);
760 for (s
= special
; s
->name
; s
++) {
761 int len
= strlen(s
->name
);
762 if (match_string(date
, s
->name
) == len
) {
769 for (i
= 1; i
< 11; i
++) {
770 int len
= strlen(number_name
[i
]);
771 if (match_string(date
, number_name
[i
]) == len
) {
776 if (match_string(date
, "last") == 4)
783 int len
= strlen(tl
->type
);
784 if (match_string(date
, tl
->type
) >= len
-1) {
785 update_tm(tm
, tl
->length
* *num
);
792 for (i
= 0; i
< 7; i
++) {
793 int match
= match_string(date
, weekday_names
[i
]);
795 int diff
, n
= *num
-1;
798 diff
= tm
->tm_wday
- i
;
803 update_tm(tm
, diff
* 24 * 60 * 60);
808 if (match_string(date
, "months") >= 5) {
809 int n
= tm
->tm_mon
- *num
;
819 if (match_string(date
, "years") >= 4) {
828 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
)
831 unsigned long number
= strtoul(date
, &end
, 10);
838 if (isdigit(end
[1])) {
839 int match
= match_multi_number(number
, *end
, date
, end
, tm
);
845 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
846 if (date
[0] != '0' || end
- date
<= 2)
851 unsigned long approxidate(const char *date
)
858 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
859 return strtoul(buffer
, NULL
, 10);
861 gettimeofday(&tv
, NULL
);
862 localtime_r(&tv
.tv_sec
, &tm
);
865 unsigned char c
= *date
;
870 date
= approxidate_digit(date
-1, &tm
, &number
);
874 date
= approxidate_alpha(date
-1, &tm
, &number
);
876 if (number
> 0 && number
< 32)
878 if (tm
.tm_mon
> now
.tm_mon
&& tm
.tm_year
== now
.tm_year
)