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 const char *show_date(unsigned long time
, int tz
)
49 static char timebuf
[200];
52 minutes
= tz
< 0 ? -tz
: tz
;
53 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
54 minutes
= tz
< 0 ? -minutes
: minutes
;
55 t
= time
+ minutes
* 60;
59 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
60 weekday_names
[tm
->tm_wday
],
61 month_names
[tm
->tm_mon
],
63 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
64 tm
->tm_year
+ 1900, tz
);
69 * Check these. And note how it doesn't do the summer-time conversion.
71 * In my world, it's always summer, and things are probably a bit off
78 } timezone_names
[] = {
79 { "IDLW", -12, 0, }, /* International Date Line West */
80 { "NT", -11, 0, }, /* Nome */
81 { "CAT", -10, 0, }, /* Central Alaska */
82 { "HST", -10, 0, }, /* Hawaii Standard */
83 { "HDT", -10, 1, }, /* Hawaii Daylight */
84 { "YST", -9, 0, }, /* Yukon Standard */
85 { "YDT", -9, 1, }, /* Yukon Daylight */
86 { "PST", -8, 0, }, /* Pacific Standard */
87 { "PDT", -8, 1, }, /* Pacific Daylight */
88 { "MST", -7, 0, }, /* Mountain Standard */
89 { "MDT", -7, 1, }, /* Mountain Daylight */
90 { "CST", -6, 0, }, /* Central Standard */
91 { "CDT", -6, 1, }, /* Central Daylight */
92 { "EST", -5, 0, }, /* Eastern Standard */
93 { "EDT", -5, 1, }, /* Eastern Daylight */
94 { "AST", -3, 0, }, /* Atlantic Standard */
95 { "ADT", -3, 1, }, /* Atlantic Daylight */
96 { "WAT", -1, 0, }, /* West Africa */
98 { "GMT", 0, 0, }, /* Greenwich Mean */
99 { "UTC", 0, 0, }, /* Universal (Coordinated) */
101 { "WET", 0, 0, }, /* Western European */
102 { "BST", 0, 1, }, /* British Summer */
103 { "CET", +1, 0, }, /* Central European */
104 { "MET", +1, 0, }, /* Middle European */
105 { "MEWT", +1, 0, }, /* Middle European Winter */
106 { "MEST", +1, 1, }, /* Middle European Summer */
107 { "CEST", +1, 1, }, /* Central European Summer */
108 { "MESZ", +1, 1, }, /* Middle European Summer */
109 { "FWT", +1, 0, }, /* French Winter */
110 { "FST", +1, 1, }, /* French Summer */
111 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
112 { "EEST", +2, 1, }, /* Eastern European Daylight */
113 { "WAST", +7, 0, }, /* West Australian Standard */
114 { "WADT", +7, 1, }, /* West Australian Daylight */
115 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
116 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
117 { "EAST", +10, 0, }, /* Eastern Australian Standard */
118 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
119 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
120 { "NZT", +11, 0, }, /* New Zealand */
121 { "NZST", +11, 0, }, /* New Zealand Standard */
122 { "NZDT", +11, 1, }, /* New Zealand Daylight */
123 { "IDLE", +12, 0, }, /* International Date Line East */
126 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
128 static int match_string(const char *date
, const char *str
)
132 for (i
= 0; *date
; date
++, str
++, i
++) {
135 if (toupper(*date
) == toupper(*str
))
144 static int skip_alpha(const char *date
)
149 } while (isalpha(date
[i
]));
154 * Parse month, weekday, or timezone name
156 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
160 for (i
= 0; i
< 12; i
++) {
161 int match
= match_string(date
, month_names
[i
]);
168 for (i
= 0; i
< 7; i
++) {
169 int match
= match_string(date
, weekday_names
[i
]);
176 for (i
= 0; i
< NR_TZ
; i
++) {
177 int match
= match_string(date
, timezone_names
[i
].name
);
179 int off
= timezone_names
[i
].offset
;
181 /* This is bogus, but we like summer */
182 off
+= timezone_names
[i
].dst
;
184 /* Only use the tz name offset if we don't have anything better */
192 if (match_string(date
, "PM") == 2) {
193 if (tm
->tm_hour
> 0 && tm
->tm_hour
< 12)
199 return skip_alpha(date
);
202 static int is_date(int year
, int month
, int day
, struct tm
*tm
)
204 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
206 tm
->tm_mon
= month
-1;
210 if (year
>= 1970 && year
< 2100) {
212 } else if (year
> 70 && year
< 100) {
214 } else if (year
< 38) {
219 tm
->tm_mon
= month
-1;
227 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
231 num2
= strtol(end
+1, &end
, 10);
233 if (*end
== c
&& isdigit(end
[1]))
234 num3
= strtol(end
+1, &end
, 10);
241 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
253 if (is_date(num
, num2
, num3
, tm
))
256 if (is_date(num
, num3
, num2
, tm
))
260 if (is_date(num3
, num2
, num
, tm
))
263 if (is_date(num3
, num
, num2
, tm
))
271 * We've seen a digit. Time? Year? Date?
273 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
279 num
= strtoul(date
, &end
, 10);
282 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
284 if (num
> 946684800) {
286 if (gmtime_r(&time
, tm
)) {
293 * Check for special formats: num[:-/]num[same]num
299 if (isdigit(end
[1])) {
300 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
307 * None of the special formats? Try to guess what
308 * the number meant. We use the number of digits
309 * to make a more educated guess..
314 } while (isdigit(date
[n
]));
316 /* Four-digit year or a timezone? */
318 if (num
<= 1200 && *offset
== -1) {
319 unsigned int minutes
= num
% 100;
320 unsigned int hours
= num
/ 100;
321 *offset
= hours
*60 + minutes
;
322 } else if (num
> 1900 && num
< 2100)
323 tm
->tm_year
= num
- 1900;
328 * NOTE! We will give precedence to day-of-month over month or
329 * year numebers in the 1-12 range. So 05 is always "mday 5",
330 * unless we already have a mday..
332 * IOW, 01 Apr 05 parses as "April 1st, 2005".
334 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
339 /* Two-digit year? */
340 if (n
== 2 && tm
->tm_year
< 0) {
341 if (num
< 10 && tm
->tm_mday
>= 0) {
342 tm
->tm_year
= num
+ 100;
351 if (num
> 0 && num
< 32) {
353 } else if (num
> 1900) {
354 tm
->tm_year
= num
- 1900;
355 } else if (num
> 70) {
357 } else if (num
> 0 && num
< 13) {
364 static int match_tz(const char *date
, int *offp
)
367 int offset
= strtoul(date
+1, &end
, 10);
369 int n
= end
- date
- 1;
375 * Don't accept any random crap.. At least 3 digits, and
376 * a valid minute. We might want to check that the minutes
377 * are divisible by 30 or something too.
379 if (min
< 60 && n
> 2) {
380 offset
= hour
*60+min
;
389 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
397 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
400 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
401 (i.e. English) day/month names, and it doesn't work correctly with %z. */
402 int parse_date(const char *date
, char *result
, int maxlen
)
408 memset(&tm
, 0, sizeof(tm
));
418 unsigned char c
= *date
;
420 /* Stop at end of string or newline */
425 match
= match_alpha(date
, &tm
, &offset
);
427 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
428 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
429 match
= match_tz(date
, &offset
);
439 /* mktime uses local timezone */
440 then
= my_mktime(&tm
);
442 offset
= (then
- mktime(&tm
)) / 60;
449 return date_string(then
, offset
, result
, maxlen
);
452 void datestamp(char *buf
, int bufsize
)
459 offset
= my_mktime(localtime(&now
)) - now
;
462 date_string(now
, offset
, buf
, bufsize
);
465 static void update_tm(struct tm
*tm
, unsigned long sec
)
467 time_t n
= mktime(tm
) - sec
;
471 static void date_yesterday(struct tm
*tm
, int *num
)
473 update_tm(tm
, 24*60*60);
476 static void date_time(struct tm
*tm
, int hour
)
478 if (tm
->tm_hour
< hour
)
479 date_yesterday(tm
, NULL
);
485 static void date_midnight(struct tm
*tm
, int *num
)
490 static void date_noon(struct tm
*tm
, int *num
)
495 static void date_tea(struct tm
*tm
, int *num
)
500 static const struct special
{
502 void (*fn
)(struct tm
*, int *);
504 { "yesterday", date_yesterday
},
505 { "noon", date_noon
},
506 { "midnight", date_midnight
},
511 static const char *number_name
[] = {
512 "zero", "one", "two", "three", "four",
513 "five", "six", "seven", "eight", "nine", "ten",
516 static const struct typelen
{
523 { "days", 24*60*60 },
524 { "weeks", 7*24*60*60 },
528 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
530 const struct typelen
*tl
;
531 const struct special
*s
;
532 const char *end
= date
;
535 while (isalpha(*++end
))
538 for (i
= 0; i
< 12; i
++) {
539 int match
= match_string(date
, month_names
[i
]);
546 for (s
= special
; s
->name
; s
++) {
547 int len
= strlen(s
->name
);
548 if (match_string(date
, s
->name
) == len
) {
555 for (i
= 1; i
< 11; i
++) {
556 int len
= strlen(number_name
[i
]);
557 if (match_string(date
, number_name
[i
]) == len
) {
562 if (match_string(date
, "last") == 4)
569 int len
= strlen(tl
->type
);
570 if (match_string(date
, tl
->type
) >= len
-1) {
571 update_tm(tm
, tl
->length
* *num
);
578 for (i
= 0; i
< 7; i
++) {
579 int match
= match_string(date
, weekday_names
[i
]);
581 int diff
, n
= *num
-1;
584 diff
= tm
->tm_wday
- i
;
589 update_tm(tm
, diff
* 24 * 60 * 60);
594 if (match_string(date
, "months") >= 5) {
595 int n
= tm
->tm_mon
- *num
;
605 if (match_string(date
, "years") >= 4) {
614 unsigned long approxidate(const char *date
)
621 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
622 return strtoul(buffer
, NULL
, 10);
624 gettimeofday(&tv
, NULL
);
625 localtime_r(&tv
.tv_sec
, &tm
);
628 unsigned char c
= *date
;
634 number
= strtoul(date
-1, &end
, 10);
639 date
= approxidate_alpha(date
-1, &tm
, &number
);
641 if (number
> 0 && number
< 32)
643 if (tm
.tm_mon
> now
.tm_mon
)