]>
Commit | Line | Data |
---|---|---|
ecee9d9e ET |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
6 | ||
e99d59ff LT |
7 | #include "cache.h" |
8 | ||
ecee9d9e ET |
9 | static time_t my_mktime(struct tm *tm) |
10 | { | |
11 | static const int mdays[] = { | |
12 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | |
13 | }; | |
14 | int year = tm->tm_year - 70; | |
15 | int month = tm->tm_mon; | |
16 | int day = tm->tm_mday; | |
17 | ||
18 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
19 | return -1; | |
20 | if (month < 0 || month > 11) /* array bounds */ | |
21 | return -1; | |
22 | if (month < 2 || (year + 2) % 4) | |
23 | day--; | |
24 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + | |
25 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
26 | } | |
27 | ||
28 | static const char *month_names[] = { | |
89967023 LT |
29 | "January", "February", "March", "April", "May", "June", |
30 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
31 | }; |
32 | ||
33 | static const char *weekday_names[] = { | |
6b7b0427 | 34 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
ecee9d9e ET |
35 | }; |
36 | ||
9a8e35e9 LT |
37 | static time_t gm_time_t(unsigned long time, int tz) |
38 | { | |
39 | int minutes; | |
40 | ||
41 | minutes = tz < 0 ? -tz : tz; | |
42 | minutes = (minutes / 100)*60 + (minutes % 100); | |
43 | minutes = tz < 0 ? -minutes : minutes; | |
44 | return time + minutes * 60; | |
45 | } | |
46 | ||
f80cd783 LT |
47 | /* |
48 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
49 | * thing, which means that tz -0100 is passed in as the integer -100, | |
50 | * even though it means "sixty minutes off" | |
51 | */ | |
2a387043 | 52 | static struct tm *time_to_tm(unsigned long time, int tz) |
f80cd783 | 53 | { |
9a8e35e9 | 54 | time_t t = gm_time_t(time, tz); |
2a387043 JH |
55 | return gmtime(&t); |
56 | } | |
57 | ||
a7b02ccf JH |
58 | /* |
59 | * What value of "tz" was in effect back then at "time" in the | |
60 | * local timezone? | |
61 | */ | |
62 | static int local_tzoffset(unsigned long time) | |
63 | { | |
64 | time_t t, t_local; | |
65 | struct tm tm; | |
66 | int offset, eastwest; | |
67 | ||
68 | t = time; | |
69 | localtime_r(&t, &tm); | |
70 | t_local = my_mktime(&tm); | |
71 | ||
72 | if (t_local < t) { | |
73 | eastwest = -1; | |
74 | offset = t - t_local; | |
75 | } else { | |
76 | eastwest = 1; | |
77 | offset = t_local - t; | |
78 | } | |
79 | offset /= 60; /* in minutes */ | |
80 | offset = (offset % 60) + ((offset / 60) * 100); | |
81 | return offset * eastwest; | |
82 | } | |
83 | ||
f8493ec0 | 84 | const char *show_date(unsigned long time, int tz, enum date_mode mode) |
2a387043 JH |
85 | { |
86 | struct tm *tm; | |
87 | static char timebuf[200]; | |
88 | ||
f8493ec0 | 89 | if (mode == DATE_RELATIVE) { |
9a8e35e9 | 90 | unsigned long diff; |
9a8e35e9 LT |
91 | struct timeval now; |
92 | gettimeofday(&now, NULL); | |
da8f070c | 93 | if (now.tv_sec < time) |
9a8e35e9 | 94 | return "in the future"; |
da8f070c | 95 | diff = now.tv_sec - time; |
9a8e35e9 LT |
96 | if (diff < 90) { |
97 | snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff); | |
98 | return timebuf; | |
99 | } | |
100 | /* Turn it into minutes */ | |
101 | diff = (diff + 30) / 60; | |
102 | if (diff < 90) { | |
103 | snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff); | |
104 | return timebuf; | |
105 | } | |
106 | /* Turn it into hours */ | |
107 | diff = (diff + 30) / 60; | |
108 | if (diff < 36) { | |
109 | snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff); | |
110 | return timebuf; | |
111 | } | |
112 | /* We deal with number of days from here on */ | |
113 | diff = (diff + 12) / 24; | |
114 | if (diff < 14) { | |
115 | snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff); | |
116 | return timebuf; | |
117 | } | |
118 | /* Say weeks for the past 10 weeks or so */ | |
119 | if (diff < 70) { | |
120 | snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7); | |
121 | return timebuf; | |
122 | } | |
123 | /* Say months for the past 12 months or so */ | |
124 | if (diff < 360) { | |
125 | snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30); | |
126 | return timebuf; | |
127 | } | |
128 | /* Else fall back on absolute format.. */ | |
129 | } | |
130 | ||
a7b02ccf JH |
131 | if (mode == DATE_LOCAL) |
132 | tz = local_tzoffset(time); | |
133 | ||
2a387043 | 134 | tm = time_to_tm(time, tz); |
f80cd783 LT |
135 | if (!tm) |
136 | return NULL; | |
f8493ec0 JS |
137 | if (mode == DATE_SHORT) |
138 | sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, | |
139 | tm->tm_mon + 1, tm->tm_mday); | |
140 | else | |
a7b02ccf | 141 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", |
f8493ec0 JS |
142 | weekday_names[tm->tm_wday], |
143 | month_names[tm->tm_mon], | |
144 | tm->tm_mday, | |
145 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
a7b02ccf JH |
146 | tm->tm_year + 1900, |
147 | (mode == DATE_LOCAL) ? 0 : ' ', | |
148 | tz); | |
f80cd783 LT |
149 | return timebuf; |
150 | } | |
151 | ||
2a387043 JH |
152 | const char *show_rfc2822_date(unsigned long time, int tz) |
153 | { | |
154 | struct tm *tm; | |
155 | static char timebuf[200]; | |
156 | ||
157 | tm = time_to_tm(time, tz); | |
158 | if (!tm) | |
159 | return NULL; | |
160 | sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", | |
161 | weekday_names[tm->tm_wday], tm->tm_mday, | |
162 | month_names[tm->tm_mon], tm->tm_year + 1900, | |
163 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); | |
164 | return timebuf; | |
165 | } | |
166 | ||
89967023 LT |
167 | /* |
168 | * Check these. And note how it doesn't do the summer-time conversion. | |
169 | * | |
170 | * In my world, it's always summer, and things are probably a bit off | |
171 | * in other ways too. | |
172 | */ | |
173 | static const struct { | |
174 | const char *name; | |
175 | int offset; | |
5e2a78a4 | 176 | int dst; |
89967023 | 177 | } timezone_names[] = { |
5e2a78a4 LT |
178 | { "IDLW", -12, 0, }, /* International Date Line West */ |
179 | { "NT", -11, 0, }, /* Nome */ | |
180 | { "CAT", -10, 0, }, /* Central Alaska */ | |
181 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
182 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
183 | { "YST", -9, 0, }, /* Yukon Standard */ | |
184 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
185 | { "PST", -8, 0, }, /* Pacific Standard */ | |
186 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
187 | { "MST", -7, 0, }, /* Mountain Standard */ | |
188 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
189 | { "CST", -6, 0, }, /* Central Standard */ | |
190 | { "CDT", -6, 1, }, /* Central Daylight */ | |
191 | { "EST", -5, 0, }, /* Eastern Standard */ | |
192 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
193 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
194 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
195 | { "WAT", -1, 0, }, /* West Africa */ | |
196 | ||
197 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
198 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
199 | ||
200 | { "WET", 0, 0, }, /* Western European */ | |
201 | { "BST", 0, 1, }, /* British Summer */ | |
202 | { "CET", +1, 0, }, /* Central European */ | |
203 | { "MET", +1, 0, }, /* Middle European */ | |
204 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
205 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
206 | { "CEST", +1, 1, }, /* Central European Summer */ | |
207 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
208 | { "FWT", +1, 0, }, /* French Winter */ | |
209 | { "FST", +1, 1, }, /* French Summer */ | |
210 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 211 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
212 | { "WAST", +7, 0, }, /* West Australian Standard */ |
213 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
214 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
215 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
216 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
217 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
218 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
219 | { "NZT", +11, 0, }, /* New Zealand */ | |
220 | { "NZST", +11, 0, }, /* New Zealand Standard */ | |
221 | { "NZDT", +11, 1, }, /* New Zealand Daylight */ | |
222 | { "IDLE", +12, 0, }, /* International Date Line East */ | |
89967023 | 223 | }; |
ecee9d9e | 224 | |
89967023 | 225 | static int match_string(const char *date, const char *str) |
ecee9d9e | 226 | { |
89967023 LT |
227 | int i = 0; |
228 | ||
229 | for (i = 0; *date; date++, str++, i++) { | |
230 | if (*date == *str) | |
231 | continue; | |
232 | if (toupper(*date) == toupper(*str)) | |
233 | continue; | |
234 | if (!isalnum(*date)) | |
235 | break; | |
236 | return 0; | |
237 | } | |
238 | return i; | |
ecee9d9e ET |
239 | } |
240 | ||
68849b54 LT |
241 | static int skip_alpha(const char *date) |
242 | { | |
243 | int i = 0; | |
244 | do { | |
245 | i++; | |
246 | } while (isalpha(date[i])); | |
247 | return i; | |
248 | } | |
249 | ||
89967023 LT |
250 | /* |
251 | * Parse month, weekday, or timezone name | |
252 | */ | |
253 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 254 | { |
89967023 | 255 | int i; |
ecee9d9e | 256 | |
89967023 LT |
257 | for (i = 0; i < 12; i++) { |
258 | int match = match_string(date, month_names[i]); | |
259 | if (match >= 3) { | |
260 | tm->tm_mon = i; | |
261 | return match; | |
ecee9d9e | 262 | } |
89967023 | 263 | } |
ecee9d9e | 264 | |
89967023 LT |
265 | for (i = 0; i < 7; i++) { |
266 | int match = match_string(date, weekday_names[i]); | |
267 | if (match >= 3) { | |
268 | tm->tm_wday = i; | |
269 | return match; | |
270 | } | |
271 | } | |
ecee9d9e | 272 | |
b4f2a6ac | 273 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
89967023 LT |
274 | int match = match_string(date, timezone_names[i].name); |
275 | if (match >= 3) { | |
5e2a78a4 LT |
276 | int off = timezone_names[i].offset; |
277 | ||
278 | /* This is bogus, but we like summer */ | |
279 | off += timezone_names[i].dst; | |
280 | ||
92e2311b LT |
281 | /* Only use the tz name offset if we don't have anything better */ |
282 | if (*offset == -1) | |
283 | *offset = 60*off; | |
284 | ||
89967023 | 285 | return match; |
ecee9d9e ET |
286 | } |
287 | } | |
ecee9d9e | 288 | |
68849b54 | 289 | if (match_string(date, "PM") == 2) { |
18b633ca LT |
290 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
291 | return 2; | |
292 | } | |
293 | ||
294 | if (match_string(date, "AM") == 2) { | |
295 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
68849b54 LT |
296 | return 2; |
297 | } | |
298 | ||
89967023 | 299 | /* BAD CRAP */ |
68849b54 | 300 | return skip_alpha(date); |
89967023 | 301 | } |
ecee9d9e | 302 | |
38035cf4 | 303 | static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
89967023 | 304 | { |
198b0fb6 | 305 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
38035cf4 JH |
306 | struct tm check = *tm; |
307 | struct tm *r = (now_tm ? &check : tm); | |
308 | time_t specified; | |
309 | ||
310 | r->tm_mon = month - 1; | |
311 | r->tm_mday = day; | |
198b0fb6 | 312 | if (year == -1) { |
38035cf4 JH |
313 | if (!now_tm) |
314 | return 1; | |
315 | r->tm_year = now_tm->tm_year; | |
89967023 | 316 | } |
38035cf4 JH |
317 | else if (year >= 1970 && year < 2100) |
318 | r->tm_year = year - 1900; | |
319 | else if (year > 70 && year < 100) | |
320 | r->tm_year = year; | |
321 | else if (year < 38) | |
322 | r->tm_year = year + 100; | |
323 | else | |
198b0fb6 | 324 | return 0; |
38035cf4 JH |
325 | if (!now_tm) |
326 | return 1; | |
327 | ||
328 | specified = my_mktime(r); | |
198b0fb6 | 329 | |
38035cf4 JH |
330 | /* Be it commit time or author time, it does not make |
331 | * sense to specify timestamp way into the future. Make | |
332 | * sure it is not later than ten days from now... | |
333 | */ | |
334 | if (now + 10*24*3600 < specified) | |
335 | return 0; | |
336 | tm->tm_mon = r->tm_mon; | |
337 | tm->tm_mday = r->tm_mday; | |
338 | if (year != -1) | |
339 | tm->tm_year = r->tm_year; | |
198b0fb6 | 340 | return 1; |
89967023 | 341 | } |
198b0fb6 LT |
342 | return 0; |
343 | } | |
ecee9d9e | 344 | |
26a2d8ae | 345 | static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) |
198b0fb6 | 346 | { |
38035cf4 JH |
347 | time_t now; |
348 | struct tm now_tm; | |
349 | struct tm *refuse_future; | |
198b0fb6 LT |
350 | long num2, num3; |
351 | ||
352 | num2 = strtol(end+1, &end, 10); | |
353 | num3 = -1; | |
354 | if (*end == c && isdigit(end[1])) | |
355 | num3 = strtol(end+1, &end, 10); | |
356 | ||
357 | /* Time? Date? */ | |
89967023 | 358 | switch (c) { |
198b0fb6 LT |
359 | case ':': |
360 | if (num3 < 0) | |
361 | num3 = 0; | |
362 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
363 | tm->tm_hour = num; | |
364 | tm->tm_min = num2; | |
365 | tm->tm_sec = num3; | |
89967023 LT |
366 | break; |
367 | } | |
198b0fb6 | 368 | return 0; |
89967023 LT |
369 | |
370 | case '-': | |
371 | case '/': | |
38035cf4 JH |
372 | case '.': |
373 | now = time(NULL); | |
374 | refuse_future = NULL; | |
375 | if (gmtime_r(&now, &now_tm)) | |
376 | refuse_future = &now_tm; | |
377 | ||
198b0fb6 LT |
378 | if (num > 70) { |
379 | /* yyyy-mm-dd? */ | |
38035cf4 | 380 | if (is_date(num, num2, num3, refuse_future, now, tm)) |
198b0fb6 LT |
381 | break; |
382 | /* yyyy-dd-mm? */ | |
38035cf4 | 383 | if (is_date(num, num3, num2, refuse_future, now, tm)) |
89967023 | 384 | break; |
198b0fb6 | 385 | } |
38035cf4 JH |
386 | /* Our eastern European friends say dd.mm.yy[yy] |
387 | * is the norm there, so giving precedence to | |
388 | * mm/dd/yy[yy] form only when separator is not '.' | |
389 | */ | |
390 | if (c != '.' && | |
391 | is_date(num3, num, num2, refuse_future, now, tm)) | |
392 | break; | |
393 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | |
394 | if (is_date(num3, num2, num, refuse_future, now, tm)) | |
89967023 | 395 | break; |
38035cf4 JH |
396 | /* Funny European mm.dd.yy */ |
397 | if (c == '.' && | |
398 | is_date(num3, num, num2, refuse_future, now, tm)) | |
198b0fb6 LT |
399 | break; |
400 | return 0; | |
401 | } | |
402 | return end - date; | |
403 | } | |
404 | ||
405 | /* | |
406 | * We've seen a digit. Time? Year? Date? | |
407 | */ | |
26a2d8ae | 408 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
198b0fb6 LT |
409 | { |
410 | int n; | |
411 | char *end; | |
412 | unsigned long num; | |
413 | ||
414 | num = strtoul(date, &end, 10); | |
415 | ||
416 | /* | |
417 | * Seconds since 1970? We trigger on that for anything after Jan 1, 2000 | |
418 | */ | |
419 | if (num > 946684800) { | |
420 | time_t time = num; | |
9cb480f2 JH |
421 | if (gmtime_r(&time, tm)) { |
422 | *tm_gmt = 1; | |
198b0fb6 | 423 | return end - date; |
9cb480f2 | 424 | } |
198b0fb6 LT |
425 | } |
426 | ||
427 | /* | |
38035cf4 | 428 | * Check for special formats: num[-.:/]num[same]num |
198b0fb6 LT |
429 | */ |
430 | switch (*end) { | |
431 | case ':': | |
38035cf4 | 432 | case '.': |
198b0fb6 LT |
433 | case '/': |
434 | case '-': | |
435 | if (isdigit(end[1])) { | |
436 | int match = match_multi_number(num, *end, date, end, tm); | |
437 | if (match) | |
438 | return match; | |
89967023 LT |
439 | } |
440 | } | |
198b0fb6 LT |
441 | |
442 | /* | |
443 | * None of the special formats? Try to guess what | |
444 | * the number meant. We use the number of digits | |
445 | * to make a more educated guess.. | |
446 | */ | |
447 | n = 0; | |
448 | do { | |
449 | n++; | |
450 | } while (isdigit(date[n])); | |
451 | ||
452 | /* Four-digit year or a timezone? */ | |
453 | if (n == 4) { | |
7122f82f | 454 | if (num <= 1400 && *offset == -1) { |
198b0fb6 LT |
455 | unsigned int minutes = num % 100; |
456 | unsigned int hours = num / 100; | |
457 | *offset = hours*60 + minutes; | |
458 | } else if (num > 1900 && num < 2100) | |
459 | tm->tm_year = num - 1900; | |
460 | return n; | |
461 | } | |
462 | ||
463 | /* | |
464 | * NOTE! We will give precedence to day-of-month over month or | |
82f9d58a | 465 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
198b0fb6 LT |
466 | * unless we already have a mday.. |
467 | * | |
468 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
469 | */ | |
470 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
471 | tm->tm_mday = num; | |
472 | return n; | |
473 | } | |
474 | ||
475 | /* Two-digit year? */ | |
476 | if (n == 2 && tm->tm_year < 0) { | |
477 | if (num < 10 && tm->tm_mday >= 0) { | |
478 | tm->tm_year = num + 100; | |
479 | return n; | |
480 | } | |
481 | if (num >= 70) { | |
482 | tm->tm_year = num; | |
483 | return n; | |
484 | } | |
485 | } | |
486 | ||
487 | if (num > 0 && num < 32) { | |
488 | tm->tm_mday = num; | |
489 | } else if (num > 1900) { | |
490 | tm->tm_year = num - 1900; | |
491 | } else if (num > 70) { | |
492 | tm->tm_year = num; | |
493 | } else if (num > 0 && num < 13) { | |
494 | tm->tm_mon = num-1; | |
495 | } | |
ecee9d9e | 496 | |
198b0fb6 | 497 | return n; |
89967023 | 498 | } |
ecee9d9e | 499 | |
26a2d8ae | 500 | static int match_tz(const char *date, int *offp) |
89967023 LT |
501 | { |
502 | char *end; | |
503 | int offset = strtoul(date+1, &end, 10); | |
504 | int min, hour; | |
198b0fb6 | 505 | int n = end - date - 1; |
ecee9d9e | 506 | |
89967023 LT |
507 | min = offset % 100; |
508 | hour = offset / 100; | |
ecee9d9e | 509 | |
198b0fb6 LT |
510 | /* |
511 | * Don't accept any random crap.. At least 3 digits, and | |
512 | * a valid minute. We might want to check that the minutes | |
513 | * are divisible by 30 or something too. | |
514 | */ | |
68849b54 LT |
515 | if (min < 60 && n > 2) { |
516 | offset = hour*60+min; | |
517 | if (*date == '-') | |
518 | offset = -offset; | |
ecee9d9e | 519 | |
68849b54 LT |
520 | *offp = offset; |
521 | } | |
89967023 LT |
522 | return end - date; |
523 | } | |
ecee9d9e | 524 | |
01c6ad29 LT |
525 | static int date_string(unsigned long date, int offset, char *buf, int len) |
526 | { | |
527 | int sign = '+'; | |
528 | ||
529 | if (offset < 0) { | |
530 | offset = -offset; | |
531 | sign = '-'; | |
532 | } | |
533 | return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); | |
534 | } | |
535 | ||
89967023 LT |
536 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
537 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
2a39064c | 538 | int parse_date(const char *date, char *result, int maxlen) |
89967023 LT |
539 | { |
540 | struct tm tm; | |
01c6ad29 | 541 | int offset, tm_gmt; |
89967023 | 542 | time_t then; |
ecee9d9e | 543 | |
89967023 LT |
544 | memset(&tm, 0, sizeof(tm)); |
545 | tm.tm_year = -1; | |
546 | tm.tm_mon = -1; | |
547 | tm.tm_mday = -1; | |
eaa85129 LT |
548 | tm.tm_isdst = -1; |
549 | offset = -1; | |
9cb480f2 | 550 | tm_gmt = 0; |
89967023 LT |
551 | |
552 | for (;;) { | |
553 | int match = 0; | |
554 | unsigned char c = *date; | |
555 | ||
556 | /* Stop at end of string or newline */ | |
557 | if (!c || c == '\n') | |
558 | break; | |
559 | ||
560 | if (isalpha(c)) | |
561 | match = match_alpha(date, &tm, &offset); | |
562 | else if (isdigit(c)) | |
9cb480f2 | 563 | match = match_digit(date, &tm, &offset, &tm_gmt); |
89967023 LT |
564 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
565 | match = match_tz(date, &offset); | |
566 | ||
567 | if (!match) { | |
568 | /* BAD CRAP */ | |
569 | match = 1; | |
570 | } | |
571 | ||
572 | date += match; | |
573 | } | |
ecee9d9e | 574 | |
eaa85129 LT |
575 | /* mktime uses local timezone */ |
576 | then = my_mktime(&tm); | |
577 | if (offset == -1) | |
578 | offset = (then - mktime(&tm)) / 60; | |
579 | ||
ecee9d9e | 580 | if (then == -1) |
2a39064c | 581 | return -1; |
ecee9d9e | 582 | |
9cb480f2 JH |
583 | if (!tm_gmt) |
584 | then -= offset * 60; | |
01c6ad29 | 585 | return date_string(then, offset, result, maxlen); |
ecee9d9e ET |
586 | } |
587 | ||
588 | void datestamp(char *buf, int bufsize) | |
589 | { | |
590 | time_t now; | |
591 | int offset; | |
592 | ||
593 | time(&now); | |
594 | ||
595 | offset = my_mktime(localtime(&now)) - now; | |
596 | offset /= 60; | |
597 | ||
01c6ad29 | 598 | date_string(now, offset, buf, bufsize); |
ecee9d9e | 599 | } |
3c07b1d1 LT |
600 | |
601 | static void update_tm(struct tm *tm, unsigned long sec) | |
602 | { | |
603 | time_t n = mktime(tm) - sec; | |
604 | localtime_r(&n, tm); | |
605 | } | |
606 | ||
a8aca418 LT |
607 | static void date_yesterday(struct tm *tm, int *num) |
608 | { | |
609 | update_tm(tm, 24*60*60); | |
610 | } | |
611 | ||
612 | static void date_time(struct tm *tm, int hour) | |
613 | { | |
614 | if (tm->tm_hour < hour) | |
615 | date_yesterday(tm, NULL); | |
616 | tm->tm_hour = hour; | |
617 | tm->tm_min = 0; | |
618 | tm->tm_sec = 0; | |
619 | } | |
620 | ||
621 | static void date_midnight(struct tm *tm, int *num) | |
622 | { | |
623 | date_time(tm, 0); | |
624 | } | |
625 | ||
626 | static void date_noon(struct tm *tm, int *num) | |
627 | { | |
628 | date_time(tm, 12); | |
629 | } | |
630 | ||
631 | static void date_tea(struct tm *tm, int *num) | |
632 | { | |
633 | date_time(tm, 17); | |
634 | } | |
635 | ||
393d340e LT |
636 | static void date_pm(struct tm *tm, int *num) |
637 | { | |
18b633ca | 638 | int hour, n = *num; |
393d340e LT |
639 | *num = 0; |
640 | ||
18b633ca LT |
641 | hour = tm->tm_hour; |
642 | if (n) { | |
643 | hour = n; | |
393d340e LT |
644 | tm->tm_min = 0; |
645 | tm->tm_sec = 0; | |
646 | } | |
18b633ca | 647 | tm->tm_hour = (hour % 12) + 12; |
393d340e LT |
648 | } |
649 | ||
650 | static void date_am(struct tm *tm, int *num) | |
651 | { | |
18b633ca | 652 | int hour, n = *num; |
393d340e LT |
653 | *num = 0; |
654 | ||
18b633ca LT |
655 | hour = tm->tm_hour; |
656 | if (n) { | |
657 | hour = n; | |
393d340e LT |
658 | tm->tm_min = 0; |
659 | tm->tm_sec = 0; | |
660 | } | |
18b633ca | 661 | tm->tm_hour = (hour % 12); |
393d340e LT |
662 | } |
663 | ||
a8aca418 LT |
664 | static const struct special { |
665 | const char *name; | |
666 | void (*fn)(struct tm *, int *); | |
667 | } special[] = { | |
668 | { "yesterday", date_yesterday }, | |
669 | { "noon", date_noon }, | |
670 | { "midnight", date_midnight }, | |
671 | { "tea", date_tea }, | |
393d340e LT |
672 | { "PM", date_pm }, |
673 | { "AM", date_am }, | |
a8aca418 LT |
674 | { NULL } |
675 | }; | |
676 | ||
3c07b1d1 LT |
677 | static const char *number_name[] = { |
678 | "zero", "one", "two", "three", "four", | |
679 | "five", "six", "seven", "eight", "nine", "ten", | |
680 | }; | |
681 | ||
a8aca418 | 682 | static const struct typelen { |
3c07b1d1 LT |
683 | const char *type; |
684 | int length; | |
685 | } typelen[] = { | |
686 | { "seconds", 1 }, | |
687 | { "minutes", 60 }, | |
688 | { "hours", 60*60 }, | |
689 | { "days", 24*60*60 }, | |
690 | { "weeks", 7*24*60*60 }, | |
691 | { NULL } | |
692 | }; | |
693 | ||
694 | static const char *approxidate_alpha(const char *date, struct tm *tm, int *num) | |
695 | { | |
a8aca418 LT |
696 | const struct typelen *tl; |
697 | const struct special *s; | |
3c07b1d1 | 698 | const char *end = date; |
5df7dbba | 699 | int i; |
3c07b1d1 | 700 | |
5df7dbba PH |
701 | while (isalpha(*++end)); |
702 | ; | |
3c07b1d1 LT |
703 | |
704 | for (i = 0; i < 12; i++) { | |
705 | int match = match_string(date, month_names[i]); | |
706 | if (match >= 3) { | |
707 | tm->tm_mon = i; | |
708 | return end; | |
709 | } | |
710 | } | |
711 | ||
a8aca418 LT |
712 | for (s = special; s->name; s++) { |
713 | int len = strlen(s->name); | |
714 | if (match_string(date, s->name) == len) { | |
715 | s->fn(tm, num); | |
716 | return end; | |
717 | } | |
3c07b1d1 LT |
718 | } |
719 | ||
720 | if (!*num) { | |
721 | for (i = 1; i < 11; i++) { | |
722 | int len = strlen(number_name[i]); | |
723 | if (match_string(date, number_name[i]) == len) { | |
724 | *num = i; | |
725 | return end; | |
726 | } | |
727 | } | |
728 | if (match_string(date, "last") == 4) | |
729 | *num = 1; | |
730 | return end; | |
731 | } | |
732 | ||
733 | tl = typelen; | |
734 | while (tl->type) { | |
735 | int len = strlen(tl->type); | |
736 | if (match_string(date, tl->type) >= len-1) { | |
737 | update_tm(tm, tl->length * *num); | |
738 | *num = 0; | |
739 | return end; | |
740 | } | |
741 | tl++; | |
742 | } | |
743 | ||
6b7b0427 LT |
744 | for (i = 0; i < 7; i++) { |
745 | int match = match_string(date, weekday_names[i]); | |
746 | if (match >= 3) { | |
747 | int diff, n = *num -1; | |
748 | *num = 0; | |
749 | ||
750 | diff = tm->tm_wday - i; | |
751 | if (diff <= 0) | |
752 | n++; | |
753 | diff += 7*n; | |
754 | ||
755 | update_tm(tm, diff * 24 * 60 * 60); | |
756 | return end; | |
757 | } | |
758 | } | |
759 | ||
3c07b1d1 LT |
760 | if (match_string(date, "months") >= 5) { |
761 | int n = tm->tm_mon - *num; | |
762 | *num = 0; | |
763 | while (n < 0) { | |
764 | n += 12; | |
765 | tm->tm_year--; | |
766 | } | |
767 | tm->tm_mon = n; | |
768 | return end; | |
769 | } | |
770 | ||
771 | if (match_string(date, "years") >= 4) { | |
772 | tm->tm_year -= *num; | |
773 | *num = 0; | |
774 | return end; | |
775 | } | |
776 | ||
777 | return end; | |
778 | } | |
779 | ||
e92a54d9 LT |
780 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num) |
781 | { | |
782 | char *end; | |
783 | unsigned long number = strtoul(date, &end, 10); | |
784 | ||
393d340e LT |
785 | switch (*end) { |
786 | case ':': | |
787 | case '.': | |
788 | case '/': | |
789 | case '-': | |
790 | if (isdigit(end[1])) { | |
791 | int match = match_multi_number(number, *end, date, end, tm); | |
792 | if (match) | |
793 | return date + match; | |
794 | } | |
795 | } | |
796 | ||
e92a54d9 LT |
797 | *num = number; |
798 | return end; | |
799 | } | |
800 | ||
3c07b1d1 LT |
801 | unsigned long approxidate(const char *date) |
802 | { | |
803 | int number = 0; | |
804 | struct tm tm, now; | |
805 | struct timeval tv; | |
806 | char buffer[50]; | |
807 | ||
808 | if (parse_date(date, buffer, sizeof(buffer)) > 0) | |
809 | return strtoul(buffer, NULL, 10); | |
810 | ||
811 | gettimeofday(&tv, NULL); | |
812 | localtime_r(&tv.tv_sec, &tm); | |
813 | now = tm; | |
814 | for (;;) { | |
815 | unsigned char c = *date; | |
816 | if (!c) | |
817 | break; | |
818 | date++; | |
819 | if (isdigit(c)) { | |
e92a54d9 | 820 | date = approxidate_digit(date-1, &tm, &number); |
3c07b1d1 LT |
821 | continue; |
822 | } | |
823 | if (isalpha(c)) | |
824 | date = approxidate_alpha(date-1, &tm, &number); | |
825 | } | |
826 | if (number > 0 && number < 32) | |
827 | tm.tm_mday = number; | |
b73cebf4 | 828 | if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year) |
3c07b1d1 LT |
829 | tm.tm_year--; |
830 | return mktime(&tm); | |
831 | } |