2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 static time_t my_mktime(struct tm *tm)
15 static const int mdays[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
18 int year = tm->tm_year - 70;
19 int month = tm->tm_mon;
20 int day = tm->tm_mday;
22 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
24 if (month < 0 || month > 11) /* array bounds */
26 if (month < 2 || (year + 2) % 4)
28 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
29 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
32 static const char *month_names[] = {
33 "January", "February", "March", "April", "May", "June",
34 "July", "August", "September", "October", "November", "December"
37 static const char *weekday_names[] = {
38 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
42 * Check these. And note how it doesn't do the summer-time conversion.
44 * In my world, it's always summer, and things are probably a bit off
50 } timezone_names[] = {
51 { "IDLW", -12 }, /* International Date Line West */
52 { "NT", -11 }, /* Nome */
53 { "CAT", -10 }, /* Central Alaska */
54 { "HST", -10 }, /* Hawaii Standard */
55 { "HDT", -9 }, /* Hawaii Daylight */
56 { "YDT", -8 }, /* Yukon Daylight */
57 { "YST", -9 }, /* Yukon Standard */
58 { "PST", -8 }, /* Pacific Standard */
59 { "PDT", -7 }, /* Pacific Daylight */
60 { "MST", -7 }, /* Mountain Standard */
61 { "MDT", -6 }, /* Mountain Daylight */
62 { "CST", -6 }, /* Central Standard */
63 { "CDT", -5 }, /* Central Daylight */
64 { "EST", -5 }, /* Eastern Standard */
65 { "EDT", -4 }, /* Eastern Daylight */
66 { "AST", -3 }, /* Atlantic Standard */
67 { "ADT", -2 }, /* Atlantic Daylight */
68 { "WAT", -1 }, /* West Africa */
70 { "GMT", 0 }, /* Greenwich Mean */
71 { "UTC", 0 }, /* Universal (Coordinated) */
73 { "WET", 0 }, /* Western European */
74 { "BST", 0 }, /* British Summer */
75 { "CET", +1 }, /* Central European */
76 { "MET", +1 }, /* Middle European */
77 { "MEWT", +1 }, /* Middle European Winter */
78 { "MEST", +2 }, /* Middle European Summer */
79 { "CEST", +2 }, /* Central European Summer */
80 { "MESZ", +1 }, /* Middle European Summer */
81 { "FWT", +1 }, /* French Winter */
82 { "FST", +2 }, /* French Summer */
83 { "EET", +2 }, /* Eastern Europe, USSR Zone 1 */
84 { "WAST", +7 }, /* West Australian Standard */
85 { "WADT", +8 }, /* West Australian Daylight */
86 { "CCT", +8 }, /* China Coast, USSR Zone 7 */
87 { "JST", +9 }, /* Japan Standard, USSR Zone 8 */
88 { "EAST", +10 }, /* Eastern Australian Standard */
89 { "EADT", +11 }, /* Eastern Australian Daylight */
90 { "GST", +10 }, /* Guam Standard, USSR Zone 9 */
91 { "NZT", +11 }, /* New Zealand */
92 { "NZST", +11 }, /* New Zealand Standard */
93 { "NZDT", +12 }, /* New Zealand Daylight */
94 { "IDLE", +12 }, /* International Date Line East */
97 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
99 static int match_string(const char *date, const char *str)
103 for (i = 0; *date; date++, str++, i++) {
106 if (toupper(*date) == toupper(*str))
116 * Parse month, weekday, or timezone name
118 static int match_alpha(const char *date, struct tm *tm, int *offset)
122 for (i = 0; i < 12; i++) {
123 int match = match_string(date, month_names[i]);
130 for (i = 0; i < 7; i++) {
131 int match = match_string(date, weekday_names[i]);
138 for (i = 0; i < NR_TZ; i++) {
139 int match = match_string(date, timezone_names[i].name);
141 *offset = 60*timezone_names[i].offset;
150 static int match_digit(char *date, struct tm *tm, int *offset)
153 unsigned long num, num2, num3;
155 num = strtoul(date, &end, 10);
157 /* Time? num:num[:num] */
158 if (num < 24 && end[0] == ':' && isdigit(end[1])) {
160 num = strtoul(end+1, &end, 10);
163 if (end[0] == ':' && isdigit(end[1])) {
164 num = strtoul(end+1, &end, 10);
172 /* Year? Day of month? Numeric date-string?*/
176 if (num > 0 && num < 32) {
181 tm->tm_year = num - 1900;
192 if (num && num < 32 && isdigit(end[1])) {
193 num2 = strtoul(end+1, &end, 10);
194 if (!num2 || num2 > 31)
203 tm->tm_mon = num - 1;
205 if (*end == c && isdigit(end[1])) {
206 num3 = strtoul(end, &end, 10);
219 static int match_tz(char *date, int *offp)
222 int offset = strtoul(date+1, &end, 10);
228 offset = hour*60+min;
236 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
237 (i.e. English) day/month names, and it doesn't work correctly with %z. */
238 void parse_date(char *date, char *result, int maxlen)
244 memset(&tm, 0, sizeof(tm));
252 unsigned char c = *date;
254 /* Stop at end of string or newline */
259 match = match_alpha(date, &tm, &offset);
261 match = match_digit(date, &tm, &offset);
262 else if ((c == '-' || c == '+') && isdigit(date[1]))
263 match = match_tz(date, &offset);
273 then = my_mktime(&tm); /* mktime uses local timezone */
279 snprintf(result, maxlen, "%lu %+03d%02d", then, offset/60, offset % 60);
282 void datestamp(char *buf, int bufsize)
289 offset = my_mktime(localtime(&now)) - now;
292 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);