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