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