2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 static time_t my_mktime(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"
41 * The "tz" thing is passed in as this strange "decimal parse of tz"
42 * thing, which means that tz -0100 is passed in as the integer -100,
43 * even though it means "sixty minutes off"
45 static struct tm
*time_to_tm(unsigned long time
, int tz
)
50 minutes
= tz
< 0 ? -tz
: tz
;
51 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
52 minutes
= tz
< 0 ? -minutes
: minutes
;
53 t
= time
+ minutes
* 60;
57 const char *show_date(unsigned long time
, int tz
)
60 static char timebuf
[200];
62 tm
= time_to_tm(time
, tz
);
65 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
66 weekday_names
[tm
->tm_wday
],
67 month_names
[tm
->tm_mon
],
69 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
70 tm
->tm_year
+ 1900, tz
);
74 const char *show_rfc2822_date(unsigned long time
, int tz
)
77 static char timebuf
[200];
79 tm
= time_to_tm(time
, tz
);
82 sprintf(timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
83 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
84 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
85 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
90 * Check these. And note how it doesn't do the summer-time conversion.
92 * In my world, it's always summer, and things are probably a bit off
99 } timezone_names
[] = {
100 { "IDLW", -12, 0, }, /* International Date Line West */
101 { "NT", -11, 0, }, /* Nome */
102 { "CAT", -10, 0, }, /* Central Alaska */
103 { "HST", -10, 0, }, /* Hawaii Standard */
104 { "HDT", -10, 1, }, /* Hawaii Daylight */
105 { "YST", -9, 0, }, /* Yukon Standard */
106 { "YDT", -9, 1, }, /* Yukon Daylight */
107 { "PST", -8, 0, }, /* Pacific Standard */
108 { "PDT", -8, 1, }, /* Pacific Daylight */
109 { "MST", -7, 0, }, /* Mountain Standard */
110 { "MDT", -7, 1, }, /* Mountain Daylight */
111 { "CST", -6, 0, }, /* Central Standard */
112 { "CDT", -6, 1, }, /* Central Daylight */
113 { "EST", -5, 0, }, /* Eastern Standard */
114 { "EDT", -5, 1, }, /* Eastern Daylight */
115 { "AST", -3, 0, }, /* Atlantic Standard */
116 { "ADT", -3, 1, }, /* Atlantic Daylight */
117 { "WAT", -1, 0, }, /* West Africa */
119 { "GMT", 0, 0, }, /* Greenwich Mean */
120 { "UTC", 0, 0, }, /* Universal (Coordinated) */
122 { "WET", 0, 0, }, /* Western European */
123 { "BST", 0, 1, }, /* British Summer */
124 { "CET", +1, 0, }, /* Central European */
125 { "MET", +1, 0, }, /* Middle European */
126 { "MEWT", +1, 0, }, /* Middle European Winter */
127 { "MEST", +1, 1, }, /* Middle European Summer */
128 { "CEST", +1, 1, }, /* Central European Summer */
129 { "MESZ", +1, 1, }, /* Middle European Summer */
130 { "FWT", +1, 0, }, /* French Winter */
131 { "FST", +1, 1, }, /* French Summer */
132 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
133 { "EEST", +2, 1, }, /* Eastern European Daylight */
134 { "WAST", +7, 0, }, /* West Australian Standard */
135 { "WADT", +7, 1, }, /* West Australian Daylight */
136 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
137 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
138 { "EAST", +10, 0, }, /* Eastern Australian Standard */
139 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
140 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
141 { "NZT", +11, 0, }, /* New Zealand */
142 { "NZST", +11, 0, }, /* New Zealand Standard */
143 { "NZDT", +11, 1, }, /* New Zealand Daylight */
144 { "IDLE", +12, 0, }, /* International Date Line East */
147 static int match_string(const char *date
, const char *str
)
151 for (i
= 0; *date
; date
++, str
++, i
++) {
154 if (toupper(*date
) == toupper(*str
))
163 static int skip_alpha(const char *date
)
168 } while (isalpha(date
[i
]));
173 * Parse month, weekday, or timezone name
175 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
179 for (i
= 0; i
< 12; i
++) {
180 int match
= match_string(date
, month_names
[i
]);
187 for (i
= 0; i
< 7; i
++) {
188 int match
= match_string(date
, weekday_names
[i
]);
195 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
196 int match
= match_string(date
, timezone_names
[i
].name
);
198 int off
= timezone_names
[i
].offset
;
200 /* This is bogus, but we like summer */
201 off
+= timezone_names
[i
].dst
;
203 /* Only use the tz name offset if we don't have anything better */
211 if (match_string(date
, "PM") == 2) {
212 if (tm
->tm_hour
> 0 && tm
->tm_hour
< 12)
218 return skip_alpha(date
);
221 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
223 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
224 struct tm check
= *tm
;
225 struct tm
*r
= (now_tm
? &check
: tm
);
228 r
->tm_mon
= month
- 1;
233 r
->tm_year
= now_tm
->tm_year
;
235 else if (year
>= 1970 && year
< 2100)
236 r
->tm_year
= year
- 1900;
237 else if (year
> 70 && year
< 100)
240 r
->tm_year
= year
+ 100;
246 specified
= my_mktime(r
);
248 /* Be it commit time or author time, it does not make
249 * sense to specify timestamp way into the future. Make
250 * sure it is not later than ten days from now...
252 if (now
+ 10*24*3600 < specified
)
254 tm
->tm_mon
= r
->tm_mon
;
255 tm
->tm_mday
= r
->tm_mday
;
257 tm
->tm_year
= r
->tm_year
;
263 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
267 struct tm
*refuse_future
;
270 num2
= strtol(end
+1, &end
, 10);
272 if (*end
== c
&& isdigit(end
[1]))
273 num3
= strtol(end
+1, &end
, 10);
280 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
292 refuse_future
= NULL
;
293 if (gmtime_r(&now
, &now_tm
))
294 refuse_future
= &now_tm
;
298 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
301 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
304 /* Our eastern European friends say dd.mm.yy[yy]
305 * is the norm there, so giving precedence to
306 * mm/dd/yy[yy] form only when separator is not '.'
309 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
311 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
312 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
314 /* Funny European mm.dd.yy */
316 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
324 * We've seen a digit. Time? Year? Date?
326 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
332 num
= strtoul(date
, &end
, 10);
335 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
337 if (num
> 946684800) {
339 if (gmtime_r(&time
, tm
)) {
346 * Check for special formats: num[-.:/]num[same]num
353 if (isdigit(end
[1])) {
354 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
361 * None of the special formats? Try to guess what
362 * the number meant. We use the number of digits
363 * to make a more educated guess..
368 } while (isdigit(date
[n
]));
370 /* Four-digit year or a timezone? */
372 if (num
<= 1400 && *offset
== -1) {
373 unsigned int minutes
= num
% 100;
374 unsigned int hours
= num
/ 100;
375 *offset
= hours
*60 + minutes
;
376 } else if (num
> 1900 && num
< 2100)
377 tm
->tm_year
= num
- 1900;
382 * NOTE! We will give precedence to day-of-month over month or
383 * year numbers in the 1-12 range. So 05 is always "mday 5",
384 * unless we already have a mday..
386 * IOW, 01 Apr 05 parses as "April 1st, 2005".
388 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
393 /* Two-digit year? */
394 if (n
== 2 && tm
->tm_year
< 0) {
395 if (num
< 10 && tm
->tm_mday
>= 0) {
396 tm
->tm_year
= num
+ 100;
405 if (num
> 0 && num
< 32) {
407 } else if (num
> 1900) {
408 tm
->tm_year
= num
- 1900;
409 } else if (num
> 70) {
411 } else if (num
> 0 && num
< 13) {
418 static int match_tz(const char *date
, int *offp
)
421 int offset
= strtoul(date
+1, &end
, 10);
423 int n
= end
- date
- 1;
429 * Don't accept any random crap.. At least 3 digits, and
430 * a valid minute. We might want to check that the minutes
431 * are divisible by 30 or something too.
433 if (min
< 60 && n
> 2) {
434 offset
= hour
*60+min
;
443 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
451 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
454 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
455 (i.e. English) day/month names, and it doesn't work correctly with %z. */
456 int parse_date(const char *date
, char *result
, int maxlen
)
462 memset(&tm
, 0, sizeof(tm
));
472 unsigned char c
= *date
;
474 /* Stop at end of string or newline */
479 match
= match_alpha(date
, &tm
, &offset
);
481 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
482 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
483 match
= match_tz(date
, &offset
);
493 /* mktime uses local timezone */
494 then
= my_mktime(&tm
);
496 offset
= (then
- mktime(&tm
)) / 60;
503 return date_string(then
, offset
, result
, maxlen
);
506 void datestamp(char *buf
, int bufsize
)
513 offset
= my_mktime(localtime(&now
)) - now
;
516 date_string(now
, offset
, buf
, bufsize
);
519 static void update_tm(struct tm
*tm
, unsigned long sec
)
521 time_t n
= mktime(tm
) - sec
;
525 static void date_yesterday(struct tm
*tm
, int *num
)
527 update_tm(tm
, 24*60*60);
530 static void date_time(struct tm
*tm
, int hour
)
532 if (tm
->tm_hour
< hour
)
533 date_yesterday(tm
, NULL
);
539 static void date_midnight(struct tm
*tm
, int *num
)
544 static void date_noon(struct tm
*tm
, int *num
)
549 static void date_tea(struct tm
*tm
, int *num
)
554 static const struct special
{
556 void (*fn
)(struct tm
*, int *);
558 { "yesterday", date_yesterday
},
559 { "noon", date_noon
},
560 { "midnight", date_midnight
},
565 static const char *number_name
[] = {
566 "zero", "one", "two", "three", "four",
567 "five", "six", "seven", "eight", "nine", "ten",
570 static const struct typelen
{
577 { "days", 24*60*60 },
578 { "weeks", 7*24*60*60 },
582 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
584 const struct typelen
*tl
;
585 const struct special
*s
;
586 const char *end
= date
;
589 while (isalpha(*++end
))
592 for (i
= 0; i
< 12; i
++) {
593 int match
= match_string(date
, month_names
[i
]);
600 for (s
= special
; s
->name
; s
++) {
601 int len
= strlen(s
->name
);
602 if (match_string(date
, s
->name
) == len
) {
609 for (i
= 1; i
< 11; i
++) {
610 int len
= strlen(number_name
[i
]);
611 if (match_string(date
, number_name
[i
]) == len
) {
616 if (match_string(date
, "last") == 4)
623 int len
= strlen(tl
->type
);
624 if (match_string(date
, tl
->type
) >= len
-1) {
625 update_tm(tm
, tl
->length
* *num
);
632 for (i
= 0; i
< 7; i
++) {
633 int match
= match_string(date
, weekday_names
[i
]);
635 int diff
, n
= *num
-1;
638 diff
= tm
->tm_wday
- i
;
643 update_tm(tm
, diff
* 24 * 60 * 60);
648 if (match_string(date
, "months") >= 5) {
649 int n
= tm
->tm_mon
- *num
;
659 if (match_string(date
, "years") >= 4) {
668 unsigned long approxidate(const char *date
)
675 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
676 return strtoul(buffer
, NULL
, 10);
678 gettimeofday(&tv
, NULL
);
679 localtime_r(&tv
.tv_sec
, &tm
);
682 unsigned char c
= *date
;
688 number
= strtoul(date
-1, &end
, 10);
693 date
= approxidate_alpha(date
-1, &tm
, &number
);
695 if (number
> 0 && number
< 32)
697 if (tm
.tm_mon
> now
.tm_mon
&& tm
.tm_year
== now
.tm_year
)