blob: 29f15404feab0d98f377b312a3fcf5d8888fdb0a [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 */
Junio C Hamano23418ea2010-01-11 23:52:47 -080012static time_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
Jonathan Nieder7d29afd2012-04-23 19:30:23 +070089void show_date_relative(unsigned long time, int tz,
Alex Riesen33012fc2009-08-30 22:26:05 -040090 const struct timeval *now,
Jonathan Nieder7d29afd2012-04-23 19:30:23 +070091 struct strbuf *timebuf)
Alex Riesen33012fc2009-08-30 22:26:05 -040092{
93 unsigned long diff;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +070094 if (now->tv_sec < time) {
95 strbuf_addstr(timebuf, _("in the future"));
96 return;
97 }
Alex Riesen33012fc2009-08-30 22:26:05 -040098 diff = now->tv_sec - time;
99 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700100 strbuf_addf(timebuf,
101 Q_("%lu second ago", "%lu seconds ago", diff), diff);
102 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400103 }
104 /* Turn it into minutes */
105 diff = (diff + 30) / 60;
106 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700107 strbuf_addf(timebuf,
108 Q_("%lu minute ago", "%lu minutes ago", diff), diff);
109 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400110 }
111 /* Turn it into hours */
112 diff = (diff + 30) / 60;
113 if (diff < 36) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700114 strbuf_addf(timebuf,
115 Q_("%lu hour ago", "%lu hours ago", diff), diff);
116 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400117 }
118 /* We deal with number of days from here on */
119 diff = (diff + 12) / 24;
120 if (diff < 14) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700121 strbuf_addf(timebuf,
122 Q_("%lu day ago", "%lu days ago", diff), diff);
123 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400124 }
125 /* Say weeks for the past 10 weeks or so */
126 if (diff < 70) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700127 strbuf_addf(timebuf,
128 Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
129 (diff + 3) / 7);
130 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400131 }
132 /* Say months for the past 12 months or so */
Johan Sageryddbc1b1f2009-10-03 13:20:18 +0900133 if (diff < 365) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700134 strbuf_addf(timebuf,
135 Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
136 (diff + 15) / 30);
137 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400138 }
139 /* Give years and months for 5 years or so */
140 if (diff < 1825) {
Michael J Gruberf1e9c542011-04-20 11:12:11 +0200141 unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
142 unsigned long years = totalmonths / 12;
143 unsigned long months = totalmonths % 12;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700144 if (months) {
145 struct strbuf sb = STRBUF_INIT;
146 strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
147 /* TRANSLATORS: "%s" is "<n> years" */
148 strbuf_addf(timebuf,
149 Q_("%s, %lu month ago", "%s, %lu months ago", months),
150 sb.buf, months);
151 strbuf_release(&sb);
152 } else
153 strbuf_addf(timebuf,
154 Q_("%lu year ago", "%lu years ago", years), years);
155 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400156 }
157 /* Otherwise, just years. Centuries is probably overkill. */
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700158 strbuf_addf(timebuf,
159 Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
160 (diff + 183) / 365);
Alex Riesen33012fc2009-08-30 22:26:05 -0400161}
162
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100163const char *show_date(unsigned long time, int tz, enum date_mode mode)
Junio C Hamano2a387042006-05-01 01:44:33 -0700164{
165 struct tm *tm;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700166 static struct strbuf timebuf = STRBUF_INIT;
Junio C Hamano2a387042006-05-01 01:44:33 -0700167
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800168 if (mode == DATE_RAW) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700169 strbuf_reset(&timebuf);
170 strbuf_addf(&timebuf, "%lu %+05d", time, tz);
171 return timebuf.buf;
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800172 }
173
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100174 if (mode == DATE_RELATIVE) {
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700175 struct timeval now;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700176
177 strbuf_reset(&timebuf);
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700178 gettimeofday(&now, NULL);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700179 show_date_relative(time, tz, &now, &timebuf);
180 return timebuf.buf;
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700181 }
182
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -0700183 if (mode == DATE_LOCAL)
184 tz = local_tzoffset(time);
185
Junio C Hamano2a387042006-05-01 01:44:33 -0700186 tm = time_to_tm(time, tz);
Linus Torvaldsf80cd782005-05-06 15:28:59 -0700187 if (!tm)
188 return NULL;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700189
190 strbuf_reset(&timebuf);
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100191 if (mode == DATE_SHORT)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700192 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100193 tm->tm_mon + 1, tm->tm_mday);
Robin Rosenbergee8f8382007-07-14 01:00:42 +0200194 else if (mode == DATE_ISO8601)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700195 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
Robin Rosenbergee8f8382007-07-14 01:00:42 +0200196 tm->tm_year + 1900,
197 tm->tm_mon + 1,
198 tm->tm_mday,
199 tm->tm_hour, tm->tm_min, tm->tm_sec,
200 tz);
Junio C Hamano73013af2007-07-13 23:14:52 -0700201 else if (mode == DATE_RFC2822)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700202 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
Junio C Hamano73013af2007-07-13 23:14:52 -0700203 weekday_names[tm->tm_wday], tm->tm_mday,
204 month_names[tm->tm_mon], tm->tm_year + 1900,
205 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100206 else
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700207 strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100208 weekday_names[tm->tm_wday],
209 month_names[tm->tm_mon],
210 tm->tm_mday,
211 tm->tm_hour, tm->tm_min, tm->tm_sec,
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -0700212 tm->tm_year + 1900,
213 (mode == DATE_LOCAL) ? 0 : ' ',
214 tz);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700215 return timebuf.buf;
Linus Torvaldsf80cd782005-05-06 15:28:59 -0700216}
217
218/*
Linus Torvalds89967022005-04-30 13:19:56 -0700219 * Check these. And note how it doesn't do the summer-time conversion.
220 *
221 * In my world, it's always summer, and things are probably a bit off
222 * in other ways too.
223 */
224static const struct {
225 const char *name;
226 int offset;
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700227 int dst;
Linus Torvalds89967022005-04-30 13:19:56 -0700228} timezone_names[] = {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700229 { "IDLW", -12, 0, }, /* International Date Line West */
230 { "NT", -11, 0, }, /* Nome */
231 { "CAT", -10, 0, }, /* Central Alaska */
232 { "HST", -10, 0, }, /* Hawaii Standard */
233 { "HDT", -10, 1, }, /* Hawaii Daylight */
234 { "YST", -9, 0, }, /* Yukon Standard */
235 { "YDT", -9, 1, }, /* Yukon Daylight */
236 { "PST", -8, 0, }, /* Pacific Standard */
237 { "PDT", -8, 1, }, /* Pacific Daylight */
238 { "MST", -7, 0, }, /* Mountain Standard */
239 { "MDT", -7, 1, }, /* Mountain Daylight */
240 { "CST", -6, 0, }, /* Central Standard */
241 { "CDT", -6, 1, }, /* Central Daylight */
242 { "EST", -5, 0, }, /* Eastern Standard */
243 { "EDT", -5, 1, }, /* Eastern Daylight */
244 { "AST", -3, 0, }, /* Atlantic Standard */
245 { "ADT", -3, 1, }, /* Atlantic Daylight */
246 { "WAT", -1, 0, }, /* West Africa */
Edgar Toernigecee9d92005-04-30 09:46:49 -0700247
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700248 { "GMT", 0, 0, }, /* Greenwich Mean */
249 { "UTC", 0, 0, }, /* Universal (Coordinated) */
Marcus Comstedt75b37e72010-05-17 21:07:10 +0200250 { "Z", 0, 0, }, /* Zulu, alias for UTC */
Linus Torvalds89967022005-04-30 13:19:56 -0700251
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700252 { "WET", 0, 0, }, /* Western European */
253 { "BST", 0, 1, }, /* British Summer */
254 { "CET", +1, 0, }, /* Central European */
255 { "MET", +1, 0, }, /* Middle European */
256 { "MEWT", +1, 0, }, /* Middle European Winter */
257 { "MEST", +1, 1, }, /* Middle European Summer */
258 { "CEST", +1, 1, }, /* Central European Summer */
259 { "MESZ", +1, 1, }, /* Middle European Summer */
260 { "FWT", +1, 0, }, /* French Winter */
261 { "FST", +1, 1, }, /* French Summer */
262 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
Linus Torvalds92e23112005-04-30 15:21:57 -0700263 { "EEST", +2, 1, }, /* Eastern European Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700264 { "WAST", +7, 0, }, /* West Australian Standard */
265 { "WADT", +7, 1, }, /* West Australian Daylight */
266 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
267 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
268 { "EAST", +10, 0, }, /* Eastern Australian Standard */
269 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
270 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
Steven Drake695ed472008-02-26 13:45:53 +1300271 { "NZT", +12, 0, }, /* New Zealand */
272 { "NZST", +12, 0, }, /* New Zealand Standard */
273 { "NZDT", +12, 1, }, /* New Zealand Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700274 { "IDLE", +12, 0, }, /* International Date Line East */
Linus Torvalds89967022005-04-30 13:19:56 -0700275};
276
Linus Torvalds89967022005-04-30 13:19:56 -0700277static int match_string(const char *date, const char *str)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700278{
Linus Torvalds89967022005-04-30 13:19:56 -0700279 int i = 0;
280
281 for (i = 0; *date; date++, str++, i++) {
282 if (*date == *str)
283 continue;
284 if (toupper(*date) == toupper(*str))
285 continue;
286 if (!isalnum(*date))
287 break;
288 return 0;
289 }
290 return i;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700291}
292
Linus Torvalds68849b52005-05-01 12:34:56 -0700293static int skip_alpha(const char *date)
294{
295 int i = 0;
296 do {
297 i++;
298 } while (isalpha(date[i]));
299 return i;
300}
301
Linus Torvalds89967022005-04-30 13:19:56 -0700302/*
303* Parse month, weekday, or timezone name
304*/
305static int match_alpha(const char *date, struct tm *tm, int *offset)
306{
307 int i;
308
309 for (i = 0; i < 12; i++) {
310 int match = match_string(date, month_names[i]);
311 if (match >= 3) {
312 tm->tm_mon = i;
313 return match;
314 }
315 }
316
317 for (i = 0; i < 7; i++) {
318 int match = match_string(date, weekday_names[i]);
319 if (match >= 3) {
320 tm->tm_wday = i;
321 return match;
322 }
323 }
324
Junio C Hamanob4f2a6a2006-03-09 11:58:05 -0800325 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
Linus Torvalds89967022005-04-30 13:19:56 -0700326 int match = match_string(date, timezone_names[i].name);
Marcus Comstedt75b37e72010-05-17 21:07:10 +0200327 if (match >= 3 || match == strlen(timezone_names[i].name)) {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700328 int off = timezone_names[i].offset;
329
330 /* This is bogus, but we like summer */
331 off += timezone_names[i].dst;
332
Linus Torvalds92e23112005-04-30 15:21:57 -0700333 /* Only use the tz name offset if we don't have anything better */
334 if (*offset == -1)
335 *offset = 60*off;
336
Linus Torvalds89967022005-04-30 13:19:56 -0700337 return match;
338 }
339 }
340
Linus Torvalds68849b52005-05-01 12:34:56 -0700341 if (match_string(date, "PM") == 2) {
Linus Torvalds18b633c2006-09-29 12:36:13 -0700342 tm->tm_hour = (tm->tm_hour % 12) + 12;
343 return 2;
344 }
345
346 if (match_string(date, "AM") == 2) {
347 tm->tm_hour = (tm->tm_hour % 12) + 0;
Linus Torvalds68849b52005-05-01 12:34:56 -0700348 return 2;
349 }
350
Linus Torvalds89967022005-04-30 13:19:56 -0700351 /* BAD CRAP */
Linus Torvalds68849b52005-05-01 12:34:56 -0700352 return skip_alpha(date);
Linus Torvalds89967022005-04-30 13:19:56 -0700353}
354
Junio C Hamano38035cf2006-04-05 15:31:12 -0700355static 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 -0700356{
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700357 if (month > 0 && month < 13 && day > 0 && day < 32) {
Junio C Hamano38035cf2006-04-05 15:31:12 -0700358 struct tm check = *tm;
359 struct tm *r = (now_tm ? &check : tm);
360 time_t specified;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700361
Junio C Hamano38035cf2006-04-05 15:31:12 -0700362 r->tm_mon = month - 1;
363 r->tm_mday = day;
364 if (year == -1) {
365 if (!now_tm)
366 return 1;
367 r->tm_year = now_tm->tm_year;
368 }
369 else if (year >= 1970 && year < 2100)
370 r->tm_year = year - 1900;
371 else if (year > 70 && year < 100)
372 r->tm_year = year;
373 else if (year < 38)
374 r->tm_year = year + 100;
375 else
376 return 0;
377 if (!now_tm)
378 return 1;
379
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200380 specified = tm_to_time_t(r);
Junio C Hamano38035cf2006-04-05 15:31:12 -0700381
382 /* Be it commit time or author time, it does not make
383 * sense to specify timestamp way into the future. Make
384 * sure it is not later than ten days from now...
385 */
Mike Gorchake6e87512013-02-25 23:51:16 +0200386 if ((specified != -1) && (now + 10*24*3600 < specified))
Junio C Hamano38035cf2006-04-05 15:31:12 -0700387 return 0;
388 tm->tm_mon = r->tm_mon;
389 tm->tm_mday = r->tm_mday;
390 if (year != -1)
391 tm->tm_year = r->tm_year;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700392 return 1;
Linus Torvalds89967022005-04-30 13:19:56 -0700393 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700394 return 0;
395}
Linus Torvalds89967022005-04-30 13:19:56 -0700396
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700397static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700398{
Junio C Hamano38035cf2006-04-05 15:31:12 -0700399 time_t now;
400 struct tm now_tm;
401 struct tm *refuse_future;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700402 long num2, num3;
403
404 num2 = strtol(end+1, &end, 10);
405 num3 = -1;
406 if (*end == c && isdigit(end[1]))
407 num3 = strtol(end+1, &end, 10);
408
409 /* Time? Date? */
Linus Torvalds89967022005-04-30 13:19:56 -0700410 switch (c) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700411 case ':':
412 if (num3 < 0)
413 num3 = 0;
414 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
415 tm->tm_hour = num;
416 tm->tm_min = num2;
417 tm->tm_sec = num3;
Linus Torvalds89967022005-04-30 13:19:56 -0700418 break;
419 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700420 return 0;
Linus Torvalds89967022005-04-30 13:19:56 -0700421
422 case '-':
423 case '/':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700424 case '.':
425 now = time(NULL);
426 refuse_future = NULL;
427 if (gmtime_r(&now, &now_tm))
428 refuse_future = &now_tm;
429
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700430 if (num > 70) {
431 /* yyyy-mm-dd? */
Junio C Hamano38035cf2006-04-05 15:31:12 -0700432 if (is_date(num, num2, num3, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700433 break;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700434 /* yyyy-dd-mm? */
Junio C Hamano38035cf2006-04-05 15:31:12 -0700435 if (is_date(num, num3, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700436 break;
437 }
Junio C Hamano38035cf2006-04-05 15:31:12 -0700438 /* Our eastern European friends say dd.mm.yy[yy]
439 * is the norm there, so giving precedence to
440 * mm/dd/yy[yy] form only when separator is not '.'
441 */
442 if (c != '.' &&
443 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700444 break;
Junio C Hamano38035cf2006-04-05 15:31:12 -0700445 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
446 if (is_date(num3, num2, num, refuse_future, now, tm))
447 break;
448 /* Funny European mm.dd.yy */
449 if (c == '.' &&
450 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700451 break;
452 return 0;
453 }
454 return end - date;
455}
456
Linus Torvalds36e49862009-08-22 18:11:44 -0700457/*
458 * Have we filled in any part of the time/date yet?
459 * We just do a binary 'and' to see if the sign bit
460 * is set in all the values.
461 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700462static inline int nodate(struct tm *tm)
463{
Linus Torvalds36e49862009-08-22 18:11:44 -0700464 return (tm->tm_year &
465 tm->tm_mon &
466 tm->tm_mday &
467 tm->tm_hour &
468 tm->tm_min &
469 tm->tm_sec) < 0;
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700470}
471
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700472/*
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700473 * We've seen a digit. Time? Year? Date?
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700474 */
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700475static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700476{
477 int n;
478 char *end;
479 unsigned long num;
480
481 num = strtoul(date, &end, 10);
482
483 /*
Johannes Sixta1a5a632007-06-06 10:11:55 +0200484 * Seconds since 1970? We trigger on that for any numbers with
485 * more than 8 digits. This is because we don't want to rule out
486 * numbers like 20070606 as a YYYYMMDD date.
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700487 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700488 if (num >= 100000000 && nodate(tm)) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700489 time_t time = num;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700490 if (gmtime_r(&time, tm)) {
491 *tm_gmt = 1;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700492 return end - date;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700493 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700494 }
495
496 /*
Junio C Hamano38035cf2006-04-05 15:31:12 -0700497 * Check for special formats: num[-.:/]num[same]num
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700498 */
499 switch (*end) {
500 case ':':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700501 case '.':
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700502 case '/':
503 case '-':
504 if (isdigit(end[1])) {
505 int match = match_multi_number(num, *end, date, end, tm);
506 if (match)
507 return match;
Linus Torvalds89967022005-04-30 13:19:56 -0700508 }
509 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700510
511 /*
512 * None of the special formats? Try to guess what
513 * the number meant. We use the number of digits
514 * to make a more educated guess..
515 */
516 n = 0;
517 do {
518 n++;
519 } while (isdigit(date[n]));
520
521 /* Four-digit year or a timezone? */
522 if (n == 4) {
Paul Eggert7122f822006-06-08 14:54:13 -0700523 if (num <= 1400 && *offset == -1) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700524 unsigned int minutes = num % 100;
525 unsigned int hours = num / 100;
526 *offset = hours*60 + minutes;
527 } else if (num > 1900 && num < 2100)
528 tm->tm_year = num - 1900;
529 return n;
530 }
531
532 /*
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700533 * Ignore lots of numerals. We took care of 4-digit years above.
534 * Days or months must be one or two digits.
535 */
536 if (n > 2)
537 return n;
538
539 /*
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700540 * NOTE! We will give precedence to day-of-month over month or
Junio C Hamano82f9d582005-12-29 01:30:08 -0800541 * year numbers in the 1-12 range. So 05 is always "mday 5",
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700542 * unless we already have a mday..
543 *
544 * IOW, 01 Apr 05 parses as "April 1st, 2005".
545 */
546 if (num > 0 && num < 32 && tm->tm_mday < 0) {
547 tm->tm_mday = num;
548 return n;
549 }
550
551 /* Two-digit year? */
552 if (n == 2 && tm->tm_year < 0) {
553 if (num < 10 && tm->tm_mday >= 0) {
554 tm->tm_year = num + 100;
555 return n;
556 }
557 if (num >= 70) {
558 tm->tm_year = num;
559 return n;
560 }
561 }
562
Linus Torvalds90290552009-08-22 15:10:07 -0700563 if (num > 0 && num < 13 && tm->tm_mon < 0)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700564 tm->tm_mon = num-1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700565
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700566 return n;
Linus Torvalds89967022005-04-30 13:19:56 -0700567}
568
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700569static int match_tz(const char *date, int *offp)
Linus Torvalds89967022005-04-30 13:19:56 -0700570{
571 char *end;
Haitao Liee646eb2011-09-09 18:10:33 +0800572 int hour = strtoul(date + 1, &end, 10);
573 int n = end - (date + 1);
574 int min = 0;
Linus Torvalds89967022005-04-30 13:19:56 -0700575
Haitao Liee646eb2011-09-09 18:10:33 +0800576 if (n == 4) {
577 /* hhmm */
578 min = hour % 100;
579 hour = hour / 100;
580 } else if (n != 2) {
581 min = 99; /* random crap */
582 } else if (*end == ':') {
583 /* hh:mm? */
584 min = strtoul(end + 1, &end, 10);
585 if (end - (date + 1) != 5)
586 min = 99; /* random crap */
587 } /* otherwise we parsed "hh" */
Linus Torvalds89967022005-04-30 13:19:56 -0700588
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700589 /*
Haitao Liee646eb2011-09-09 18:10:33 +0800590 * Don't accept any random crap. Even though some places have
591 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
592 * UTC+14), there is something wrong if hour part is much
593 * larger than that. We might also want to check that the
594 * minutes are divisible by 15 or something too. (Offset of
595 * Kathmandu, Nepal is UTC+5:45)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700596 */
Haitao Liee646eb2011-09-09 18:10:33 +0800597 if (min < 60 && hour < 24) {
598 int offset = hour * 60 + min;
Linus Torvalds68849b52005-05-01 12:34:56 -0700599 if (*date == '-')
600 offset = -offset;
Linus Torvalds68849b52005-05-01 12:34:56 -0700601 *offp = offset;
602 }
Linus Torvalds89967022005-04-30 13:19:56 -0700603 return end - date;
604}
605
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700606static int date_string(unsigned long date, int offset, char *buf, int len)
607{
608 int sign = '+';
609
610 if (offset < 0) {
611 offset = -offset;
612 sign = '-';
613 }
614 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
615}
616
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800617/*
618 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
619 * only when it appears not as part of any other string.
620 */
621static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset)
622{
623 char *end;
624 unsigned long stamp;
625 int ofs;
626
Junio C Hamanobe21d162012-07-12 13:46:49 -0700627 if (*date < '0' || '9' < *date)
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800628 return -1;
629 stamp = strtoul(date, &end, 10);
630 if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-'))
631 return -1;
632 date = end + 2;
633 ofs = strtol(date, &end, 10);
634 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
635 return -1;
636 ofs = (ofs / 100) * 60 + (ofs % 100);
637 if (date[-1] == '-')
638 ofs = -ofs;
639 *timestamp = stamp;
640 *offset = ofs;
641 return 0;
642}
643
Edgar Toernigecee9d92005-04-30 09:46:49 -0700644/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
645 (i.e. English) day/month names, and it doesn't work correctly with %z. */
Jonathan Nieder9644c062010-07-15 18:22:57 +0200646int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700647{
648 struct tm tm;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200649 int tm_gmt;
650 unsigned long dummy_timestamp;
651 int dummy_offset;
652
653 if (!timestamp)
654 timestamp = &dummy_timestamp;
655 if (!offset)
656 offset = &dummy_offset;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700657
658 memset(&tm, 0, sizeof(tm));
Linus Torvalds89967022005-04-30 13:19:56 -0700659 tm.tm_year = -1;
660 tm.tm_mon = -1;
661 tm.tm_mday = -1;
Linus Torvaldseaa85122005-04-30 14:25:02 -0700662 tm.tm_isdst = -1;
Linus Torvalds36e49862009-08-22 18:11:44 -0700663 tm.tm_hour = -1;
664 tm.tm_min = -1;
665 tm.tm_sec = -1;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200666 *offset = -1;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700667 tm_gmt = 0;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700668
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800669 if (*date == '@' &&
670 !match_object_header_date(date + 1, timestamp, offset))
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800671 return 0; /* success */
Linus Torvalds89967022005-04-30 13:19:56 -0700672 for (;;) {
673 int match = 0;
674 unsigned char c = *date;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700675
Linus Torvalds89967022005-04-30 13:19:56 -0700676 /* Stop at end of string or newline */
677 if (!c || c == '\n')
678 break;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700679
Linus Torvalds89967022005-04-30 13:19:56 -0700680 if (isalpha(c))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200681 match = match_alpha(date, &tm, offset);
Linus Torvalds89967022005-04-30 13:19:56 -0700682 else if (isdigit(c))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200683 match = match_digit(date, &tm, offset, &tm_gmt);
Linus Torvalds89967022005-04-30 13:19:56 -0700684 else if ((c == '-' || c == '+') && isdigit(date[1]))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200685 match = match_tz(date, offset);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700686
Linus Torvalds89967022005-04-30 13:19:56 -0700687 if (!match) {
688 /* BAD CRAP */
689 match = 1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700690 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700691
Linus Torvalds89967022005-04-30 13:19:56 -0700692 date += match;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700693 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700694
Linus Torvaldseaa85122005-04-30 14:25:02 -0700695 /* mktime uses local timezone */
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200696 *timestamp = tm_to_time_t(&tm);
Mike Gorchake1033da2013-02-25 23:53:40 +0200697 if (*offset == -1) {
698 time_t temp_time = mktime(&tm);
699 if ((time_t)*timestamp > temp_time) {
700 *offset = ((time_t)*timestamp - temp_time) / 60;
701 } else {
702 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
703 }
704 }
Linus Torvaldseaa85122005-04-30 14:25:02 -0700705
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200706 if (*timestamp == -1)
Linus Torvalds2a390642005-09-19 15:53:50 -0700707 return -1;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700708
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700709 if (!tm_gmt)
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200710 *timestamp -= *offset * 60;
Jonathan Nieder9644c062010-07-15 18:22:57 +0200711 return 0; /* success */
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200712}
713
Junio C Hamano3d27b9b2013-04-17 15:38:08 -0700714int parse_expiry_date(const char *date, unsigned long *timestamp)
715{
716 int errors = 0;
717
718 if (!strcmp(date, "never") || !strcmp(date, "false"))
719 *timestamp = 0;
720 else if (!strcmp(date, "all") || !strcmp(date, "now"))
721 /*
722 * We take over "now" here, which usually translates
723 * to the current timestamp. This is because the user
724 * really means to expire everything she has done in
725 * the past, and by definition reflogs are the record
726 * of the past, and there is nothing from the future
727 * to be kept.
728 */
729 *timestamp = ULONG_MAX;
730 else
731 *timestamp = approxidate_careful(date, &errors);
732
733 return errors;
734}
735
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200736int parse_date(const char *date, char *result, int maxlen)
737{
738 unsigned long timestamp;
739 int offset;
Jonathan Nieder9644c062010-07-15 18:22:57 +0200740 if (parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200741 return -1;
Jonathan Nieder9644c062010-07-15 18:22:57 +0200742 return date_string(timestamp, offset, result, maxlen);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700743}
744
Andy Parkins856665f2007-09-28 15:17:31 +0100745enum date_mode parse_date_format(const char *format)
746{
747 if (!strcmp(format, "relative"))
748 return DATE_RELATIVE;
749 else if (!strcmp(format, "iso8601") ||
750 !strcmp(format, "iso"))
751 return DATE_ISO8601;
752 else if (!strcmp(format, "rfc2822") ||
753 !strcmp(format, "rfc"))
754 return DATE_RFC2822;
755 else if (!strcmp(format, "short"))
756 return DATE_SHORT;
757 else if (!strcmp(format, "local"))
758 return DATE_LOCAL;
759 else if (!strcmp(format, "default"))
760 return DATE_NORMAL;
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800761 else if (!strcmp(format, "raw"))
762 return DATE_RAW;
Andy Parkins856665f2007-09-28 15:17:31 +0100763 else
764 die("unknown date format %s", format);
765}
766
Edgar Toernigecee9d92005-04-30 09:46:49 -0700767void datestamp(char *buf, int bufsize)
768{
769 time_t now;
770 int offset;
771
772 time(&now);
773
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200774 offset = tm_to_time_t(localtime(&now)) - now;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700775 offset /= 60;
776
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700777 date_string(now, offset, buf, bufsize);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700778}
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800779
Linus Torvalds90290552009-08-22 15:10:07 -0700780/*
781 * Relative time update (eg "2 days ago"). If we haven't set the time
782 * yet, we need to set it from current time.
783 */
784static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800785{
Linus Torvalds90290552009-08-22 15:10:07 -0700786 time_t n;
787
788 if (tm->tm_mday < 0)
789 tm->tm_mday = now->tm_mday;
790 if (tm->tm_mon < 0)
791 tm->tm_mon = now->tm_mon;
792 if (tm->tm_year < 0) {
793 tm->tm_year = now->tm_year;
794 if (tm->tm_mon > now->tm_mon)
795 tm->tm_year--;
796 }
797
798 n = mktime(tm) - sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800799 localtime_r(&n, tm);
Linus Torvalds90290552009-08-22 15:10:07 -0700800 return n;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800801}
802
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800803static void date_now(struct tm *tm, struct tm *now, int *num)
804{
805 update_tm(tm, now, 0);
806}
807
Linus Torvalds90290552009-08-22 15:10:07 -0700808static void date_yesterday(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800809{
Linus Torvalds90290552009-08-22 15:10:07 -0700810 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800811}
812
Linus Torvalds90290552009-08-22 15:10:07 -0700813static void date_time(struct tm *tm, struct tm *now, int hour)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800814{
815 if (tm->tm_hour < hour)
Linus Torvalds90290552009-08-22 15:10:07 -0700816 date_yesterday(tm, now, NULL);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800817 tm->tm_hour = hour;
818 tm->tm_min = 0;
819 tm->tm_sec = 0;
820}
821
Linus Torvalds90290552009-08-22 15:10:07 -0700822static void date_midnight(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800823{
Linus Torvalds90290552009-08-22 15:10:07 -0700824 date_time(tm, now, 0);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800825}
826
Linus Torvalds90290552009-08-22 15:10:07 -0700827static void date_noon(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800828{
Linus Torvalds90290552009-08-22 15:10:07 -0700829 date_time(tm, now, 12);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800830}
831
Linus Torvalds90290552009-08-22 15:10:07 -0700832static void date_tea(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800833{
Linus Torvalds90290552009-08-22 15:10:07 -0700834 date_time(tm, now, 17);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800835}
836
Linus Torvalds90290552009-08-22 15:10:07 -0700837static void date_pm(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -0700838{
Linus Torvalds18b633c2006-09-29 12:36:13 -0700839 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -0700840 *num = 0;
841
Linus Torvalds18b633c2006-09-29 12:36:13 -0700842 hour = tm->tm_hour;
843 if (n) {
844 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -0700845 tm->tm_min = 0;
846 tm->tm_sec = 0;
847 }
Linus Torvalds18b633c2006-09-29 12:36:13 -0700848 tm->tm_hour = (hour % 12) + 12;
Linus Torvalds393d3402006-09-28 12:14:27 -0700849}
850
Linus Torvalds90290552009-08-22 15:10:07 -0700851static void date_am(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -0700852{
Linus Torvalds18b633c2006-09-29 12:36:13 -0700853 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -0700854 *num = 0;
855
Linus Torvalds18b633c2006-09-29 12:36:13 -0700856 hour = tm->tm_hour;
857 if (n) {
858 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -0700859 tm->tm_min = 0;
860 tm->tm_sec = 0;
861 }
Linus Torvalds18b633c2006-09-29 12:36:13 -0700862 tm->tm_hour = (hour % 12);
Linus Torvalds393d3402006-09-28 12:14:27 -0700863}
864
Linus Torvalds90290552009-08-22 15:10:07 -0700865static void date_never(struct tm *tm, struct tm *now, int *num)
Johannes Schindelinaf663662007-07-24 19:18:34 +0100866{
Olivier Marin8c6b5782008-06-17 18:34:57 +0200867 time_t n = 0;
868 localtime_r(&n, tm);
Johannes Schindelinaf663662007-07-24 19:18:34 +0100869}
870
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800871static const struct special {
872 const char *name;
Linus Torvalds90290552009-08-22 15:10:07 -0700873 void (*fn)(struct tm *, struct tm *, int *);
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800874} special[] = {
875 { "yesterday", date_yesterday },
876 { "noon", date_noon },
877 { "midnight", date_midnight },
878 { "tea", date_tea },
Linus Torvalds393d3402006-09-28 12:14:27 -0700879 { "PM", date_pm },
880 { "AM", date_am },
Johannes Schindelinaf663662007-07-24 19:18:34 +0100881 { "never", date_never },
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800882 { "now", date_now },
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800883 { NULL }
884};
885
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800886static const char *number_name[] = {
887 "zero", "one", "two", "three", "four",
888 "five", "six", "seven", "eight", "nine", "ten",
889};
890
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800891static const struct typelen {
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800892 const char *type;
893 int length;
894} typelen[] = {
895 { "seconds", 1 },
896 { "minutes", 60 },
897 { "hours", 60*60 },
898 { "days", 24*60*60 },
899 { "weeks", 7*24*60*60 },
900 { NULL }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700901};
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800902
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800903static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800904{
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800905 const struct typelen *tl;
906 const struct special *s;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800907 const char *end = date;
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +0200908 int i;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800909
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +0200910 while (isalpha(*++end));
911 ;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800912
913 for (i = 0; i < 12; i++) {
914 int match = match_string(date, month_names[i]);
915 if (match >= 3) {
916 tm->tm_mon = i;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800917 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800918 return end;
919 }
920 }
921
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800922 for (s = special; s->name; s++) {
923 int len = strlen(s->name);
924 if (match_string(date, s->name) == len) {
Linus Torvalds90290552009-08-22 15:10:07 -0700925 s->fn(tm, now, num);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800926 *touched = 1;
Linus Torvaldsa8aca412005-11-18 08:56:40 -0800927 return end;
928 }
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800929 }
930
931 if (!*num) {
932 for (i = 1; i < 11; i++) {
933 int len = strlen(number_name[i]);
934 if (match_string(date, number_name[i]) == len) {
935 *num = i;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800936 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800937 return end;
938 }
939 }
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800940 if (match_string(date, "last") == 4) {
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800941 *num = 1;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800942 *touched = 1;
943 }
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800944 return end;
945 }
946
947 tl = typelen;
948 while (tl->type) {
949 int len = strlen(tl->type);
950 if (match_string(date, tl->type) >= len-1) {
Linus Torvalds90290552009-08-22 15:10:07 -0700951 update_tm(tm, now, tl->length * *num);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800952 *num = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800953 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800954 return end;
955 }
956 tl++;
957 }
958
Linus Torvalds6b7b0422005-11-17 12:36:30 -0800959 for (i = 0; i < 7; i++) {
960 int match = match_string(date, weekday_names[i]);
961 if (match >= 3) {
962 int diff, n = *num -1;
963 *num = 0;
964
965 diff = tm->tm_wday - i;
966 if (diff <= 0)
967 n++;
968 diff += 7*n;
969
Linus Torvalds90290552009-08-22 15:10:07 -0700970 update_tm(tm, now, diff * 24 * 60 * 60);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800971 *touched = 1;
Linus Torvalds6b7b0422005-11-17 12:36:30 -0800972 return end;
973 }
974 }
975
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800976 if (match_string(date, "months") >= 5) {
Jeff King931e8e22009-08-30 22:31:42 -0400977 int n;
978 update_tm(tm, now, 0); /* fill in date fields if needed */
979 n = tm->tm_mon - *num;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800980 *num = 0;
981 while (n < 0) {
982 n += 12;
983 tm->tm_year--;
984 }
985 tm->tm_mon = n;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800986 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800987 return end;
988 }
989
990 if (match_string(date, "years") >= 4) {
Jeff King931e8e22009-08-30 22:31:42 -0400991 update_tm(tm, now, 0); /* fill in date fields if needed */
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800992 tm->tm_year -= *num;
993 *num = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800994 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800995 return end;
996 }
997
998 return end;
999}
1000
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001001static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
1002{
1003 char *end;
1004 unsigned long number = strtoul(date, &end, 10);
1005
Linus Torvalds393d3402006-09-28 12:14:27 -07001006 switch (*end) {
1007 case ':':
1008 case '.':
1009 case '/':
1010 case '-':
1011 if (isdigit(end[1])) {
1012 int match = match_multi_number(number, *end, date, end, tm);
1013 if (match)
1014 return date + match;
1015 }
1016 }
1017
Linus Torvalds9f2b6d22008-08-16 21:25:40 -07001018 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1019 if (date[0] != '0' || end - date <= 2)
1020 *num = number;
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001021 return end;
1022}
1023
Linus Torvalds90290552009-08-22 15:10:07 -07001024/*
1025 * Do we have a pending number at the end, or when
1026 * we see a new one? Let's assume it's a month day,
1027 * as in "Dec 6, 1992"
1028 */
1029static void pending_number(struct tm *tm, int *num)
1030{
1031 int number = *num;
1032
1033 if (number) {
1034 *num = 0;
1035 if (tm->tm_mday < 0 && number < 32)
1036 tm->tm_mday = number;
Linus Torvalds36e49862009-08-22 18:11:44 -07001037 else if (tm->tm_mon < 0 && number < 13)
1038 tm->tm_mon = number-1;
1039 else if (tm->tm_year < 0) {
1040 if (number > 1969 && number < 2100)
1041 tm->tm_year = number - 1900;
1042 else if (number > 69 && number < 100)
1043 tm->tm_year = number;
1044 else if (number < 38)
1045 tm->tm_year = 100 + number;
1046 /* We screw up for number = 00 ? */
1047 }
Linus Torvalds90290552009-08-22 15:10:07 -07001048 }
1049}
1050
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001051static unsigned long approxidate_str(const char *date,
1052 const struct timeval *tv,
1053 int *error_ret)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001054{
1055 int number = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001056 int touched = 0;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001057 struct tm tm, now;
Bernd Ahlersf697b332009-04-06 19:26:37 +02001058 time_t time_sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001059
Alex Riesen33012fc2009-08-30 22:26:05 -04001060 time_sec = tv->tv_sec;
Bernd Ahlersf697b332009-04-06 19:26:37 +02001061 localtime_r(&time_sec, &tm);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001062 now = tm;
Linus Torvalds90290552009-08-22 15:10:07 -07001063
1064 tm.tm_year = -1;
1065 tm.tm_mon = -1;
1066 tm.tm_mday = -1;
1067
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001068 for (;;) {
1069 unsigned char c = *date;
1070 if (!c)
1071 break;
1072 date++;
1073 if (isdigit(c)) {
Linus Torvalds90290552009-08-22 15:10:07 -07001074 pending_number(&tm, &number);
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001075 date = approxidate_digit(date-1, &tm, &number);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001076 touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001077 continue;
1078 }
1079 if (isalpha(c))
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001080 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001081 }
Linus Torvalds90290552009-08-22 15:10:07 -07001082 pending_number(&tm, &number);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001083 if (!touched)
1084 *error_ret = 1;
Linus Torvalds90290552009-08-22 15:10:07 -07001085 return update_tm(&tm, &now, 0);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001086}
Alex Riesen33012fc2009-08-30 22:26:05 -04001087
1088unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1089{
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001090 unsigned long timestamp;
1091 int offset;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001092 int errors = 0;
Alex Riesen33012fc2009-08-30 22:26:05 -04001093
Jonathan Nieder9644c062010-07-15 18:22:57 +02001094 if (!parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001095 return timestamp;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001096 return approxidate_str(date, tv, &errors);
Alex Riesen33012fc2009-08-30 22:26:05 -04001097}
1098
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001099unsigned long approxidate_careful(const char *date, int *error_ret)
Alex Riesen33012fc2009-08-30 22:26:05 -04001100{
1101 struct timeval tv;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001102 unsigned long timestamp;
1103 int offset;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001104 int dummy = 0;
1105 if (!error_ret)
1106 error_ret = &dummy;
Alex Riesen33012fc2009-08-30 22:26:05 -04001107
Jonathan Nieder9644c062010-07-15 18:22:57 +02001108 if (!parse_date_basic(date, &timestamp, &offset)) {
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001109 *error_ret = 0;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001110 return timestamp;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001111 }
Alex Riesen33012fc2009-08-30 22:26:05 -04001112
1113 gettimeofday(&tv, NULL);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001114 return approxidate_str(date, &tv, error_ret);
Alex Riesen33012fc2009-08-30 22:26:05 -04001115}