blob: e9ee4aa7484c531fab8e6d24fbdabdc640378e28 [file] [log] [blame]
Edgar Toernigecee9d92005-04-30 09:46:49 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
Linus Torvaldse99d59f2005-05-20 11:46:10 -07007#include "cache.h"
8
Johannes Sixtbb5799d2008-06-23 08:31:41 +02009/*
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)
Edgar Toernigecee9d92005-04-30 09:46:49 -070013{
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--;
Linus Torvalds36e49862009-08-22 18:11:44 -070027 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
28 return -1;
Edgar Toernigecee9d92005-04-30 09:46:49 -070029 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
30 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
31}
32
33static const char *month_names[] = {
Linus Torvalds89967022005-04-30 13:19:56 -070034 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
Edgar Toernigecee9d92005-04-30 09:46:49 -070036};
37
38static const char *weekday_names[] = {
Linus Torvalds6b7b0422005-11-17 12:36:30 -080039 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
Edgar Toernigecee9d92005-04-30 09:46:49 -070040};
41
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070042static time_t gm_time_t(unsigned long time, int tz)
43{
44 int minutes;
45
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
49 return time + minutes * 60;
50}
51
Linus Torvalds89967022005-04-30 13:19:56 -070052/*
Linus Torvaldsf80cd782005-05-06 15:28:59 -070053 * The "tz" thing is passed in as this strange "decimal parse of tz"
54 * thing, which means that tz -0100 is passed in as the integer -100,
55 * even though it means "sixty minutes off"
56 */
Junio C Hamano2a387042006-05-01 01:44:33 -070057static struct tm *time_to_tm(unsigned long time, int tz)
Linus Torvaldsf80cd782005-05-06 15:28:59 -070058{
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070059 time_t t = gm_time_t(time, tz);
Junio C Hamano2a387042006-05-01 01:44:33 -070060 return gmtime(&t);
61}
62
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070063/*
64 * What value of "tz" was in effect back then at "time" in the
65 * local timezone?
66 */
67static int local_tzoffset(unsigned long time)
68{
69 time_t t, t_local;
70 struct tm tm;
71 int offset, eastwest;
72
73 t = time;
74 localtime_r(&t, &tm);
Johannes Sixtbb5799d2008-06-23 08:31:41 +020075 t_local = tm_to_time_t(&tm);
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070076
77 if (t_local < t) {
78 eastwest = -1;
79 offset = t - t_local;
80 } else {
81 eastwest = 1;
82 offset = t_local - t;
83 }
84 offset /= 60; /* in minutes */
85 offset = (offset % 60) + ((offset / 60) * 100);
86 return offset * eastwest;
87}
88
Alex Riesen33012fc2009-08-30 22:26:05 -040089const char *show_date_relative(unsigned long time, int tz,
90 const struct timeval *now,
91 char *timebuf,
92 size_t timebuf_size)
93{
94 unsigned long diff;
95 if (now->tv_sec < time)
96 return "in the future";
97 diff = now->tv_sec - time;
98 if (diff < 90) {
99 snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
100 return timebuf;
101 }
102 /* Turn it into minutes */
103 diff = (diff + 30) / 60;
104 if (diff < 90) {
105 snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
106 return timebuf;
107 }
108 /* Turn it into hours */
109 diff = (diff + 30) / 60;
110 if (diff < 36) {
111 snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
112 return timebuf;
113 }
114 /* We deal with number of days from here on */
115 diff = (diff + 12) / 24;
116 if (diff < 14) {
117 snprintf(timebuf, timebuf_size, "%lu days ago", diff);
118 return timebuf;
119 }
120 /* Say weeks for the past 10 weeks or so */
121 if (diff < 70) {
122 snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
123 return timebuf;
124 }
125 /* Say months for the past 12 months or so */
126 if (diff < 360) {
127 snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
128 return timebuf;
129 }
130 /* Give years and months for 5 years or so */
131 if (diff < 1825) {
132 unsigned long years = diff / 365;
133 unsigned long months = (diff % 365 + 15) / 30;
134 int n;
135 n = snprintf(timebuf, timebuf_size, "%lu year%s",
136 years, (years > 1 ? "s" : ""));
137 if (months)
138 snprintf(timebuf + n, timebuf_size - n,
139 ", %lu month%s ago",
140 months, (months > 1 ? "s" : ""));
141 else
142 snprintf(timebuf + n, timebuf_size - n, " ago");
143 return timebuf;
144 }
145 /* Otherwise, just years. Centuries is probably overkill. */
146 snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
147 return timebuf;
148}
149
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100150const char *show_date(unsigned long time, int tz, enum date_mode mode)
Junio C Hamano2a387042006-05-01 01:44:33 -0700151{
152 struct tm *tm;
153 static char timebuf[200];
154
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800155 if (mode == DATE_RAW) {
156 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
157 return timebuf;
158 }
159
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100160 if (mode == DATE_RELATIVE) {
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700161 struct timeval now;
162 gettimeofday(&now, NULL);
Alex Riesen33012fc2009-08-30 22:26:05 -0400163 return show_date_relative(time, tz, &now,
164 timebuf, sizeof(timebuf));
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700165 }
166
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -0700167 if (mode == DATE_LOCAL)
168 tz = local_tzoffset(time);
169
Junio C Hamano2a387042006-05-01 01:44:33 -0700170 tm = time_to_tm(time, tz);
Linus Torvaldsf80cd782005-05-06 15:28:59 -0700171 if (!tm)
172 return NULL;
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100173 if (mode == DATE_SHORT)
174 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
175 tm->tm_mon + 1, tm->tm_mday);
Robin Rosenbergee8f8382007-07-14 01:00:42 +0200176 else if (mode == DATE_ISO8601)
177 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
178 tm->tm_year + 1900,
179 tm->tm_mon + 1,
180 tm->tm_mday,
181 tm->tm_hour, tm->tm_min, tm->tm_sec,
182 tz);
Junio C Hamano73013af2007-07-13 23:14:52 -0700183 else if (mode == DATE_RFC2822)
184 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
185 weekday_names[tm->tm_wday], tm->tm_mday,
186 month_names[tm->tm_mon], tm->tm_year + 1900,
187 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100188 else
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -0700189 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100190 weekday_names[tm->tm_wday],
191 month_names[tm->tm_mon],
192 tm->tm_mday,
193 tm->tm_hour, tm->tm_min, tm->tm_sec,
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -0700194 tm->tm_year + 1900,
195 (mode == DATE_LOCAL) ? 0 : ' ',
196 tz);
Linus Torvaldsf80cd782005-05-06 15:28:59 -0700197 return timebuf;
198}
199
200/*
Linus Torvalds89967022005-04-30 13:19:56 -0700201 * Check these. And note how it doesn't do the summer-time conversion.
202 *
203 * In my world, it's always summer, and things are probably a bit off
204 * in other ways too.
205 */
206static const struct {
207 const char *name;
208 int offset;
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700209 int dst;
Linus Torvalds89967022005-04-30 13:19:56 -0700210} timezone_names[] = {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700211 { "IDLW", -12, 0, }, /* International Date Line West */
212 { "NT", -11, 0, }, /* Nome */
213 { "CAT", -10, 0, }, /* Central Alaska */
214 { "HST", -10, 0, }, /* Hawaii Standard */
215 { "HDT", -10, 1, }, /* Hawaii Daylight */
216 { "YST", -9, 0, }, /* Yukon Standard */
217 { "YDT", -9, 1, }, /* Yukon Daylight */
218 { "PST", -8, 0, }, /* Pacific Standard */
219 { "PDT", -8, 1, }, /* Pacific Daylight */
220 { "MST", -7, 0, }, /* Mountain Standard */
221 { "MDT", -7, 1, }, /* Mountain Daylight */
222 { "CST", -6, 0, }, /* Central Standard */
223 { "CDT", -6, 1, }, /* Central Daylight */
224 { "EST", -5, 0, }, /* Eastern Standard */
225 { "EDT", -5, 1, }, /* Eastern Daylight */
226 { "AST", -3, 0, }, /* Atlantic Standard */
227 { "ADT", -3, 1, }, /* Atlantic Daylight */
228 { "WAT", -1, 0, }, /* West Africa */
Edgar Toernigecee9d92005-04-30 09:46:49 -0700229
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700230 { "GMT", 0, 0, }, /* Greenwich Mean */
231 { "UTC", 0, 0, }, /* Universal (Coordinated) */
Linus Torvalds89967022005-04-30 13:19:56 -0700232
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700233 { "WET", 0, 0, }, /* Western European */
234 { "BST", 0, 1, }, /* British Summer */
235 { "CET", +1, 0, }, /* Central European */
236 { "MET", +1, 0, }, /* Middle European */
237 { "MEWT", +1, 0, }, /* Middle European Winter */
238 { "MEST", +1, 1, }, /* Middle European Summer */
239 { "CEST", +1, 1, }, /* Central European Summer */
240 { "MESZ", +1, 1, }, /* Middle European Summer */
241 { "FWT", +1, 0, }, /* French Winter */
242 { "FST", +1, 1, }, /* French Summer */
243 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
Linus Torvalds92e23112005-04-30 15:21:57 -0700244 { "EEST", +2, 1, }, /* Eastern European Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700245 { "WAST", +7, 0, }, /* West Australian Standard */
246 { "WADT", +7, 1, }, /* West Australian Daylight */
247 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
248 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
249 { "EAST", +10, 0, }, /* Eastern Australian Standard */
250 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
251 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
Steven Drake695ed472008-02-26 13:45:53 +1300252 { "NZT", +12, 0, }, /* New Zealand */
253 { "NZST", +12, 0, }, /* New Zealand Standard */
254 { "NZDT", +12, 1, }, /* New Zealand Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700255 { "IDLE", +12, 0, }, /* International Date Line East */
Linus Torvalds89967022005-04-30 13:19:56 -0700256};
257
Linus Torvalds89967022005-04-30 13:19:56 -0700258static int match_string(const char *date, const char *str)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700259{
Linus Torvalds89967022005-04-30 13:19:56 -0700260 int i = 0;
261
262 for (i = 0; *date; date++, str++, i++) {
263 if (*date == *str)
264 continue;
265 if (toupper(*date) == toupper(*str))
266 continue;
267 if (!isalnum(*date))
268 break;
269 return 0;
270 }
271 return i;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700272}
273
Linus Torvalds68849b52005-05-01 12:34:56 -0700274static int skip_alpha(const char *date)
275{
276 int i = 0;
277 do {
278 i++;
279 } while (isalpha(date[i]));
280 return i;
281}
282
Linus Torvalds89967022005-04-30 13:19:56 -0700283/*
284* Parse month, weekday, or timezone name
285*/
286static int match_alpha(const char *date, struct tm *tm, int *offset)
287{
288 int i;
289
290 for (i = 0; i < 12; i++) {
291 int match = match_string(date, month_names[i]);
292 if (match >= 3) {
293 tm->tm_mon = i;
294 return match;
295 }
296 }
297
298 for (i = 0; i < 7; i++) {
299 int match = match_string(date, weekday_names[i]);
300 if (match >= 3) {
301 tm->tm_wday = i;
302 return match;
303 }
304 }
305
Junio C Hamanob4f2a6a2006-03-09 11:58:05 -0800306 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
Linus Torvalds89967022005-04-30 13:19:56 -0700307 int match = match_string(date, timezone_names[i].name);
308 if (match >= 3) {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700309 int off = timezone_names[i].offset;
310
311 /* This is bogus, but we like summer */
312 off += timezone_names[i].dst;
313
Linus Torvalds92e23112005-04-30 15:21:57 -0700314 /* Only use the tz name offset if we don't have anything better */
315 if (*offset == -1)
316 *offset = 60*off;
317
Linus Torvalds89967022005-04-30 13:19:56 -0700318 return match;
319 }
320 }
321
Linus Torvalds68849b52005-05-01 12:34:56 -0700322 if (match_string(date, "PM") == 2) {
Linus Torvalds18b633c2006-09-29 12:36:13 -0700323 tm->tm_hour = (tm->tm_hour % 12) + 12;
324 return 2;
325 }
326
327 if (match_string(date, "AM") == 2) {
328 tm->tm_hour = (tm->tm_hour % 12) + 0;
Linus Torvalds68849b52005-05-01 12:34:56 -0700329 return 2;
330 }
331
Linus Torvalds89967022005-04-30 13:19:56 -0700332 /* BAD CRAP */
Linus Torvalds68849b52005-05-01 12:34:56 -0700333 return skip_alpha(date);
Linus Torvalds89967022005-04-30 13:19:56 -0700334}
335
Junio C Hamano38035cf2006-04-05 15:31:12 -0700336static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
Linus Torvalds89967022005-04-30 13:19:56 -0700337{
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700338 if (month > 0 && month < 13 && day > 0 && day < 32) {
Junio C Hamano38035cf2006-04-05 15:31:12 -0700339 struct tm check = *tm;
340 struct tm *r = (now_tm ? &check : tm);
341 time_t specified;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700342
Junio C Hamano38035cf2006-04-05 15:31:12 -0700343 r->tm_mon = month - 1;
344 r->tm_mday = day;
345 if (year == -1) {
346 if (!now_tm)
347 return 1;
348 r->tm_year = now_tm->tm_year;
349 }
350 else if (year >= 1970 && year < 2100)
351 r->tm_year = year - 1900;
352 else if (year > 70 && year < 100)
353 r->tm_year = year;
354 else if (year < 38)
355 r->tm_year = year + 100;
356 else
357 return 0;
358 if (!now_tm)
359 return 1;
360
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200361 specified = tm_to_time_t(r);
Junio C Hamano38035cf2006-04-05 15:31:12 -0700362
363 /* Be it commit time or author time, it does not make
364 * sense to specify timestamp way into the future. Make
365 * sure it is not later than ten days from now...
366 */
367 if (now + 10*24*3600 < specified)
368 return 0;
369 tm->tm_mon = r->tm_mon;
370 tm->tm_mday = r->tm_mday;
371 if (year != -1)
372 tm->tm_year = r->tm_year;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700373 return 1;
Linus Torvalds89967022005-04-30 13:19:56 -0700374 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700375 return 0;
376}
Linus Torvalds89967022005-04-30 13:19:56 -0700377
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700378static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700379{
Junio C Hamano38035cf2006-04-05 15:31:12 -0700380 time_t now;
381 struct tm now_tm;
382 struct tm *refuse_future;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700383 long num2, num3;
384
385 num2 = strtol(end+1, &end, 10);
386 num3 = -1;
387 if (*end == c && isdigit(end[1]))
388 num3 = strtol(end+1, &end, 10);
389
390 /* Time? Date? */
Linus Torvalds89967022005-04-30 13:19:56 -0700391 switch (c) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700392 case ':':
393 if (num3 < 0)
394 num3 = 0;
395 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
396 tm->tm_hour = num;
397 tm->tm_min = num2;
398 tm->tm_sec = num3;
Linus Torvalds89967022005-04-30 13:19:56 -0700399 break;
400 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700401 return 0;
Linus Torvalds89967022005-04-30 13:19:56 -0700402
403 case '-':
404 case '/':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700405 case '.':
406 now = time(NULL);
407 refuse_future = NULL;
408 if (gmtime_r(&now, &now_tm))
409 refuse_future = &now_tm;
410
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700411 if (num > 70) {
412 /* yyyy-mm-dd? */
Junio C Hamano38035cf2006-04-05 15:31:12 -0700413 if (is_date(num, num2, num3, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700414 break;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700415 /* yyyy-dd-mm? */
Junio C Hamano38035cf2006-04-05 15:31:12 -0700416 if (is_date(num, num3, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700417 break;
418 }
Junio C Hamano38035cf2006-04-05 15:31:12 -0700419 /* Our eastern European friends say dd.mm.yy[yy]
420 * is the norm there, so giving precedence to
421 * mm/dd/yy[yy] form only when separator is not '.'
422 */
423 if (c != '.' &&
424 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700425 break;
Junio C Hamano38035cf2006-04-05 15:31:12 -0700426 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
427 if (is_date(num3, num2, num, refuse_future, now, tm))
428 break;
429 /* Funny European mm.dd.yy */
430 if (c == '.' &&
431 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700432 break;
433 return 0;
434 }
435 return end - date;
436}
437
Linus Torvalds36e49862009-08-22 18:11:44 -0700438/*
439 * Have we filled in any part of the time/date yet?
440 * We just do a binary 'and' to see if the sign bit
441 * is set in all the values.
442 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700443static inline int nodate(struct tm *tm)
444{
Linus Torvalds36e49862009-08-22 18:11:44 -0700445 return (tm->tm_year &
446 tm->tm_mon &
447 tm->tm_mday &
448 tm->tm_hour &
449 tm->tm_min &
450 tm->tm_sec) < 0;
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700451}
452
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700453/*
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700454 * We've seen a digit. Time? Year? Date?
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700455 */
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700456static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700457{
458 int n;
459 char *end;
460 unsigned long num;
461
462 num = strtoul(date, &end, 10);
463
464 /*
Johannes Sixta1a5a632007-06-06 10:11:55 +0200465 * Seconds since 1970? We trigger on that for any numbers with
466 * more than 8 digits. This is because we don't want to rule out
467 * numbers like 20070606 as a YYYYMMDD date.
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700468 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700469 if (num >= 100000000 && nodate(tm)) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700470 time_t time = num;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700471 if (gmtime_r(&time, tm)) {
472 *tm_gmt = 1;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700473 return end - date;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700474 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700475 }
476
477 /*
Junio C Hamano38035cf2006-04-05 15:31:12 -0700478 * Check for special formats: num[-.:/]num[same]num
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700479 */
480 switch (*end) {
481 case ':':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700482 case '.':
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700483 case '/':
484 case '-':
485 if (isdigit(end[1])) {
486 int match = match_multi_number(num, *end, date, end, tm);
487 if (match)
488 return match;
Linus Torvalds89967022005-04-30 13:19:56 -0700489 }
490 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700491
492 /*
493 * None of the special formats? Try to guess what
494 * the number meant. We use the number of digits
495 * to make a more educated guess..
496 */
497 n = 0;
498 do {
499 n++;
500 } while (isdigit(date[n]));
501
502 /* Four-digit year or a timezone? */
503 if (n == 4) {
Paul Eggert7122f822006-06-08 14:54:13 -0700504 if (num <= 1400 && *offset == -1) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700505 unsigned int minutes = num % 100;
506 unsigned int hours = num / 100;
507 *offset = hours*60 + minutes;
508 } else if (num > 1900 && num < 2100)
509 tm->tm_year = num - 1900;
510 return n;
511 }
512
513 /*
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700514 * Ignore lots of numerals. We took care of 4-digit years above.
515 * Days or months must be one or two digits.
516 */
517 if (n > 2)
518 return n;
519
520 /*
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700521 * NOTE! We will give precedence to day-of-month over month or
Junio C Hamano82f9d582005-12-29 01:30:08 -0800522 * year numbers in the 1-12 range. So 05 is always "mday 5",
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700523 * unless we already have a mday..
524 *
525 * IOW, 01 Apr 05 parses as "April 1st, 2005".
526 */
527 if (num > 0 && num < 32 && tm->tm_mday < 0) {
528 tm->tm_mday = num;
529 return n;
530 }
531
532 /* Two-digit year? */
533 if (n == 2 && tm->tm_year < 0) {
534 if (num < 10 && tm->tm_mday >= 0) {
535 tm->tm_year = num + 100;
536 return n;
537 }
538 if (num >= 70) {
539 tm->tm_year = num;
540 return n;
541 }
542 }
543
Linus Torvalds90290552009-08-22 15:10:07 -0700544 if (num > 0 && num < 13 && tm->tm_mon < 0)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700545 tm->tm_mon = num-1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700546
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700547 return n;
Linus Torvalds89967022005-04-30 13:19:56 -0700548}
549
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700550static int match_tz(const char *date, int *offp)
Linus Torvalds89967022005-04-30 13:19:56 -0700551{
552 char *end;
553 int offset = strtoul(date+1, &end, 10);
554 int min, hour;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700555 int n = end - date - 1;
Linus Torvalds89967022005-04-30 13:19:56 -0700556
557 min = offset % 100;
558 hour = offset / 100;
559
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700560 /*
561 * Don't accept any random crap.. At least 3 digits, and
562 * a valid minute. We might want to check that the minutes
563 * are divisible by 30 or something too.
564 */
Linus Torvalds68849b52005-05-01 12:34:56 -0700565 if (min < 60 && n > 2) {
566 offset = hour*60+min;
567 if (*date == '-')
568 offset = -offset;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700569
Linus Torvalds68849b52005-05-01 12:34:56 -0700570 *offp = offset;
571 }
Linus Torvalds89967022005-04-30 13:19:56 -0700572 return end - date;
573}
574
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700575static int date_string(unsigned long date, int offset, char *buf, int len)
576{
577 int sign = '+';
578
579 if (offset < 0) {
580 offset = -offset;
581 sign = '-';
582 }
583 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
584}
585
Edgar Toernigecee9d92005-04-30 09:46:49 -0700586/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
587 (i.e. English) day/month names, and it doesn't work correctly with %z. */
Linus Torvalds2a390642005-09-19 15:53:50 -0700588int parse_date(const char *date, char *result, int maxlen)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700589{
590 struct tm tm;
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700591 int offset, tm_gmt;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700592 time_t then;
593
594 memset(&tm, 0, sizeof(tm));
Linus Torvalds89967022005-04-30 13:19:56 -0700595 tm.tm_year = -1;
596 tm.tm_mon = -1;
597 tm.tm_mday = -1;
Linus Torvaldseaa85122005-04-30 14:25:02 -0700598 tm.tm_isdst = -1;
Linus Torvalds36e49862009-08-22 18:11:44 -0700599 tm.tm_hour = -1;
600 tm.tm_min = -1;
601 tm.tm_sec = -1;
Linus Torvaldseaa85122005-04-30 14:25:02 -0700602 offset = -1;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700603 tm_gmt = 0;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700604
Linus Torvalds89967022005-04-30 13:19:56 -0700605 for (;;) {
606 int match = 0;
607 unsigned char c = *date;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700608
Linus Torvalds89967022005-04-30 13:19:56 -0700609 /* Stop at end of string or newline */
610 if (!c || c == '\n')
611 break;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700612
Linus Torvalds89967022005-04-30 13:19:56 -0700613 if (isalpha(c))
614 match = match_alpha(date, &tm, &offset);
615 else if (isdigit(c))
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700616 match = match_digit(date, &tm, &offset, &tm_gmt);
Linus Torvalds89967022005-04-30 13:19:56 -0700617 else if ((c == '-' || c == '+') && isdigit(date[1]))
618 match = match_tz(date, &offset);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700619
Linus Torvalds89967022005-04-30 13:19:56 -0700620 if (!match) {
621 /* BAD CRAP */
622 match = 1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700623 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700624
Linus Torvalds89967022005-04-30 13:19:56 -0700625 date += match;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700626 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700627
Linus Torvaldseaa85122005-04-30 14:25:02 -0700628 /* mktime uses local timezone */
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200629 then = tm_to_time_t(&tm);
Linus Torvaldseaa85122005-04-30 14:25:02 -0700630 if (offset == -1)
631 offset = (then - mktime(&tm)) / 60;
632
Edgar Toernigecee9d92005-04-30 09:46:49 -0700633 if (then == -1)
Linus Torvalds2a390642005-09-19 15:53:50 -0700634 return -1;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700635
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700636 if (!tm_gmt)
637 then -= offset * 60;
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700638 return date_string(then, offset, result, maxlen);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700639}
640
Andy Parkins856665f2007-09-28 15:17:31 +0100641enum date_mode parse_date_format(const char *format)
642{
643 if (!strcmp(format, "relative"))
644 return DATE_RELATIVE;
645 else if (!strcmp(format, "iso8601") ||
646 !strcmp(format, "iso"))
647 return DATE_ISO8601;
648 else if (!strcmp(format, "rfc2822") ||
649 !strcmp(format, "rfc"))
650 return DATE_RFC2822;
651 else if (!strcmp(format, "short"))
652 return DATE_SHORT;
653 else if (!strcmp(format, "local"))
654 return DATE_LOCAL;
655 else if (!strcmp(format, "default"))
656 return DATE_NORMAL;
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800657 else if (!strcmp(format, "raw"))
658 return DATE_RAW;
Andy Parkins856665f2007-09-28 15:17:31 +0100659 else
660 die("unknown date format %s", format);
661}
662
Edgar Toernigecee9d92005-04-30 09:46:49 -0700663void datestamp(char *buf, int bufsize)
664{
665 time_t now;
666 int offset;
667
668 time(&now);
669
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200670 offset = tm_to_time_t(localtime(&now)) - now;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700671 offset /= 60;
672
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700673 date_string(now, offset, buf, bufsize);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700674}
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800675
Linus Torvalds90290552009-08-22 15:10:07 -0700676/*
677 * Relative time update (eg "2 days ago"). If we haven't set the time
678 * yet, we need to set it from current time.
679 */
680static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800681{
Linus Torvalds90290552009-08-22 15:10:07 -0700682 time_t n;
683
684 if (tm->tm_mday < 0)
685 tm->tm_mday = now->tm_mday;
686 if (tm->tm_mon < 0)
687 tm->tm_mon = now->tm_mon;
688 if (tm->tm_year < 0) {
689 tm->tm_year = now->tm_year;
690 if (tm->tm_mon > now->tm_mon)
691 tm->tm_year--;
692 }
693
694 n = mktime(tm) - sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800695 localtime_r(&n, tm);
Linus Torvalds90290552009-08-22 15:10:07 -0700696 return n;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800697}
698
Linus Torvalds90290552009-08-22 15:10:07 -0700699static void date_yesterday(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800700{
Linus Torvalds90290552009-08-22 15:10:07 -0700701 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800702}
703
Linus Torvalds90290552009-08-22 15:10:07 -0700704static void date_time(struct tm *tm, struct tm *now, int hour)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800705{
706 if (tm->tm_hour < hour)
Linus Torvalds90290552009-08-22 15:10:07 -0700707 date_yesterday(tm, now, NULL);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800708 tm->tm_hour = hour;
709 tm->tm_min = 0;
710 tm->tm_sec = 0;
711}
712
Linus Torvalds90290552009-08-22 15:10:07 -0700713static void date_midnight(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800714{
Linus Torvalds90290552009-08-22 15:10:07 -0700715 date_time(tm, now, 0);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800716}
717
Linus Torvalds90290552009-08-22 15:10:07 -0700718static void date_noon(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800719{
Linus Torvalds90290552009-08-22 15:10:07 -0700720 date_time(tm, now, 12);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800721}
722
Linus Torvalds90290552009-08-22 15:10:07 -0700723static void date_tea(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800724{
Linus Torvalds90290552009-08-22 15:10:07 -0700725 date_time(tm, now, 17);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800726}
727
Linus Torvalds90290552009-08-22 15:10:07 -0700728static void date_pm(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -0700729{
Linus Torvalds18b633c2006-09-29 12:36:13 -0700730 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -0700731 *num = 0;
732
Linus Torvalds18b633c2006-09-29 12:36:13 -0700733 hour = tm->tm_hour;
734 if (n) {
735 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -0700736 tm->tm_min = 0;
737 tm->tm_sec = 0;
738 }
Linus Torvalds18b633c2006-09-29 12:36:13 -0700739 tm->tm_hour = (hour % 12) + 12;
Linus Torvalds393d3402006-09-28 12:14:27 -0700740}
741
Linus Torvalds90290552009-08-22 15:10:07 -0700742static void date_am(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -0700743{
Linus Torvalds18b633c2006-09-29 12:36:13 -0700744 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -0700745 *num = 0;
746
Linus Torvalds18b633c2006-09-29 12:36:13 -0700747 hour = tm->tm_hour;
748 if (n) {
749 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -0700750 tm->tm_min = 0;
751 tm->tm_sec = 0;
752 }
Linus Torvalds18b633c2006-09-29 12:36:13 -0700753 tm->tm_hour = (hour % 12);
Linus Torvalds393d3402006-09-28 12:14:27 -0700754}
755
Linus Torvalds90290552009-08-22 15:10:07 -0700756static void date_never(struct tm *tm, struct tm *now, int *num)
Johannes Schindelinaf663662007-07-24 19:18:34 +0100757{
Olivier Marin8c6b5782008-06-17 18:34:57 +0200758 time_t n = 0;
759 localtime_r(&n, tm);
Johannes Schindelinaf663662007-07-24 19:18:34 +0100760}
761
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800762static const struct special {
763 const char *name;
Linus Torvalds90290552009-08-22 15:10:07 -0700764 void (*fn)(struct tm *, struct tm *, int *);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800765} special[] = {
766 { "yesterday", date_yesterday },
767 { "noon", date_noon },
768 { "midnight", date_midnight },
769 { "tea", date_tea },
Linus Torvalds393d3402006-09-28 12:14:27 -0700770 { "PM", date_pm },
771 { "AM", date_am },
Johannes Schindelinaf663662007-07-24 19:18:34 +0100772 { "never", date_never },
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800773 { NULL }
774};
775
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800776static const char *number_name[] = {
777 "zero", "one", "two", "three", "four",
778 "five", "six", "seven", "eight", "nine", "ten",
779};
780
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800781static const struct typelen {
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800782 const char *type;
783 int length;
784} typelen[] = {
785 { "seconds", 1 },
786 { "minutes", 60 },
787 { "hours", 60*60 },
788 { "days", 24*60*60 },
789 { "weeks", 7*24*60*60 },
790 { NULL }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700791};
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800792
Linus Torvalds90290552009-08-22 15:10:07 -0700793static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800794{
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800795 const struct typelen *tl;
796 const struct special *s;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800797 const char *end = date;
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +0200798 int i;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800799
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +0200800 while (isalpha(*++end));
801 ;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800802
803 for (i = 0; i < 12; i++) {
804 int match = match_string(date, month_names[i]);
805 if (match >= 3) {
806 tm->tm_mon = i;
807 return end;
808 }
809 }
810
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800811 for (s = special; s->name; s++) {
812 int len = strlen(s->name);
813 if (match_string(date, s->name) == len) {
Linus Torvalds90290552009-08-22 15:10:07 -0700814 s->fn(tm, now, num);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800815 return end;
816 }
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800817 }
818
819 if (!*num) {
820 for (i = 1; i < 11; i++) {
821 int len = strlen(number_name[i]);
822 if (match_string(date, number_name[i]) == len) {
823 *num = i;
824 return end;
825 }
826 }
827 if (match_string(date, "last") == 4)
828 *num = 1;
829 return end;
830 }
831
832 tl = typelen;
833 while (tl->type) {
834 int len = strlen(tl->type);
835 if (match_string(date, tl->type) >= len-1) {
Linus Torvalds90290552009-08-22 15:10:07 -0700836 update_tm(tm, now, tl->length * *num);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800837 *num = 0;
838 return end;
839 }
840 tl++;
841 }
842
Linus Torvalds6b7b0422005-11-17 12:36:30 -0800843 for (i = 0; i < 7; i++) {
844 int match = match_string(date, weekday_names[i]);
845 if (match >= 3) {
846 int diff, n = *num -1;
847 *num = 0;
848
849 diff = tm->tm_wday - i;
850 if (diff <= 0)
851 n++;
852 diff += 7*n;
853
Linus Torvalds90290552009-08-22 15:10:07 -0700854 update_tm(tm, now, diff * 24 * 60 * 60);
Linus Torvalds6b7b0422005-11-17 12:36:30 -0800855 return end;
856 }
857 }
858
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800859 if (match_string(date, "months") >= 5) {
Jeff King931e8e22009-08-30 22:31:42 -0400860 int n;
861 update_tm(tm, now, 0); /* fill in date fields if needed */
862 n = tm->tm_mon - *num;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800863 *num = 0;
864 while (n < 0) {
865 n += 12;
866 tm->tm_year--;
867 }
868 tm->tm_mon = n;
869 return end;
870 }
871
872 if (match_string(date, "years") >= 4) {
Jeff King931e8e22009-08-30 22:31:42 -0400873 update_tm(tm, now, 0); /* fill in date fields if needed */
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800874 tm->tm_year -= *num;
875 *num = 0;
876 return end;
877 }
878
879 return end;
880}
881
Linus Torvaldse92a54d2006-09-28 12:12:28 -0700882static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
883{
884 char *end;
885 unsigned long number = strtoul(date, &end, 10);
886
Linus Torvalds393d3402006-09-28 12:14:27 -0700887 switch (*end) {
888 case ':':
889 case '.':
890 case '/':
891 case '-':
892 if (isdigit(end[1])) {
893 int match = match_multi_number(number, *end, date, end, tm);
894 if (match)
895 return date + match;
896 }
897 }
898
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700899 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
900 if (date[0] != '0' || end - date <= 2)
901 *num = number;
Linus Torvaldse92a54d2006-09-28 12:12:28 -0700902 return end;
903}
904
Linus Torvalds90290552009-08-22 15:10:07 -0700905/*
906 * Do we have a pending number at the end, or when
907 * we see a new one? Let's assume it's a month day,
908 * as in "Dec 6, 1992"
909 */
910static void pending_number(struct tm *tm, int *num)
911{
912 int number = *num;
913
914 if (number) {
915 *num = 0;
916 if (tm->tm_mday < 0 && number < 32)
917 tm->tm_mday = number;
Linus Torvalds36e49862009-08-22 18:11:44 -0700918 else if (tm->tm_mon < 0 && number < 13)
919 tm->tm_mon = number-1;
920 else if (tm->tm_year < 0) {
921 if (number > 1969 && number < 2100)
922 tm->tm_year = number - 1900;
923 else if (number > 69 && number < 100)
924 tm->tm_year = number;
925 else if (number < 38)
926 tm->tm_year = 100 + number;
927 /* We screw up for number = 00 ? */
928 }
Linus Torvalds90290552009-08-22 15:10:07 -0700929 }
930}
931
Alex Riesen33012fc2009-08-30 22:26:05 -0400932static unsigned long approxidate_str(const char *date, const struct timeval *tv)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800933{
934 int number = 0;
935 struct tm tm, now;
Bernd Ahlersf697b332009-04-06 19:26:37 +0200936 time_t time_sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800937
Alex Riesen33012fc2009-08-30 22:26:05 -0400938 time_sec = tv->tv_sec;
Bernd Ahlersf697b332009-04-06 19:26:37 +0200939 localtime_r(&time_sec, &tm);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800940 now = tm;
Linus Torvalds90290552009-08-22 15:10:07 -0700941
942 tm.tm_year = -1;
943 tm.tm_mon = -1;
944 tm.tm_mday = -1;
945
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800946 for (;;) {
947 unsigned char c = *date;
948 if (!c)
949 break;
950 date++;
951 if (isdigit(c)) {
Linus Torvalds90290552009-08-22 15:10:07 -0700952 pending_number(&tm, &number);
Linus Torvaldse92a54d2006-09-28 12:12:28 -0700953 date = approxidate_digit(date-1, &tm, &number);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800954 continue;
955 }
956 if (isalpha(c))
Linus Torvalds90290552009-08-22 15:10:07 -0700957 date = approxidate_alpha(date-1, &tm, &now, &number);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800958 }
Linus Torvalds90290552009-08-22 15:10:07 -0700959 pending_number(&tm, &number);
960 return update_tm(&tm, &now, 0);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800961}
Alex Riesen33012fc2009-08-30 22:26:05 -0400962
963unsigned long approxidate_relative(const char *date, const struct timeval *tv)
964{
965 char buffer[50];
966
967 if (parse_date(date, buffer, sizeof(buffer)) > 0)
968 return strtoul(buffer, NULL, 0);
969
970 return approxidate_str(date, tv);
971}
972
973unsigned long approxidate(const char *date)
974{
975 struct timeval tv;
976 char buffer[50];
977
978 if (parse_date(date, buffer, sizeof(buffer)) > 0)
979 return strtoul(buffer, NULL, 0);
980
981 gettimeofday(&tv, NULL);
982 return approxidate_str(date, &tv);
983}