[PATCH] delta read
[git/jnareb-git.git] / date.c
blob1f366a67f8b0d282088a26d900a3031fdc3e6012
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <time.h>
13 #include "cache.h"
15 static time_t my_mktime(struct tm *tm)
17 static const int mdays[] = {
18 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
20 int year = tm->tm_year - 70;
21 int month = tm->tm_mon;
22 int day = tm->tm_mday;
24 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
25 return -1;
26 if (month < 0 || month > 11) /* array bounds */
27 return -1;
28 if (month < 2 || (year + 2) % 4)
29 day--;
30 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
31 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
34 static const char *month_names[] = {
35 "January", "February", "March", "April", "May", "June",
36 "July", "August", "September", "October", "November", "December"
39 static const char *weekday_names[] = {
40 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
44 * The "tz" thing is passed in as this strange "decimal parse of tz"
45 * thing, which means that tz -0100 is passed in as the integer -100,
46 * even though it means "sixty minutes off"
48 const char *show_date(unsigned long time, int tz)
50 struct tm *tm;
51 time_t t;
52 static char timebuf[200];
53 int minutes;
55 minutes = tz < 0 ? -tz : tz;
56 minutes = (minutes / 100)*60 + (minutes % 100);
57 minutes = tz < 0 ? -minutes : minutes;
58 t = time + minutes * 60;
59 tm = gmtime(&t);
60 if (!tm)
61 return NULL;
62 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
63 weekday_names[tm->tm_wday],
64 month_names[tm->tm_mon],
65 tm->tm_mday,
66 tm->tm_hour, tm->tm_min, tm->tm_sec,
67 tm->tm_year + 1900, tz);
68 return timebuf;
72 * Check these. And note how it doesn't do the summer-time conversion.
74 * In my world, it's always summer, and things are probably a bit off
75 * in other ways too.
77 static const struct {
78 const char *name;
79 int offset;
80 int dst;
81 } timezone_names[] = {
82 { "IDLW", -12, 0, }, /* International Date Line West */
83 { "NT", -11, 0, }, /* Nome */
84 { "CAT", -10, 0, }, /* Central Alaska */
85 { "HST", -10, 0, }, /* Hawaii Standard */
86 { "HDT", -10, 1, }, /* Hawaii Daylight */
87 { "YST", -9, 0, }, /* Yukon Standard */
88 { "YDT", -9, 1, }, /* Yukon Daylight */
89 { "PST", -8, 0, }, /* Pacific Standard */
90 { "PDT", -8, 1, }, /* Pacific Daylight */
91 { "MST", -7, 0, }, /* Mountain Standard */
92 { "MDT", -7, 1, }, /* Mountain Daylight */
93 { "CST", -6, 0, }, /* Central Standard */
94 { "CDT", -6, 1, }, /* Central Daylight */
95 { "EST", -5, 0, }, /* Eastern Standard */
96 { "EDT", -5, 1, }, /* Eastern Daylight */
97 { "AST", -3, 0, }, /* Atlantic Standard */
98 { "ADT", -3, 1, }, /* Atlantic Daylight */
99 { "WAT", -1, 0, }, /* West Africa */
101 { "GMT", 0, 0, }, /* Greenwich Mean */
102 { "UTC", 0, 0, }, /* Universal (Coordinated) */
104 { "WET", 0, 0, }, /* Western European */
105 { "BST", 0, 1, }, /* British Summer */
106 { "CET", +1, 0, }, /* Central European */
107 { "MET", +1, 0, }, /* Middle European */
108 { "MEWT", +1, 0, }, /* Middle European Winter */
109 { "MEST", +1, 1, }, /* Middle European Summer */
110 { "CEST", +1, 1, }, /* Central European Summer */
111 { "MESZ", +1, 1, }, /* Middle European Summer */
112 { "FWT", +1, 0, }, /* French Winter */
113 { "FST", +1, 1, }, /* French Summer */
114 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
115 { "EEST", +2, 1, }, /* Eastern European Daylight */
116 { "WAST", +7, 0, }, /* West Australian Standard */
117 { "WADT", +7, 1, }, /* West Australian Daylight */
118 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
119 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
120 { "EAST", +10, 0, }, /* Eastern Australian Standard */
121 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
122 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
123 { "NZT", +11, 0, }, /* New Zealand */
124 { "NZST", +11, 0, }, /* New Zealand Standard */
125 { "NZDT", +11, 1, }, /* New Zealand Daylight */
126 { "IDLE", +12, 0, }, /* International Date Line East */
129 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
131 static int match_string(const char *date, const char *str)
133 int i = 0;
135 for (i = 0; *date; date++, str++, i++) {
136 if (*date == *str)
137 continue;
138 if (toupper(*date) == toupper(*str))
139 continue;
140 if (!isalnum(*date))
141 break;
142 return 0;
144 return i;
147 static int skip_alpha(const char *date)
149 int i = 0;
150 do {
151 i++;
152 } while (isalpha(date[i]));
153 return i;
157 * Parse month, weekday, or timezone name
159 static int match_alpha(const char *date, struct tm *tm, int *offset)
161 int i;
163 for (i = 0; i < 12; i++) {
164 int match = match_string(date, month_names[i]);
165 if (match >= 3) {
166 tm->tm_mon = i;
167 return match;
171 for (i = 0; i < 7; i++) {
172 int match = match_string(date, weekday_names[i]);
173 if (match >= 3) {
174 tm->tm_wday = i;
175 return match;
179 for (i = 0; i < NR_TZ; i++) {
180 int match = match_string(date, timezone_names[i].name);
181 if (match >= 3) {
182 int off = timezone_names[i].offset;
184 /* This is bogus, but we like summer */
185 off += timezone_names[i].dst;
187 /* Only use the tz name offset if we don't have anything better */
188 if (*offset == -1)
189 *offset = 60*off;
191 return match;
195 if (match_string(date, "PM") == 2) {
196 if (tm->tm_hour > 0 && tm->tm_hour < 12)
197 tm->tm_hour += 12;
198 return 2;
201 /* BAD CRAP */
202 return skip_alpha(date);
205 static int is_date(int year, int month, int day, struct tm *tm)
207 if (month > 0 && month < 13 && day > 0 && day < 32) {
208 if (year == -1) {
209 tm->tm_mon = month-1;
210 tm->tm_mday = day;
211 return 1;
213 if (year >= 1970 && year < 2100) {
214 year -= 1900;
215 } else if (year > 70 && year < 100) {
216 /* ok */
217 } else if (year < 38) {
218 year += 100;
219 } else
220 return 0;
222 tm->tm_mon = month-1;
223 tm->tm_mday = day;
224 tm->tm_year = year;
225 return 1;
227 return 0;
230 static int match_multi_number(unsigned long num, char c, char *date, char *end, struct tm *tm)
232 long num2, num3;
234 num2 = strtol(end+1, &end, 10);
235 num3 = -1;
236 if (*end == c && isdigit(end[1]))
237 num3 = strtol(end+1, &end, 10);
239 /* Time? Date? */
240 switch (c) {
241 case ':':
242 if (num3 < 0)
243 num3 = 0;
244 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
245 tm->tm_hour = num;
246 tm->tm_min = num2;
247 tm->tm_sec = num3;
248 break;
250 return 0;
252 case '-':
253 case '/':
254 if (num > 70) {
255 /* yyyy-mm-dd? */
256 if (is_date(num, num2, num3, tm))
257 break;
258 /* yyyy-dd-mm? */
259 if (is_date(num, num3, num2, tm))
260 break;
262 /* mm/dd/yy ? */
263 if (is_date(num3, num2, num, tm))
264 break;
265 /* dd/mm/yy ? */
266 if (is_date(num3, num, num2, tm))
267 break;
268 return 0;
270 return end - date;
274 * We've seen a digit. Time? Year? Date?
276 static int match_digit(char *date, struct tm *tm, int *offset)
278 int n;
279 char *end;
280 unsigned long num;
282 num = strtoul(date, &end, 10);
285 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
287 if (num > 946684800) {
288 time_t time = num;
289 if (gmtime_r(&time, tm))
290 return end - date;
294 * Check for special formats: num[:-/]num[same]num
296 switch (*end) {
297 case ':':
298 case '/':
299 case '-':
300 if (isdigit(end[1])) {
301 int match = match_multi_number(num, *end, date, end, tm);
302 if (match)
303 return match;
308 * None of the special formats? Try to guess what
309 * the number meant. We use the number of digits
310 * to make a more educated guess..
312 n = 0;
313 do {
314 n++;
315 } while (isdigit(date[n]));
317 /* Four-digit year or a timezone? */
318 if (n == 4) {
319 if (num <= 1200 && *offset == -1) {
320 unsigned int minutes = num % 100;
321 unsigned int hours = num / 100;
322 *offset = hours*60 + minutes;
323 } else if (num > 1900 && num < 2100)
324 tm->tm_year = num - 1900;
325 return n;
329 * NOTE! We will give precedence to day-of-month over month or
330 * year numebers in the 1-12 range. So 05 is always "mday 5",
331 * unless we already have a mday..
333 * IOW, 01 Apr 05 parses as "April 1st, 2005".
335 if (num > 0 && num < 32 && tm->tm_mday < 0) {
336 tm->tm_mday = num;
337 return n;
340 /* Two-digit year? */
341 if (n == 2 && tm->tm_year < 0) {
342 if (num < 10 && tm->tm_mday >= 0) {
343 tm->tm_year = num + 100;
344 return n;
346 if (num >= 70) {
347 tm->tm_year = num;
348 return n;
352 if (num > 0 && num < 32) {
353 tm->tm_mday = num;
354 } else if (num > 1900) {
355 tm->tm_year = num - 1900;
356 } else if (num > 70) {
357 tm->tm_year = num;
358 } else if (num > 0 && num < 13) {
359 tm->tm_mon = num-1;
362 return n;
365 static int match_tz(char *date, int *offp)
367 char *end;
368 int offset = strtoul(date+1, &end, 10);
369 int min, hour;
370 int n = end - date - 1;
372 min = offset % 100;
373 hour = offset / 100;
376 * Don't accept any random crap.. At least 3 digits, and
377 * a valid minute. We might want to check that the minutes
378 * are divisible by 30 or something too.
380 if (min < 60 && n > 2) {
381 offset = hour*60+min;
382 if (*date == '-')
383 offset = -offset;
385 *offp = offset;
387 return end - date;
390 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
391 (i.e. English) day/month names, and it doesn't work correctly with %z. */
392 void parse_date(char *date, char *result, int maxlen)
394 struct tm tm;
395 int offset, sign;
396 time_t then;
398 memset(&tm, 0, sizeof(tm));
399 tm.tm_year = -1;
400 tm.tm_mon = -1;
401 tm.tm_mday = -1;
402 tm.tm_isdst = -1;
403 offset = -1;
405 for (;;) {
406 int match = 0;
407 unsigned char c = *date;
409 /* Stop at end of string or newline */
410 if (!c || c == '\n')
411 break;
413 if (isalpha(c))
414 match = match_alpha(date, &tm, &offset);
415 else if (isdigit(c))
416 match = match_digit(date, &tm, &offset);
417 else if ((c == '-' || c == '+') && isdigit(date[1]))
418 match = match_tz(date, &offset);
420 if (!match) {
421 /* BAD CRAP */
422 match = 1;
425 date += match;
428 /* mktime uses local timezone */
429 then = my_mktime(&tm);
430 if (offset == -1)
431 offset = (then - mktime(&tm)) / 60;
433 if (then == -1)
434 return;
436 then -= offset * 60;
438 sign = '+';
439 if (offset < 0) {
440 offset = -offset;
441 sign = '-';
444 snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60);
447 void datestamp(char *buf, int bufsize)
449 time_t now;
450 int offset;
452 time(&now);
454 offset = my_mktime(localtime(&now)) - now;
455 offset /= 60;
457 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);