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