2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static time_t my_mktime(struct tm
*tm
)
13 static const int mdays
[] = {
14 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
16 int year
= tm
->tm_year
- 70;
17 int month
= tm
->tm_mon
;
18 int day
= tm
->tm_mday
;
20 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
22 if (month
< 0 || month
> 11) /* array bounds */
24 if (month
< 2 || (year
+ 2) % 4)
26 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
27 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
30 static const char *month_names
[] = {
31 "January", "February", "March", "April", "May", "June",
32 "July", "August", "September", "October", "November", "December"
35 static const char *weekday_names
[] = {
36 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
40 * The "tz" thing is passed in as this strange "decimal parse of tz"
41 * thing, which means that tz -0100 is passed in as the integer -100,
42 * even though it means "sixty minutes off"
44 const char *show_date(unsigned long time
, int tz
)
48 static char timebuf
[200];
51 minutes
= tz
< 0 ? -tz
: tz
;
52 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
53 minutes
= tz
< 0 ? -minutes
: minutes
;
54 t
= time
+ minutes
* 60;
58 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
59 weekday_names
[tm
->tm_wday
],
60 month_names
[tm
->tm_mon
],
62 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
63 tm
->tm_year
+ 1900, tz
);
68 * Check these. And note how it doesn't do the summer-time conversion.
70 * In my world, it's always summer, and things are probably a bit off
77 } timezone_names
[] = {
78 { "IDLW", -12, 0, }, /* International Date Line West */
79 { "NT", -11, 0, }, /* Nome */
80 { "CAT", -10, 0, }, /* Central Alaska */
81 { "HST", -10, 0, }, /* Hawaii Standard */
82 { "HDT", -10, 1, }, /* Hawaii Daylight */
83 { "YST", -9, 0, }, /* Yukon Standard */
84 { "YDT", -9, 1, }, /* Yukon Daylight */
85 { "PST", -8, 0, }, /* Pacific Standard */
86 { "PDT", -8, 1, }, /* Pacific Daylight */
87 { "MST", -7, 0, }, /* Mountain Standard */
88 { "MDT", -7, 1, }, /* Mountain Daylight */
89 { "CST", -6, 0, }, /* Central Standard */
90 { "CDT", -6, 1, }, /* Central Daylight */
91 { "EST", -5, 0, }, /* Eastern Standard */
92 { "EDT", -5, 1, }, /* Eastern Daylight */
93 { "AST", -3, 0, }, /* Atlantic Standard */
94 { "ADT", -3, 1, }, /* Atlantic Daylight */
95 { "WAT", -1, 0, }, /* West Africa */
97 { "GMT", 0, 0, }, /* Greenwich Mean */
98 { "UTC", 0, 0, }, /* Universal (Coordinated) */
100 { "WET", 0, 0, }, /* Western European */
101 { "BST", 0, 1, }, /* British Summer */
102 { "CET", +1, 0, }, /* Central European */
103 { "MET", +1, 0, }, /* Middle European */
104 { "MEWT", +1, 0, }, /* Middle European Winter */
105 { "MEST", +1, 1, }, /* Middle European Summer */
106 { "CEST", +1, 1, }, /* Central European Summer */
107 { "MESZ", +1, 1, }, /* Middle European Summer */
108 { "FWT", +1, 0, }, /* French Winter */
109 { "FST", +1, 1, }, /* French Summer */
110 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
111 { "EEST", +2, 1, }, /* Eastern European Daylight */
112 { "WAST", +7, 0, }, /* West Australian Standard */
113 { "WADT", +7, 1, }, /* West Australian Daylight */
114 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
115 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
116 { "EAST", +10, 0, }, /* Eastern Australian Standard */
117 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
118 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
119 { "NZT", +11, 0, }, /* New Zealand */
120 { "NZST", +11, 0, }, /* New Zealand Standard */
121 { "NZDT", +11, 1, }, /* New Zealand Daylight */
122 { "IDLE", +12, 0, }, /* International Date Line East */
125 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
127 static int match_string(const char *date
, const char *str
)
131 for (i
= 0; *date
; date
++, str
++, i
++) {
134 if (toupper(*date
) == toupper(*str
))
143 static int skip_alpha(const char *date
)
148 } while (isalpha(date
[i
]));
153 * Parse month, weekday, or timezone name
155 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
159 for (i
= 0; i
< 12; i
++) {
160 int match
= match_string(date
, month_names
[i
]);
167 for (i
= 0; i
< 7; i
++) {
168 int match
= match_string(date
, weekday_names
[i
]);
175 for (i
= 0; i
< NR_TZ
; i
++) {
176 int match
= match_string(date
, timezone_names
[i
].name
);
178 int off
= timezone_names
[i
].offset
;
180 /* This is bogus, but we like summer */
181 off
+= timezone_names
[i
].dst
;
183 /* Only use the tz name offset if we don't have anything better */
191 if (match_string(date
, "PM") == 2) {
192 if (tm
->tm_hour
> 0 && tm
->tm_hour
< 12)
198 return skip_alpha(date
);
201 static int is_date(int year
, int month
, int day
, struct tm
*tm
)
203 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
205 tm
->tm_mon
= month
-1;
209 if (year
>= 1970 && year
< 2100) {
211 } else if (year
> 70 && year
< 100) {
213 } else if (year
< 38) {
218 tm
->tm_mon
= month
-1;
226 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
230 num2
= strtol(end
+1, &end
, 10);
232 if (*end
== c
&& isdigit(end
[1]))
233 num3
= strtol(end
+1, &end
, 10);
240 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
252 if (is_date(num
, num2
, num3
, tm
))
255 if (is_date(num
, num3
, num2
, tm
))
259 if (is_date(num3
, num2
, num
, tm
))
262 if (is_date(num3
, num
, num2
, tm
))
270 * We've seen a digit. Time? Year? Date?
272 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
278 num
= strtoul(date
, &end
, 10);
281 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
283 if (num
> 946684800) {
285 if (gmtime_r(&time
, tm
)) {
292 * Check for special formats: num[:-/]num[same]num
298 if (isdigit(end
[1])) {
299 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
306 * None of the special formats? Try to guess what
307 * the number meant. We use the number of digits
308 * to make a more educated guess..
313 } while (isdigit(date
[n
]));
315 /* Four-digit year or a timezone? */
317 if (num
<= 1200 && *offset
== -1) {
318 unsigned int minutes
= num
% 100;
319 unsigned int hours
= num
/ 100;
320 *offset
= hours
*60 + minutes
;
321 } else if (num
> 1900 && num
< 2100)
322 tm
->tm_year
= num
- 1900;
327 * NOTE! We will give precedence to day-of-month over month or
328 * year numebers in the 1-12 range. So 05 is always "mday 5",
329 * unless we already have a mday..
331 * IOW, 01 Apr 05 parses as "April 1st, 2005".
333 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
338 /* Two-digit year? */
339 if (n
== 2 && tm
->tm_year
< 0) {
340 if (num
< 10 && tm
->tm_mday
>= 0) {
341 tm
->tm_year
= num
+ 100;
350 if (num
> 0 && num
< 32) {
352 } else if (num
> 1900) {
353 tm
->tm_year
= num
- 1900;
354 } else if (num
> 70) {
356 } else if (num
> 0 && num
< 13) {
363 static int match_tz(const char *date
, int *offp
)
366 int offset
= strtoul(date
+1, &end
, 10);
368 int n
= end
- date
- 1;
374 * Don't accept any random crap.. At least 3 digits, and
375 * a valid minute. We might want to check that the minutes
376 * are divisible by 30 or something too.
378 if (min
< 60 && n
> 2) {
379 offset
= hour
*60+min
;
388 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
396 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
399 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
400 (i.e. English) day/month names, and it doesn't work correctly with %z. */
401 int parse_date(const char *date
, char *result
, int maxlen
)
407 memset(&tm
, 0, sizeof(tm
));
417 unsigned char c
= *date
;
419 /* Stop at end of string or newline */
424 match
= match_alpha(date
, &tm
, &offset
);
426 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
427 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
428 match
= match_tz(date
, &offset
);
438 /* mktime uses local timezone */
439 then
= my_mktime(&tm
);
441 offset
= (then
- mktime(&tm
)) / 60;
448 return date_string(then
, offset
, result
, maxlen
);
451 void datestamp(char *buf
, int bufsize
)
458 offset
= my_mktime(localtime(&now
)) - now
;
461 date_string(now
, offset
, buf
, bufsize
);