[PATCH] Add -B flag to diff-* brothers.
[git.git] / date.c
blobff922fe4fc896f4d1132ae84ab5fa74c7f582484
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include <ctype.h>
8 #include <time.h>
10 #include "cache.h"
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 */
22 return -1;
23 if (month < 0 || month > 11) /* array bounds */
24 return -1;
25 if (month < 2 || (year + 2) % 4)
26 day--;
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 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
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)
47 struct tm *tm;
48 time_t t;
49 static char timebuf[200];
50 int minutes;
52 minutes = tz < 0 ? -tz : tz;
53 minutes = (minutes / 100)*60 + (minutes % 100);
54 minutes = tz < 0 ? -minutes : minutes;
55 t = time + minutes * 60;
56 tm = gmtime(&t);
57 if (!tm)
58 return NULL;
59 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
60 weekday_names[tm->tm_wday],
61 month_names[tm->tm_mon],
62 tm->tm_mday,
63 tm->tm_hour, tm->tm_min, tm->tm_sec,
64 tm->tm_year + 1900, tz);
65 return timebuf;
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
72 * in other ways too.
74 static const struct {
75 const char *name;
76 int offset;
77 int dst;
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)
130 int i = 0;
132 for (i = 0; *date; date++, str++, i++) {
133 if (*date == *str)
134 continue;
135 if (toupper(*date) == toupper(*str))
136 continue;
137 if (!isalnum(*date))
138 break;
139 return 0;
141 return i;
144 static int skip_alpha(const char *date)
146 int i = 0;
147 do {
148 i++;
149 } while (isalpha(date[i]));
150 return i;
154 * Parse month, weekday, or timezone name
156 static int match_alpha(const char *date, struct tm *tm, int *offset)
158 int i;
160 for (i = 0; i < 12; i++) {
161 int match = match_string(date, month_names[i]);
162 if (match >= 3) {
163 tm->tm_mon = i;
164 return match;
168 for (i = 0; i < 7; i++) {
169 int match = match_string(date, weekday_names[i]);
170 if (match >= 3) {
171 tm->tm_wday = i;
172 return match;
176 for (i = 0; i < NR_TZ; i++) {
177 int match = match_string(date, timezone_names[i].name);
178 if (match >= 3) {
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 */
185 if (*offset == -1)
186 *offset = 60*off;
188 return match;
192 if (match_string(date, "PM") == 2) {
193 if (tm->tm_hour > 0 && tm->tm_hour < 12)
194 tm->tm_hour += 12;
195 return 2;
198 /* BAD CRAP */
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) {
205 if (year == -1) {
206 tm->tm_mon = month-1;
207 tm->tm_mday = day;
208 return 1;
210 if (year >= 1970 && year < 2100) {
211 year -= 1900;
212 } else if (year > 70 && year < 100) {
213 /* ok */
214 } else if (year < 38) {
215 year += 100;
216 } else
217 return 0;
219 tm->tm_mon = month-1;
220 tm->tm_mday = day;
221 tm->tm_year = year;
222 return 1;
224 return 0;
227 static int match_multi_number(unsigned long num, char c, char *date, char *end, struct tm *tm)
229 long num2, num3;
231 num2 = strtol(end+1, &end, 10);
232 num3 = -1;
233 if (*end == c && isdigit(end[1]))
234 num3 = strtol(end+1, &end, 10);
236 /* Time? Date? */
237 switch (c) {
238 case ':':
239 if (num3 < 0)
240 num3 = 0;
241 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
242 tm->tm_hour = num;
243 tm->tm_min = num2;
244 tm->tm_sec = num3;
245 break;
247 return 0;
249 case '-':
250 case '/':
251 if (num > 70) {
252 /* yyyy-mm-dd? */
253 if (is_date(num, num2, num3, tm))
254 break;
255 /* yyyy-dd-mm? */
256 if (is_date(num, num3, num2, tm))
257 break;
259 /* mm/dd/yy ? */
260 if (is_date(num3, num2, num, tm))
261 break;
262 /* dd/mm/yy ? */
263 if (is_date(num3, num, num2, tm))
264 break;
265 return 0;
267 return end - date;
271 * We've seen a digit. Time? Year? Date?
273 static int match_digit(char *date, struct tm *tm, int *offset)
275 int n;
276 char *end;
277 unsigned long num;
279 num = strtoul(date, &end, 10);
282 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
284 if (num > 946684800) {
285 time_t time = num;
286 if (gmtime_r(&time, tm))
287 return end - date;
291 * Check for special formats: num[:-/]num[same]num
293 switch (*end) {
294 case ':':
295 case '/':
296 case '-':
297 if (isdigit(end[1])) {
298 int match = match_multi_number(num, *end, date, end, tm);
299 if (match)
300 return match;
305 * None of the special formats? Try to guess what
306 * the number meant. We use the number of digits
307 * to make a more educated guess..
309 n = 0;
310 do {
311 n++;
312 } while (isdigit(date[n]));
314 /* Four-digit year or a timezone? */
315 if (n == 4) {
316 if (num <= 1200 && *offset == -1) {
317 unsigned int minutes = num % 100;
318 unsigned int hours = num / 100;
319 *offset = hours*60 + minutes;
320 } else if (num > 1900 && num < 2100)
321 tm->tm_year = num - 1900;
322 return n;
326 * NOTE! We will give precedence to day-of-month over month or
327 * year numebers in the 1-12 range. So 05 is always "mday 5",
328 * unless we already have a mday..
330 * IOW, 01 Apr 05 parses as "April 1st, 2005".
332 if (num > 0 && num < 32 && tm->tm_mday < 0) {
333 tm->tm_mday = num;
334 return n;
337 /* Two-digit year? */
338 if (n == 2 && tm->tm_year < 0) {
339 if (num < 10 && tm->tm_mday >= 0) {
340 tm->tm_year = num + 100;
341 return n;
343 if (num >= 70) {
344 tm->tm_year = num;
345 return n;
349 if (num > 0 && num < 32) {
350 tm->tm_mday = num;
351 } else if (num > 1900) {
352 tm->tm_year = num - 1900;
353 } else if (num > 70) {
354 tm->tm_year = num;
355 } else if (num > 0 && num < 13) {
356 tm->tm_mon = num-1;
359 return n;
362 static int match_tz(char *date, int *offp)
364 char *end;
365 int offset = strtoul(date+1, &end, 10);
366 int min, hour;
367 int n = end - date - 1;
369 min = offset % 100;
370 hour = offset / 100;
373 * Don't accept any random crap.. At least 3 digits, and
374 * a valid minute. We might want to check that the minutes
375 * are divisible by 30 or something too.
377 if (min < 60 && n > 2) {
378 offset = hour*60+min;
379 if (*date == '-')
380 offset = -offset;
382 *offp = offset;
384 return end - date;
387 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
388 (i.e. English) day/month names, and it doesn't work correctly with %z. */
389 void parse_date(char *date, char *result, int maxlen)
391 struct tm tm;
392 int offset, sign;
393 time_t then;
395 memset(&tm, 0, sizeof(tm));
396 tm.tm_year = -1;
397 tm.tm_mon = -1;
398 tm.tm_mday = -1;
399 tm.tm_isdst = -1;
400 offset = -1;
402 for (;;) {
403 int match = 0;
404 unsigned char c = *date;
406 /* Stop at end of string or newline */
407 if (!c || c == '\n')
408 break;
410 if (isalpha(c))
411 match = match_alpha(date, &tm, &offset);
412 else if (isdigit(c))
413 match = match_digit(date, &tm, &offset);
414 else if ((c == '-' || c == '+') && isdigit(date[1]))
415 match = match_tz(date, &offset);
417 if (!match) {
418 /* BAD CRAP */
419 match = 1;
422 date += match;
425 /* mktime uses local timezone */
426 then = my_mktime(&tm);
427 if (offset == -1)
428 offset = (then - mktime(&tm)) / 60;
430 if (then == -1)
431 return;
433 then -= offset * 60;
435 sign = '+';
436 if (offset < 0) {
437 offset = -offset;
438 sign = '-';
441 snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60);
444 void datestamp(char *buf, int bufsize)
446 time_t now;
447 int offset;
449 time(&now);
451 offset = my_mktime(localtime(&now)) - now;
452 offset /= 60;
454 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);