🌐 AI搜索 & 代理 主页
blob: 9bc15df6f9dfa635fe91760914473d953b7c970b [file] [log] [blame]
Edgar Toernigecee9d92005-04-30 16:46:491/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
Linus Torvaldse99d59f2005-05-20 18:46:107#include "cache.h"
8
Johannes Sixtbb5799d2008-06-23 06:31:419/*
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
11 */
Junio C Hamano23418ea2010-01-12 07:52:4712static time_t tm_to_time_t(const struct tm *tm)
Edgar Toernigecee9d92005-04-30 16:46:4913{
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-23 01:11:4427 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
28 return -1;
Edgar Toernigecee9d92005-04-30 16:46:4929 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 20:19:5634 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
Edgar Toernigecee9d92005-04-30 16:46:4936};
37
38static const char *weekday_names[] = {
Linus Torvalds6b7b0422005-11-17 20:36:3039 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
Edgar Toernigecee9d92005-04-30 16:46:4940};
41
Johannes Schindelindddbad72017-04-26 19:29:3142static time_t gm_time_t(timestamp_t time, int tz)
Linus Torvalds9a8e35e2006-08-26 22:45:2643{
44 int minutes;
45
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
Johannes Schindelin1e65a982017-04-26 19:29:3649
50 if (minutes > 0) {
51 if (unsigned_add_overflows(time, minutes * 60))
52 die("Timestamp+tz too large: %"PRItime" +%04d",
53 time, tz);
54 } else if (time < -minutes * 60)
55 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
56 time += minutes * 60;
57 if (date_overflows(time))
58 die("Timestamp too large for this system: %"PRItime, time);
59 return (time_t)time;
Linus Torvalds9a8e35e2006-08-26 22:45:2660}
61
Linus Torvalds89967022005-04-30 20:19:5662/*
Linus Torvaldsf80cd782005-05-06 22:28:5963 * The "tz" thing is passed in as this strange "decimal parse of tz"
64 * thing, which means that tz -0100 is passed in as the integer -100,
65 * even though it means "sixty minutes off"
66 */
Johannes Schindelindddbad72017-04-26 19:29:3167static struct tm *time_to_tm(timestamp_t time, int tz)
Linus Torvaldsf80cd782005-05-06 22:28:5968{
Linus Torvalds9a8e35e2006-08-26 22:45:2669 time_t t = gm_time_t(time, tz);
Junio C Hamano2a387042006-05-01 08:44:3370 return gmtime(&t);
71}
72
Junio C Hamano9eafe862017-06-22 21:15:2573static struct tm *time_to_tm_local(timestamp_t time)
Jeff King6eced3e2017-06-15 13:52:1774{
75 time_t t = time;
76 return localtime(&t);
77}
78
Junio C Hamanoa7b02cc2007-04-25 06:36:2279/*
80 * What value of "tz" was in effect back then at "time" in the
81 * local timezone?
82 */
Johannes Schindelindddbad72017-04-26 19:29:3183static int local_tzoffset(timestamp_t time)
Junio C Hamanoa7b02cc2007-04-25 06:36:2284{
85 time_t t, t_local;
86 struct tm tm;
87 int offset, eastwest;
88
Johannes Schindelin1e65a982017-04-26 19:29:3689 if (date_overflows(time))
90 die("Timestamp too large for this system: %"PRItime, time);
91
92 t = (time_t)time;
Junio C Hamanoa7b02cc2007-04-25 06:36:2293 localtime_r(&t, &tm);
Johannes Sixtbb5799d2008-06-23 06:31:4194 t_local = tm_to_time_t(&tm);
Junio C Hamanoa7b02cc2007-04-25 06:36:2295
Jeff Kingbab74832016-06-20 21:14:1496 if (t_local == -1)
97 return 0; /* error; just use +0000 */
Junio C Hamanoa7b02cc2007-04-25 06:36:2298 if (t_local < t) {
99 eastwest = -1;
100 offset = t - t_local;
101 } else {
102 eastwest = 1;
103 offset = t_local - t;
104 }
105 offset /= 60; /* in minutes */
106 offset = (offset % 60) + ((offset / 60) * 100);
107 return offset * eastwest;
108}
109
Johannes Schindelindddbad72017-04-26 19:29:31110void show_date_relative(timestamp_t time, int tz,
Alex Riesen33012fc2009-08-31 02:26:05111 const struct timeval *now,
Jonathan Nieder7d29afd2012-04-23 12:30:23112 struct strbuf *timebuf)
Alex Riesen33012fc2009-08-31 02:26:05113{
Johannes Schindelindddbad72017-04-26 19:29:31114 timestamp_t diff;
Jonathan Nieder7d29afd2012-04-23 12:30:23115 if (now->tv_sec < time) {
116 strbuf_addstr(timebuf, _("in the future"));
117 return;
118 }
Alex Riesen33012fc2009-08-31 02:26:05119 diff = now->tv_sec - time;
120 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 12:30:23121 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48122 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 12:30:23123 return;
Alex Riesen33012fc2009-08-31 02:26:05124 }
125 /* Turn it into minutes */
126 diff = (diff + 30) / 60;
127 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 12:30:23128 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48129 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 12:30:23130 return;
Alex Riesen33012fc2009-08-31 02:26:05131 }
132 /* Turn it into hours */
133 diff = (diff + 30) / 60;
134 if (diff < 36) {
Jonathan Nieder7d29afd2012-04-23 12:30:23135 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48136 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 12:30:23137 return;
Alex Riesen33012fc2009-08-31 02:26:05138 }
139 /* We deal with number of days from here on */
140 diff = (diff + 12) / 24;
141 if (diff < 14) {
Jonathan Nieder7d29afd2012-04-23 12:30:23142 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48143 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 12:30:23144 return;
Alex Riesen33012fc2009-08-31 02:26:05145 }
146 /* Say weeks for the past 10 weeks or so */
147 if (diff < 70) {
Jonathan Nieder7d29afd2012-04-23 12:30:23148 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48149 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
Jonathan Nieder7d29afd2012-04-23 12:30:23150 (diff + 3) / 7);
151 return;
Alex Riesen33012fc2009-08-31 02:26:05152 }
153 /* Say months for the past 12 months or so */
Johan Sageryddbc1b1f2009-10-03 04:20:18154 if (diff < 365) {
Jonathan Nieder7d29afd2012-04-23 12:30:23155 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48156 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
Jonathan Nieder7d29afd2012-04-23 12:30:23157 (diff + 15) / 30);
158 return;
Alex Riesen33012fc2009-08-31 02:26:05159 }
160 /* Give years and months for 5 years or so */
161 if (diff < 1825) {
Johannes Schindelindddbad72017-04-26 19:29:31162 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
163 timestamp_t years = totalmonths / 12;
164 timestamp_t months = totalmonths % 12;
Jonathan Nieder7d29afd2012-04-23 12:30:23165 if (months) {
166 struct strbuf sb = STRBUF_INIT;
Johannes Schindelincb71f8b2017-04-21 10:45:48167 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
Jonathan Nieder7d29afd2012-04-23 12:30:23168 strbuf_addf(timebuf,
Jiang Xinfcaed042014-04-17 05:37:17169 /* TRANSLATORS: "%s" is "<n> years" */
Johannes Schindelincb71f8b2017-04-21 10:45:48170 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
Jonathan Nieder7d29afd2012-04-23 12:30:23171 sb.buf, months);
172 strbuf_release(&sb);
173 } else
174 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48175 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
Jonathan Nieder7d29afd2012-04-23 12:30:23176 return;
Alex Riesen33012fc2009-08-31 02:26:05177 }
178 /* Otherwise, just years. Centuries is probably overkill. */
Jonathan Nieder7d29afd2012-04-23 12:30:23179 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 10:45:48180 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
Jonathan Nieder7d29afd2012-04-23 12:30:23181 (diff + 183) / 365);
Alex Riesen33012fc2009-08-31 02:26:05182}
183
Jeff Kinga5481a62015-06-25 16:55:02184struct date_mode *date_mode_from_type(enum date_mode_type type)
185{
186 static struct date_mode mode;
Jeff Kingaa1462c2015-06-25 16:55:45187 if (type == DATE_STRFTIME)
Johannes Schindelin033abf92018-05-02 09:38:39188 BUG("cannot create anonymous strftime date_mode struct");
Jeff Kinga5481a62015-06-25 16:55:02189 mode.type = type;
Jeff Kingadd00ba2015-09-03 21:48:59190 mode.local = 0;
Jeff Kinga5481a62015-06-25 16:55:02191 return &mode;
192}
193
Johannes Schindelindddbad72017-04-26 19:29:31194const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
Junio C Hamano2a387042006-05-01 08:44:33195{
196 struct tm *tm;
Jonathan Nieder7d29afd2012-04-23 12:30:23197 static struct strbuf timebuf = STRBUF_INIT;
Junio C Hamano2a387042006-05-01 08:44:33198
Jeff King642833d2016-07-22 19:51:49199 if (mode->type == DATE_UNIX) {
200 strbuf_reset(&timebuf);
Johannes Schindelincb71f8b2017-04-21 10:45:48201 strbuf_addf(&timebuf, "%"PRItime, time);
Jeff King642833d2016-07-22 19:51:49202 return timebuf.buf;
203 }
204
Jeff Kingadd00ba2015-09-03 21:48:59205 if (mode->local)
John Keepingdc6d7822015-09-03 21:48:58206 tz = local_tzoffset(time);
207
Jeff Kinga5481a62015-06-25 16:55:02208 if (mode->type == DATE_RAW) {
Jonathan Nieder7d29afd2012-04-23 12:30:23209 strbuf_reset(&timebuf);
Johannes Schindelincb71f8b2017-04-21 10:45:48210 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
Jonathan Nieder7d29afd2012-04-23 12:30:23211 return timebuf.buf;
Linus Torvalds7dff9b32009-02-20 22:15:22212 }
213
Jeff Kinga5481a62015-06-25 16:55:02214 if (mode->type == DATE_RELATIVE) {
Linus Torvalds9a8e35e2006-08-26 22:45:26215 struct timeval now;
Jonathan Nieder7d29afd2012-04-23 12:30:23216
217 strbuf_reset(&timebuf);
Linus Torvalds9a8e35e2006-08-26 22:45:26218 gettimeofday(&now, NULL);
Jonathan Nieder7d29afd2012-04-23 12:30:23219 show_date_relative(time, tz, &now, &timebuf);
220 return timebuf.buf;
Linus Torvalds9a8e35e2006-08-26 22:45:26221 }
222
Jeff King6eced3e2017-06-15 13:52:17223 if (mode->local)
224 tm = time_to_tm_local(time);
225 else
226 tm = time_to_tm(time, tz);
Jeff King2b158462014-02-24 07:49:05227 if (!tm) {
228 tm = time_to_tm(0, 0);
229 tz = 0;
230 }
Jonathan Nieder7d29afd2012-04-23 12:30:23231
232 strbuf_reset(&timebuf);
Jeff Kinga5481a62015-06-25 16:55:02233 if (mode->type == DATE_SHORT)
Jonathan Nieder7d29afd2012-04-23 12:30:23234 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
Johannes Schindelinf8493ec2007-02-27 15:21:04235 tm->tm_mon + 1, tm->tm_mday);
Jeff Kinga5481a62015-06-25 16:55:02236 else if (mode->type == DATE_ISO8601)
Jonathan Nieder7d29afd2012-04-23 12:30:23237 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
Robin Rosenbergee8f8382007-07-13 23:00:42238 tm->tm_year + 1900,
239 tm->tm_mon + 1,
240 tm->tm_mday,
241 tm->tm_hour, tm->tm_min, tm->tm_sec,
242 tz);
Jeff Kinga5481a62015-06-25 16:55:02243 else if (mode->type == DATE_ISO8601_STRICT) {
Beat Bolli466fb672014-08-29 16:58:42244 char sign = (tz >= 0) ? '+' : '-';
245 tz = abs(tz);
246 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
247 tm->tm_year + 1900,
248 tm->tm_mon + 1,
249 tm->tm_mday,
250 tm->tm_hour, tm->tm_min, tm->tm_sec,
251 sign, tz / 100, tz % 100);
Jeff Kinga5481a62015-06-25 16:55:02252 } else if (mode->type == DATE_RFC2822)
Jonathan Nieder7d29afd2012-04-23 12:30:23253 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
Junio C Hamano73013af2007-07-14 06:14:52254 weekday_names[tm->tm_wday], tm->tm_mday,
255 month_names[tm->tm_mon], tm->tm_year + 1900,
256 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
Jeff Kingaa1462c2015-06-25 16:55:45257 else if (mode->type == DATE_STRFTIME)
Jeff King6eced3e2017-06-15 13:52:17258 strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
Ævar Arnfjörð Bjarmason3b702232017-07-01 13:15:47259 !mode->local);
Johannes Schindelinf8493ec2007-02-27 15:21:04260 else
Jonathan Nieder7d29afd2012-04-23 12:30:23261 strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
Johannes Schindelinf8493ec2007-02-27 15:21:04262 weekday_names[tm->tm_wday],
263 month_names[tm->tm_mon],
264 tm->tm_mday,
265 tm->tm_hour, tm->tm_min, tm->tm_sec,
Junio C Hamanoa7b02cc2007-04-25 06:36:22266 tm->tm_year + 1900,
Jeff Kingadd00ba2015-09-03 21:48:59267 mode->local ? 0 : ' ',
Junio C Hamanoa7b02cc2007-04-25 06:36:22268 tz);
Jonathan Nieder7d29afd2012-04-23 12:30:23269 return timebuf.buf;
Linus Torvaldsf80cd782005-05-06 22:28:59270}
271
Linus Torvaldsf80cd782005-05-06 22:28:59272/*
Linus Torvalds89967022005-04-30 20:19:56273 * Check these. And note how it doesn't do the summer-time conversion.
274 *
275 * In my world, it's always summer, and things are probably a bit off
276 * in other ways too.
277 */
278static const struct {
279 const char *name;
280 int offset;
Linus Torvalds5e2a78a2005-04-30 21:53:12281 int dst;
Linus Torvalds89967022005-04-30 20:19:56282} timezone_names[] = {
Linus Torvalds5e2a78a2005-04-30 21:53:12283 { "IDLW", -12, 0, }, /* International Date Line West */
284 { "NT", -11, 0, }, /* Nome */
285 { "CAT", -10, 0, }, /* Central Alaska */
286 { "HST", -10, 0, }, /* Hawaii Standard */
287 { "HDT", -10, 1, }, /* Hawaii Daylight */
288 { "YST", -9, 0, }, /* Yukon Standard */
289 { "YDT", -9, 1, }, /* Yukon Daylight */
290 { "PST", -8, 0, }, /* Pacific Standard */
291 { "PDT", -8, 1, }, /* Pacific Daylight */
292 { "MST", -7, 0, }, /* Mountain Standard */
293 { "MDT", -7, 1, }, /* Mountain Daylight */
294 { "CST", -6, 0, }, /* Central Standard */
295 { "CDT", -6, 1, }, /* Central Daylight */
296 { "EST", -5, 0, }, /* Eastern Standard */
297 { "EDT", -5, 1, }, /* Eastern Daylight */
298 { "AST", -3, 0, }, /* Atlantic Standard */
299 { "ADT", -3, 1, }, /* Atlantic Daylight */
300 { "WAT", -1, 0, }, /* West Africa */
Edgar Toernigecee9d92005-04-30 16:46:49301
Linus Torvalds5e2a78a2005-04-30 21:53:12302 { "GMT", 0, 0, }, /* Greenwich Mean */
303 { "UTC", 0, 0, }, /* Universal (Coordinated) */
Marcus Comstedt75b37e72010-05-17 19:07:10304 { "Z", 0, 0, }, /* Zulu, alias for UTC */
Linus Torvalds89967022005-04-30 20:19:56305
Linus Torvalds5e2a78a2005-04-30 21:53:12306 { "WET", 0, 0, }, /* Western European */
307 { "BST", 0, 1, }, /* British Summer */
308 { "CET", +1, 0, }, /* Central European */
309 { "MET", +1, 0, }, /* Middle European */
310 { "MEWT", +1, 0, }, /* Middle European Winter */
311 { "MEST", +1, 1, }, /* Middle European Summer */
312 { "CEST", +1, 1, }, /* Central European Summer */
313 { "MESZ", +1, 1, }, /* Middle European Summer */
314 { "FWT", +1, 0, }, /* French Winter */
315 { "FST", +1, 1, }, /* French Summer */
316 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
Linus Torvalds92e23112005-04-30 22:21:57317 { "EEST", +2, 1, }, /* Eastern European Daylight */
Linus Torvalds5e2a78a2005-04-30 21:53:12318 { "WAST", +7, 0, }, /* West Australian Standard */
319 { "WADT", +7, 1, }, /* West Australian Daylight */
320 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
321 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
322 { "EAST", +10, 0, }, /* Eastern Australian Standard */
323 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
324 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
Steven Drake695ed472008-02-26 00:45:53325 { "NZT", +12, 0, }, /* New Zealand */
326 { "NZST", +12, 0, }, /* New Zealand Standard */
327 { "NZDT", +12, 1, }, /* New Zealand Daylight */
Linus Torvalds5e2a78a2005-04-30 21:53:12328 { "IDLE", +12, 0, }, /* International Date Line East */
Linus Torvalds89967022005-04-30 20:19:56329};
330
Linus Torvalds89967022005-04-30 20:19:56331static int match_string(const char *date, const char *str)
Edgar Toernigecee9d92005-04-30 16:46:49332{
Linus Torvalds89967022005-04-30 20:19:56333 int i = 0;
334
335 for (i = 0; *date; date++, str++, i++) {
336 if (*date == *str)
337 continue;
338 if (toupper(*date) == toupper(*str))
339 continue;
340 if (!isalnum(*date))
341 break;
342 return 0;
343 }
344 return i;
Edgar Toernigecee9d92005-04-30 16:46:49345}
346
Linus Torvalds68849b52005-05-01 19:34:56347static int skip_alpha(const char *date)
348{
349 int i = 0;
350 do {
351 i++;
352 } while (isalpha(date[i]));
353 return i;
354}
355
Linus Torvalds89967022005-04-30 20:19:56356/*
357* Parse month, weekday, or timezone name
358*/
359static int match_alpha(const char *date, struct tm *tm, int *offset)
360{
361 int i;
362
363 for (i = 0; i < 12; i++) {
364 int match = match_string(date, month_names[i]);
365 if (match >= 3) {
366 tm->tm_mon = i;
367 return match;
368 }
369 }
370
371 for (i = 0; i < 7; i++) {
372 int match = match_string(date, weekday_names[i]);
373 if (match >= 3) {
374 tm->tm_wday = i;
375 return match;
376 }
377 }
378
Junio C Hamanob4f2a6a2006-03-09 19:58:05379 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
Linus Torvalds89967022005-04-30 20:19:56380 int match = match_string(date, timezone_names[i].name);
Marcus Comstedt75b37e72010-05-17 19:07:10381 if (match >= 3 || match == strlen(timezone_names[i].name)) {
Linus Torvalds5e2a78a2005-04-30 21:53:12382 int off = timezone_names[i].offset;
383
384 /* This is bogus, but we like summer */
385 off += timezone_names[i].dst;
386
Linus Torvalds92e23112005-04-30 22:21:57387 /* Only use the tz name offset if we don't have anything better */
388 if (*offset == -1)
389 *offset = 60*off;
390
Linus Torvalds89967022005-04-30 20:19:56391 return match;
392 }
393 }
394
Linus Torvalds68849b52005-05-01 19:34:56395 if (match_string(date, "PM") == 2) {
Linus Torvalds18b633c2006-09-29 19:36:13396 tm->tm_hour = (tm->tm_hour % 12) + 12;
397 return 2;
398 }
399
400 if (match_string(date, "AM") == 2) {
401 tm->tm_hour = (tm->tm_hour % 12) + 0;
Linus Torvalds68849b52005-05-01 19:34:56402 return 2;
403 }
404
Linus Torvalds89967022005-04-30 20:19:56405 /* BAD CRAP */
Linus Torvalds68849b52005-05-01 19:34:56406 return skip_alpha(date);
Linus Torvalds89967022005-04-30 20:19:56407}
408
Junio C Hamano38035cf2006-04-05 22:31:12409static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
Linus Torvalds89967022005-04-30 20:19:56410{
Linus Torvalds198b0fb2005-05-01 18:48:34411 if (month > 0 && month < 13 && day > 0 && day < 32) {
Junio C Hamano38035cf2006-04-05 22:31:12412 struct tm check = *tm;
413 struct tm *r = (now_tm ? &check : tm);
414 time_t specified;
Linus Torvalds198b0fb2005-05-01 18:48:34415
Junio C Hamano38035cf2006-04-05 22:31:12416 r->tm_mon = month - 1;
417 r->tm_mday = day;
418 if (year == -1) {
419 if (!now_tm)
420 return 1;
421 r->tm_year = now_tm->tm_year;
422 }
423 else if (year >= 1970 && year < 2100)
424 r->tm_year = year - 1900;
425 else if (year > 70 && year < 100)
426 r->tm_year = year;
427 else if (year < 38)
428 r->tm_year = year + 100;
429 else
430 return 0;
431 if (!now_tm)
432 return 1;
433
Johannes Sixtbb5799d2008-06-23 06:31:41434 specified = tm_to_time_t(r);
Junio C Hamano38035cf2006-04-05 22:31:12435
436 /* Be it commit time or author time, it does not make
437 * sense to specify timestamp way into the future. Make
438 * sure it is not later than ten days from now...
439 */
Mike Gorchake6e87512013-02-25 21:51:16440 if ((specified != -1) && (now + 10*24*3600 < specified))
Junio C Hamano38035cf2006-04-05 22:31:12441 return 0;
442 tm->tm_mon = r->tm_mon;
443 tm->tm_mday = r->tm_mday;
444 if (year != -1)
445 tm->tm_year = r->tm_year;
Linus Torvalds198b0fb2005-05-01 18:48:34446 return 1;
Linus Torvalds89967022005-04-30 20:19:56447 }
Linus Torvalds198b0fb2005-05-01 18:48:34448 return 0;
449}
Linus Torvalds89967022005-04-30 20:19:56450
Johannes Schindelindddbad72017-04-26 19:29:31451static int match_multi_number(timestamp_t num, char c, const char *date,
Jeff King073281e2014-11-13 11:04:52452 char *end, struct tm *tm, time_t now)
Linus Torvalds198b0fb2005-05-01 18:48:34453{
Junio C Hamano38035cf2006-04-05 22:31:12454 struct tm now_tm;
455 struct tm *refuse_future;
Linus Torvalds198b0fb2005-05-01 18:48:34456 long num2, num3;
457
458 num2 = strtol(end+1, &end, 10);
459 num3 = -1;
460 if (*end == c && isdigit(end[1]))
461 num3 = strtol(end+1, &end, 10);
462
463 /* Time? Date? */
Linus Torvalds89967022005-04-30 20:19:56464 switch (c) {
Linus Torvalds198b0fb2005-05-01 18:48:34465 case ':':
466 if (num3 < 0)
467 num3 = 0;
468 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
469 tm->tm_hour = num;
470 tm->tm_min = num2;
471 tm->tm_sec = num3;
Linus Torvalds89967022005-04-30 20:19:56472 break;
473 }
Linus Torvalds198b0fb2005-05-01 18:48:34474 return 0;
Linus Torvalds89967022005-04-30 20:19:56475
476 case '-':
477 case '/':
Junio C Hamano38035cf2006-04-05 22:31:12478 case '.':
Jeff King073281e2014-11-13 11:04:52479 if (!now)
480 now = time(NULL);
Junio C Hamano38035cf2006-04-05 22:31:12481 refuse_future = NULL;
482 if (gmtime_r(&now, &now_tm))
483 refuse_future = &now_tm;
484
Linus Torvalds198b0fb2005-05-01 18:48:34485 if (num > 70) {
486 /* yyyy-mm-dd? */
Jeff Kingd3723952014-11-13 21:43:31487 if (is_date(num, num2, num3, NULL, now, tm))
Linus Torvalds89967022005-04-30 20:19:56488 break;
Linus Torvalds198b0fb2005-05-01 18:48:34489 /* yyyy-dd-mm? */
Jeff Kingd3723952014-11-13 21:43:31490 if (is_date(num, num3, num2, NULL, now, tm))
Linus Torvalds198b0fb2005-05-01 18:48:34491 break;
492 }
Junio C Hamano38035cf2006-04-05 22:31:12493 /* Our eastern European friends say dd.mm.yy[yy]
494 * is the norm there, so giving precedence to
495 * mm/dd/yy[yy] form only when separator is not '.'
496 */
497 if (c != '.' &&
498 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 20:19:56499 break;
Junio C Hamano38035cf2006-04-05 22:31:12500 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
501 if (is_date(num3, num2, num, refuse_future, now, tm))
502 break;
503 /* Funny European mm.dd.yy */
504 if (c == '.' &&
505 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 18:48:34506 break;
507 return 0;
508 }
509 return end - date;
510}
511
Linus Torvalds36e49862009-08-23 01:11:44512/*
513 * Have we filled in any part of the time/date yet?
514 * We just do a binary 'and' to see if the sign bit
515 * is set in all the values.
516 */
Linus Torvalds9f2b6d22008-08-17 04:25:40517static inline int nodate(struct tm *tm)
518{
Linus Torvalds36e49862009-08-23 01:11:44519 return (tm->tm_year &
520 tm->tm_mon &
521 tm->tm_mday &
522 tm->tm_hour &
523 tm->tm_min &
524 tm->tm_sec) < 0;
Linus Torvalds9f2b6d22008-08-17 04:25:40525}
526
Linus Torvalds198b0fb2005-05-01 18:48:34527/*
Junio C Hamanoa6080a02007-06-07 07:04:01528 * We've seen a digit. Time? Year? Date?
Linus Torvalds198b0fb2005-05-01 18:48:34529 */
Linus Torvalds26a2d8a2005-07-12 17:33:06530static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
Linus Torvalds198b0fb2005-05-01 18:48:34531{
532 int n;
533 char *end;
Johannes Schindelindddbad72017-04-26 19:29:31534 timestamp_t num;
Linus Torvalds198b0fb2005-05-01 18:48:34535
Johannes Schindelin1aeb7e72017-04-21 10:45:44536 num = parse_timestamp(date, &end, 10);
Linus Torvalds198b0fb2005-05-01 18:48:34537
538 /*
Johannes Sixta1a5a632007-06-06 08:11:55539 * Seconds since 1970? We trigger on that for any numbers with
540 * more than 8 digits. This is because we don't want to rule out
541 * numbers like 20070606 as a YYYYMMDD date.
Linus Torvalds198b0fb2005-05-01 18:48:34542 */
Linus Torvalds9f2b6d22008-08-17 04:25:40543 if (num >= 100000000 && nodate(tm)) {
Linus Torvalds198b0fb2005-05-01 18:48:34544 time_t time = num;
Junio C Hamano9cb480f2005-06-25 09:21:16545 if (gmtime_r(&time, tm)) {
546 *tm_gmt = 1;
Linus Torvalds198b0fb2005-05-01 18:48:34547 return end - date;
Junio C Hamano9cb480f2005-06-25 09:21:16548 }
Linus Torvalds198b0fb2005-05-01 18:48:34549 }
550
551 /*
Junio C Hamano38035cf2006-04-05 22:31:12552 * Check for special formats: num[-.:/]num[same]num
Linus Torvalds198b0fb2005-05-01 18:48:34553 */
554 switch (*end) {
555 case ':':
Junio C Hamano38035cf2006-04-05 22:31:12556 case '.':
Linus Torvalds198b0fb2005-05-01 18:48:34557 case '/':
558 case '-':
559 if (isdigit(end[1])) {
Jeff King073281e2014-11-13 11:04:52560 int match = match_multi_number(num, *end, date, end, tm, 0);
Linus Torvalds198b0fb2005-05-01 18:48:34561 if (match)
562 return match;
Linus Torvalds89967022005-04-30 20:19:56563 }
564 }
Linus Torvalds198b0fb2005-05-01 18:48:34565
566 /*
567 * None of the special formats? Try to guess what
568 * the number meant. We use the number of digits
569 * to make a more educated guess..
570 */
571 n = 0;
572 do {
573 n++;
574 } while (isdigit(date[n]));
575
576 /* Four-digit year or a timezone? */
577 if (n == 4) {
Paul Eggert7122f822006-06-08 21:54:13578 if (num <= 1400 && *offset == -1) {
Linus Torvalds198b0fb2005-05-01 18:48:34579 unsigned int minutes = num % 100;
580 unsigned int hours = num / 100;
581 *offset = hours*60 + minutes;
582 } else if (num > 1900 && num < 2100)
583 tm->tm_year = num - 1900;
584 return n;
585 }
586
587 /*
Linus Torvalds9f2b6d22008-08-17 04:25:40588 * Ignore lots of numerals. We took care of 4-digit years above.
589 * Days or months must be one or two digits.
590 */
591 if (n > 2)
592 return n;
593
594 /*
Linus Torvalds198b0fb2005-05-01 18:48:34595 * NOTE! We will give precedence to day-of-month over month or
Junio C Hamano82f9d582005-12-29 09:30:08596 * year numbers in the 1-12 range. So 05 is always "mday 5",
Linus Torvalds198b0fb2005-05-01 18:48:34597 * unless we already have a mday..
598 *
599 * IOW, 01 Apr 05 parses as "April 1st, 2005".
600 */
601 if (num > 0 && num < 32 && tm->tm_mday < 0) {
602 tm->tm_mday = num;
603 return n;
604 }
605
606 /* Two-digit year? */
607 if (n == 2 && tm->tm_year < 0) {
608 if (num < 10 && tm->tm_mday >= 0) {
609 tm->tm_year = num + 100;
610 return n;
611 }
612 if (num >= 70) {
613 tm->tm_year = num;
614 return n;
615 }
616 }
617
Linus Torvalds90290552009-08-22 22:10:07618 if (num > 0 && num < 13 && tm->tm_mon < 0)
Linus Torvalds198b0fb2005-05-01 18:48:34619 tm->tm_mon = num-1;
Junio C Hamanoa6080a02007-06-07 07:04:01620
Linus Torvalds198b0fb2005-05-01 18:48:34621 return n;
Linus Torvalds89967022005-04-30 20:19:56622}
623
Linus Torvalds26a2d8a2005-07-12 17:33:06624static int match_tz(const char *date, int *offp)
Linus Torvalds89967022005-04-30 20:19:56625{
626 char *end;
Haitao Liee646eb2011-09-09 10:10:33627 int hour = strtoul(date + 1, &end, 10);
628 int n = end - (date + 1);
629 int min = 0;
Linus Torvalds89967022005-04-30 20:19:56630
Haitao Liee646eb2011-09-09 10:10:33631 if (n == 4) {
632 /* hhmm */
633 min = hour % 100;
634 hour = hour / 100;
635 } else if (n != 2) {
636 min = 99; /* random crap */
637 } else if (*end == ':') {
638 /* hh:mm? */
639 min = strtoul(end + 1, &end, 10);
640 if (end - (date + 1) != 5)
641 min = 99; /* random crap */
642 } /* otherwise we parsed "hh" */
Linus Torvalds89967022005-04-30 20:19:56643
Linus Torvalds198b0fb2005-05-01 18:48:34644 /*
Haitao Liee646eb2011-09-09 10:10:33645 * Don't accept any random crap. Even though some places have
646 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
647 * UTC+14), there is something wrong if hour part is much
648 * larger than that. We might also want to check that the
649 * minutes are divisible by 15 or something too. (Offset of
650 * Kathmandu, Nepal is UTC+5:45)
Linus Torvalds198b0fb2005-05-01 18:48:34651 */
Haitao Liee646eb2011-09-09 10:10:33652 if (min < 60 && hour < 24) {
653 int offset = hour * 60 + min;
Linus Torvalds68849b52005-05-01 19:34:56654 if (*date == '-')
655 offset = -offset;
Linus Torvalds68849b52005-05-01 19:34:56656 *offp = offset;
657 }
Linus Torvalds89967022005-04-30 20:19:56658 return end - date;
659}
660
Johannes Schindelindddbad72017-04-26 19:29:31661static void date_string(timestamp_t date, int offset, struct strbuf *buf)
Linus Torvalds01c6ad22005-09-21 22:50:28662{
663 int sign = '+';
664
665 if (offset < 0) {
666 offset = -offset;
667 sign = '-';
668 }
Johannes Schindelincb71f8b2017-04-21 10:45:48669 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
Linus Torvalds01c6ad22005-09-21 22:50:28670}
671
Junio C Hamano116eb3a2012-02-02 21:41:42672/*
673 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
674 * only when it appears not as part of any other string.
675 */
Johannes Schindelindddbad72017-04-26 19:29:31676static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
Junio C Hamano116eb3a2012-02-02 21:41:42677{
678 char *end;
Johannes Schindelindddbad72017-04-26 19:29:31679 timestamp_t stamp;
Junio C Hamano116eb3a2012-02-02 21:41:42680 int ofs;
681
Junio C Hamanobe21d162012-07-12 20:46:49682 if (*date < '0' || '9' < *date)
Junio C Hamano116eb3a2012-02-02 21:41:42683 return -1;
Johannes Schindelin1aeb7e72017-04-21 10:45:44684 stamp = parse_timestamp(date, &end, 10);
Johannes Schindelindddbad72017-04-26 19:29:31685 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
Junio C Hamano116eb3a2012-02-02 21:41:42686 return -1;
687 date = end + 2;
688 ofs = strtol(date, &end, 10);
689 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
690 return -1;
691 ofs = (ofs / 100) * 60 + (ofs % 100);
692 if (date[-1] == '-')
693 ofs = -ofs;
694 *timestamp = stamp;
695 *offset = ofs;
696 return 0;
697}
698
Edgar Toernigecee9d92005-04-30 16:46:49699/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
700 (i.e. English) day/month names, and it doesn't work correctly with %z. */
Johannes Schindelindddbad72017-04-26 19:29:31701int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
Edgar Toernigecee9d92005-04-30 16:46:49702{
703 struct tm tm;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55704 int tm_gmt;
Johannes Schindelindddbad72017-04-26 19:29:31705 timestamp_t dummy_timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55706 int dummy_offset;
707
708 if (!timestamp)
709 timestamp = &dummy_timestamp;
710 if (!offset)
711 offset = &dummy_offset;
Edgar Toernigecee9d92005-04-30 16:46:49712
713 memset(&tm, 0, sizeof(tm));
Linus Torvalds89967022005-04-30 20:19:56714 tm.tm_year = -1;
715 tm.tm_mon = -1;
716 tm.tm_mday = -1;
Linus Torvaldseaa85122005-04-30 21:25:02717 tm.tm_isdst = -1;
Linus Torvalds36e49862009-08-23 01:11:44718 tm.tm_hour = -1;
719 tm.tm_min = -1;
720 tm.tm_sec = -1;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55721 *offset = -1;
Junio C Hamano9cb480f2005-06-25 09:21:16722 tm_gmt = 0;
Edgar Toernigecee9d92005-04-30 16:46:49723
Junio C Hamano2c733fb2012-02-02 21:41:43724 if (*date == '@' &&
725 !match_object_header_date(date + 1, timestamp, offset))
Junio C Hamano116eb3a2012-02-02 21:41:42726 return 0; /* success */
Linus Torvalds89967022005-04-30 20:19:56727 for (;;) {
728 int match = 0;
729 unsigned char c = *date;
Edgar Toernigecee9d92005-04-30 16:46:49730
Linus Torvalds89967022005-04-30 20:19:56731 /* Stop at end of string or newline */
732 if (!c || c == '\n')
733 break;
Edgar Toernigecee9d92005-04-30 16:46:49734
Linus Torvalds89967022005-04-30 20:19:56735 if (isalpha(c))
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55736 match = match_alpha(date, &tm, offset);
Linus Torvalds89967022005-04-30 20:19:56737 else if (isdigit(c))
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55738 match = match_digit(date, &tm, offset, &tm_gmt);
Linus Torvalds89967022005-04-30 20:19:56739 else if ((c == '-' || c == '+') && isdigit(date[1]))
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55740 match = match_tz(date, offset);
Edgar Toernigecee9d92005-04-30 16:46:49741
Linus Torvalds89967022005-04-30 20:19:56742 if (!match) {
743 /* BAD CRAP */
744 match = 1;
Junio C Hamanoa6080a02007-06-07 07:04:01745 }
Edgar Toernigecee9d92005-04-30 16:46:49746
Linus Torvalds89967022005-04-30 20:19:56747 date += match;
Edgar Toernigecee9d92005-04-30 16:46:49748 }
Edgar Toernigecee9d92005-04-30 16:46:49749
Junio C Hamanof6e63622015-04-15 15:47:48750 /* do not use mktime(), which uses local timezone, here */
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55751 *timestamp = tm_to_time_t(&tm);
Junio C Hamano7fcec482015-04-15 15:43:58752 if (*timestamp == -1)
753 return -1;
754
Mike Gorchake1033da2013-02-25 21:53:40755 if (*offset == -1) {
Junio C Hamanof6e63622015-04-15 15:47:48756 time_t temp_time;
757
758 /* gmtime_r() in match_digit() may have clobbered it */
759 tm.tm_isdst = -1;
760 temp_time = mktime(&tm);
Mike Gorchake1033da2013-02-25 21:53:40761 if ((time_t)*timestamp > temp_time) {
762 *offset = ((time_t)*timestamp - temp_time) / 60;
763 } else {
764 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
765 }
766 }
Linus Torvaldseaa85122005-04-30 21:25:02767
Junio C Hamano9cb480f2005-06-25 09:21:16768 if (!tm_gmt)
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55769 *timestamp -= *offset * 60;
Jonathan Nieder9644c062010-07-15 16:22:57770 return 0; /* success */
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55771}
772
Johannes Schindelindddbad72017-04-26 19:29:31773int parse_expiry_date(const char *date, timestamp_t *timestamp)
Junio C Hamano3d27b9b2013-04-17 22:38:08774{
775 int errors = 0;
776
777 if (!strcmp(date, "never") || !strcmp(date, "false"))
778 *timestamp = 0;
779 else if (!strcmp(date, "all") || !strcmp(date, "now"))
780 /*
781 * We take over "now" here, which usually translates
782 * to the current timestamp. This is because the user
783 * really means to expire everything she has done in
784 * the past, and by definition reflogs are the record
785 * of the past, and there is nothing from the future
786 * to be kept.
787 */
Johannes Schindelindddbad72017-04-26 19:29:31788 *timestamp = TIME_MAX;
Junio C Hamano3d27b9b2013-04-17 22:38:08789 else
790 *timestamp = approxidate_careful(date, &errors);
791
792 return errors;
793}
794
Jeff Kingc33ddc22014-08-27 07:57:08795int parse_date(const char *date, struct strbuf *result)
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55796{
Johannes Schindelindddbad72017-04-26 19:29:31797 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55798 int offset;
Jonathan Nieder9644c062010-07-15 16:22:57799 if (parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 20:28:55800 return -1;
Jeff Kingc33ddc22014-08-27 07:57:08801 date_string(timestamp, offset, result);
802 return 0;
Edgar Toernigecee9d92005-04-30 16:46:49803}
804
Jeff Kingadd00ba2015-09-03 21:48:59805static enum date_mode_type parse_date_type(const char *format, const char **end)
806{
807 if (skip_prefix(format, "relative", end))
808 return DATE_RELATIVE;
809 if (skip_prefix(format, "iso8601-strict", end) ||
810 skip_prefix(format, "iso-strict", end))
811 return DATE_ISO8601_STRICT;
812 if (skip_prefix(format, "iso8601", end) ||
813 skip_prefix(format, "iso", end))
814 return DATE_ISO8601;
815 if (skip_prefix(format, "rfc2822", end) ||
816 skip_prefix(format, "rfc", end))
817 return DATE_RFC2822;
818 if (skip_prefix(format, "short", end))
819 return DATE_SHORT;
820 if (skip_prefix(format, "default", end))
821 return DATE_NORMAL;
822 if (skip_prefix(format, "raw", end))
823 return DATE_RAW;
Jeff King642833d2016-07-22 19:51:49824 if (skip_prefix(format, "unix", end))
825 return DATE_UNIX;
Jeff Kingadd00ba2015-09-03 21:48:59826 if (skip_prefix(format, "format", end))
827 return DATE_STRFTIME;
828
829 die("unknown date format %s", format);
830}
831
Jeff Kinga5481a62015-06-25 16:55:02832void parse_date_format(const char *format, struct date_mode *mode)
Andy Parkins856665f2007-09-28 14:17:31833{
Jeff Kingadd00ba2015-09-03 21:48:59834 const char *p;
835
836 /* historical alias */
837 if (!strcmp(format, "local"))
838 format = "default-local";
839
840 mode->type = parse_date_type(format, &p);
841 mode->local = 0;
842
843 if (skip_prefix(p, "-local", &p))
844 mode->local = 1;
845
846 if (mode->type == DATE_STRFTIME) {
847 if (!skip_prefix(p, ":", &p))
848 die("date format missing colon separator: %s", format);
849 mode->strftime_fmt = xstrdup(p);
850 } else if (*p)
Andy Parkins856665f2007-09-28 14:17:31851 die("unknown date format %s", format);
852}
853
Jeff Kingc33ddc22014-08-27 07:57:08854void datestamp(struct strbuf *out)
Edgar Toernigecee9d92005-04-30 16:46:49855{
856 time_t now;
857 int offset;
858
859 time(&now);
860
Johannes Sixtbb5799d2008-06-23 06:31:41861 offset = tm_to_time_t(localtime(&now)) - now;
Edgar Toernigecee9d92005-04-30 16:46:49862 offset /= 60;
863
Jeff Kingc33ddc22014-08-27 07:57:08864 date_string(now, offset, out);
Edgar Toernigecee9d92005-04-30 16:46:49865}
Linus Torvalds3c07b1d2005-11-15 03:29:06866
Linus Torvalds90290552009-08-22 22:10:07867/*
868 * Relative time update (eg "2 days ago"). If we haven't set the time
869 * yet, we need to set it from current time.
870 */
Johannes Schindelindddbad72017-04-26 19:29:31871static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
Linus Torvalds3c07b1d2005-11-15 03:29:06872{
Linus Torvalds90290552009-08-22 22:10:07873 time_t n;
874
875 if (tm->tm_mday < 0)
876 tm->tm_mday = now->tm_mday;
877 if (tm->tm_mon < 0)
878 tm->tm_mon = now->tm_mon;
879 if (tm->tm_year < 0) {
880 tm->tm_year = now->tm_year;
881 if (tm->tm_mon > now->tm_mon)
882 tm->tm_year--;
883 }
884
885 n = mktime(tm) - sec;
Linus Torvalds3c07b1d2005-11-15 03:29:06886 localtime_r(&n, tm);
Linus Torvalds90290552009-08-22 22:10:07887 return n;
Linus Torvalds3c07b1d2005-11-15 03:29:06888}
889
Jeff Kingc27cc942018-11-02 05:23:09890/*
891 * Do we have a pending number at the end, or when
892 * we see a new one? Let's assume it's a month day,
893 * as in "Dec 6, 1992"
894 */
895static void pending_number(struct tm *tm, int *num)
896{
897 int number = *num;
898
899 if (number) {
900 *num = 0;
901 if (tm->tm_mday < 0 && number < 32)
902 tm->tm_mday = number;
903 else if (tm->tm_mon < 0 && number < 13)
904 tm->tm_mon = number-1;
905 else if (tm->tm_year < 0) {
906 if (number > 1969 && number < 2100)
907 tm->tm_year = number - 1900;
908 else if (number > 69 && number < 100)
909 tm->tm_year = number;
910 else if (number < 38)
911 tm->tm_year = 100 + number;
912 /* We screw up for number = 00 ? */
913 }
914 }
915}
916
Junio C Hamano93cfa7c2010-01-26 19:58:00917static void date_now(struct tm *tm, struct tm *now, int *num)
918{
Jeff Kingc27cc942018-11-02 05:23:09919 *num = 0;
Junio C Hamano93cfa7c2010-01-26 19:58:00920 update_tm(tm, now, 0);
921}
922
Linus Torvalds90290552009-08-22 22:10:07923static void date_yesterday(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 16:56:40924{
Jeff Kingc27cc942018-11-02 05:23:09925 *num = 0;
Linus Torvalds90290552009-08-22 22:10:07926 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 16:56:40927}
928
Linus Torvalds90290552009-08-22 22:10:07929static void date_time(struct tm *tm, struct tm *now, int hour)
Linus Torvaldsa8aca412005-11-18 16:56:40930{
931 if (tm->tm_hour < hour)
Jeff Kingaa097b82018-11-07 01:12:53932 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 16:56:40933 tm->tm_hour = hour;
934 tm->tm_min = 0;
935 tm->tm_sec = 0;
936}
937
Linus Torvalds90290552009-08-22 22:10:07938static void date_midnight(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 16:56:40939{
Jeff Kingc27cc942018-11-02 05:23:09940 pending_number(tm, num);
Linus Torvalds90290552009-08-22 22:10:07941 date_time(tm, now, 0);
Linus Torvaldsa8aca412005-11-18 16:56:40942}
943
Linus Torvalds90290552009-08-22 22:10:07944static void date_noon(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 16:56:40945{
Jeff Kingc27cc942018-11-02 05:23:09946 pending_number(tm, num);
Linus Torvalds90290552009-08-22 22:10:07947 date_time(tm, now, 12);
Linus Torvaldsa8aca412005-11-18 16:56:40948}
949
Linus Torvalds90290552009-08-22 22:10:07950static void date_tea(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 16:56:40951{
Jeff Kingc27cc942018-11-02 05:23:09952 pending_number(tm, num);
Linus Torvalds90290552009-08-22 22:10:07953 date_time(tm, now, 17);
Linus Torvaldsa8aca412005-11-18 16:56:40954}
955
Linus Torvalds90290552009-08-22 22:10:07956static void date_pm(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 19:14:27957{
Linus Torvalds18b633c2006-09-29 19:36:13958 int hour, n = *num;
Linus Torvalds393d3402006-09-28 19:14:27959 *num = 0;
960
Linus Torvalds18b633c2006-09-29 19:36:13961 hour = tm->tm_hour;
962 if (n) {
963 hour = n;
Linus Torvalds393d3402006-09-28 19:14:27964 tm->tm_min = 0;
965 tm->tm_sec = 0;
966 }
Linus Torvalds18b633c2006-09-29 19:36:13967 tm->tm_hour = (hour % 12) + 12;
Linus Torvalds393d3402006-09-28 19:14:27968}
969
Linus Torvalds90290552009-08-22 22:10:07970static void date_am(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 19:14:27971{
Linus Torvalds18b633c2006-09-29 19:36:13972 int hour, n = *num;
Linus Torvalds393d3402006-09-28 19:14:27973 *num = 0;
974
Linus Torvalds18b633c2006-09-29 19:36:13975 hour = tm->tm_hour;
976 if (n) {
977 hour = n;
Linus Torvalds393d3402006-09-28 19:14:27978 tm->tm_min = 0;
979 tm->tm_sec = 0;
980 }
Linus Torvalds18b633c2006-09-29 19:36:13981 tm->tm_hour = (hour % 12);
Linus Torvalds393d3402006-09-28 19:14:27982}
983
Linus Torvalds90290552009-08-22 22:10:07984static void date_never(struct tm *tm, struct tm *now, int *num)
Johannes Schindelinaf663662007-07-24 18:18:34985{
Olivier Marin8c6b5782008-06-17 16:34:57986 time_t n = 0;
987 localtime_r(&n, tm);
Jeff Kingc27cc942018-11-02 05:23:09988 *num = 0;
Johannes Schindelinaf663662007-07-24 18:18:34989}
990
Linus Torvaldsa8aca412005-11-18 16:56:40991static const struct special {
992 const char *name;
Linus Torvalds90290552009-08-22 22:10:07993 void (*fn)(struct tm *, struct tm *, int *);
Linus Torvaldsa8aca412005-11-18 16:56:40994} special[] = {
995 { "yesterday", date_yesterday },
996 { "noon", date_noon },
997 { "midnight", date_midnight },
998 { "tea", date_tea },
Linus Torvalds393d3402006-09-28 19:14:27999 { "PM", date_pm },
1000 { "AM", date_am },
Johannes Schindelinaf663662007-07-24 18:18:341001 { "never", date_never },
Junio C Hamano93cfa7c2010-01-26 19:58:001002 { "now", date_now },
Linus Torvaldsa8aca412005-11-18 16:56:401003 { NULL }
1004};
1005
Linus Torvalds3c07b1d2005-11-15 03:29:061006static const char *number_name[] = {
1007 "zero", "one", "two", "three", "four",
1008 "five", "six", "seven", "eight", "nine", "ten",
1009};
1010
Linus Torvaldsa8aca412005-11-18 16:56:401011static const struct typelen {
Linus Torvalds3c07b1d2005-11-15 03:29:061012 const char *type;
1013 int length;
1014} typelen[] = {
1015 { "seconds", 1 },
1016 { "minutes", 60 },
1017 { "hours", 60*60 },
1018 { "days", 24*60*60 },
1019 { "weeks", 7*24*60*60 },
1020 { NULL }
Junio C Hamanoa6080a02007-06-07 07:04:011021};
Linus Torvalds3c07b1d2005-11-15 03:29:061022
Junio C Hamano93cfa7c2010-01-26 19:58:001023static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
Linus Torvalds3c07b1d2005-11-15 03:29:061024{
Linus Torvaldsa8aca412005-11-18 16:56:401025 const struct typelen *tl;
1026 const struct special *s;
Linus Torvalds3c07b1d2005-11-15 03:29:061027 const char *end = date;
Pierre Habouzit5df7dbb2006-08-23 10:39:161028 int i;
Linus Torvalds3c07b1d2005-11-15 03:29:061029
Jeff King38db01b2013-10-24 08:42:171030 while (isalpha(*++end))
Pierre Habouzit5df7dbb2006-08-23 10:39:161031 ;
Linus Torvalds3c07b1d2005-11-15 03:29:061032
1033 for (i = 0; i < 12; i++) {
1034 int match = match_string(date, month_names[i]);
1035 if (match >= 3) {
1036 tm->tm_mon = i;
Junio C Hamano93cfa7c2010-01-26 19:58:001037 *touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061038 return end;
1039 }
1040 }
1041
Linus Torvaldsa8aca412005-11-18 16:56:401042 for (s = special; s->name; s++) {
1043 int len = strlen(s->name);
1044 if (match_string(date, s->name) == len) {
Linus Torvalds90290552009-08-22 22:10:071045 s->fn(tm, now, num);
Junio C Hamano93cfa7c2010-01-26 19:58:001046 *touched = 1;
Linus Torvaldsa8aca412005-11-18 16:56:401047 return end;
1048 }
Linus Torvalds3c07b1d2005-11-15 03:29:061049 }
1050
1051 if (!*num) {
1052 for (i = 1; i < 11; i++) {
1053 int len = strlen(number_name[i]);
1054 if (match_string(date, number_name[i]) == len) {
1055 *num = i;
Junio C Hamano93cfa7c2010-01-26 19:58:001056 *touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061057 return end;
1058 }
1059 }
Junio C Hamano93cfa7c2010-01-26 19:58:001060 if (match_string(date, "last") == 4) {
Linus Torvalds3c07b1d2005-11-15 03:29:061061 *num = 1;
Junio C Hamano93cfa7c2010-01-26 19:58:001062 *touched = 1;
1063 }
Linus Torvalds3c07b1d2005-11-15 03:29:061064 return end;
1065 }
1066
1067 tl = typelen;
1068 while (tl->type) {
1069 int len = strlen(tl->type);
1070 if (match_string(date, tl->type) >= len-1) {
Linus Torvalds90290552009-08-22 22:10:071071 update_tm(tm, now, tl->length * *num);
Linus Torvalds3c07b1d2005-11-15 03:29:061072 *num = 0;
Junio C Hamano93cfa7c2010-01-26 19:58:001073 *touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061074 return end;
1075 }
1076 tl++;
1077 }
1078
Linus Torvalds6b7b0422005-11-17 20:36:301079 for (i = 0; i < 7; i++) {
1080 int match = match_string(date, weekday_names[i]);
1081 if (match >= 3) {
1082 int diff, n = *num -1;
1083 *num = 0;
1084
1085 diff = tm->tm_wday - i;
1086 if (diff <= 0)
1087 n++;
1088 diff += 7*n;
1089
Linus Torvalds90290552009-08-22 22:10:071090 update_tm(tm, now, diff * 24 * 60 * 60);
Junio C Hamano93cfa7c2010-01-26 19:58:001091 *touched = 1;
Linus Torvalds6b7b0422005-11-17 20:36:301092 return end;
1093 }
1094 }
1095
Linus Torvalds3c07b1d2005-11-15 03:29:061096 if (match_string(date, "months") >= 5) {
Jeff King931e8e22009-08-31 02:31:421097 int n;
1098 update_tm(tm, now, 0); /* fill in date fields if needed */
1099 n = tm->tm_mon - *num;
Linus Torvalds3c07b1d2005-11-15 03:29:061100 *num = 0;
1101 while (n < 0) {
1102 n += 12;
1103 tm->tm_year--;
1104 }
1105 tm->tm_mon = n;
Junio C Hamano93cfa7c2010-01-26 19:58:001106 *touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061107 return end;
1108 }
1109
1110 if (match_string(date, "years") >= 4) {
Jeff King931e8e22009-08-31 02:31:421111 update_tm(tm, now, 0); /* fill in date fields if needed */
Linus Torvalds3c07b1d2005-11-15 03:29:061112 tm->tm_year -= *num;
1113 *num = 0;
Junio C Hamano93cfa7c2010-01-26 19:58:001114 *touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061115 return end;
1116 }
1117
1118 return end;
1119}
1120
Jeff King073281e2014-11-13 11:04:521121static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1122 time_t now)
Linus Torvaldse92a54d2006-09-28 19:12:281123{
1124 char *end;
Johannes Schindelindddbad72017-04-26 19:29:311125 timestamp_t number = parse_timestamp(date, &end, 10);
Linus Torvaldse92a54d2006-09-28 19:12:281126
Linus Torvalds393d3402006-09-28 19:14:271127 switch (*end) {
1128 case ':':
1129 case '.':
1130 case '/':
1131 case '-':
1132 if (isdigit(end[1])) {
Jeff King073281e2014-11-13 11:04:521133 int match = match_multi_number(number, *end, date, end,
1134 tm, now);
Linus Torvalds393d3402006-09-28 19:14:271135 if (match)
1136 return date + match;
1137 }
1138 }
1139
Linus Torvalds9f2b6d22008-08-17 04:25:401140 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1141 if (date[0] != '0' || end - date <= 2)
1142 *num = number;
Linus Torvaldse92a54d2006-09-28 19:12:281143 return end;
1144}
1145
Johannes Schindelindddbad72017-04-26 19:29:311146static timestamp_t approxidate_str(const char *date,
1147 const struct timeval *tv,
1148 int *error_ret)
Linus Torvalds3c07b1d2005-11-15 03:29:061149{
1150 int number = 0;
Junio C Hamano93cfa7c2010-01-26 19:58:001151 int touched = 0;
Linus Torvalds3c07b1d2005-11-15 03:29:061152 struct tm tm, now;
Bernd Ahlersf697b332009-04-06 17:26:371153 time_t time_sec;
Linus Torvalds3c07b1d2005-11-15 03:29:061154
Alex Riesen33012fc2009-08-31 02:26:051155 time_sec = tv->tv_sec;
Bernd Ahlersf697b332009-04-06 17:26:371156 localtime_r(&time_sec, &tm);
Linus Torvalds3c07b1d2005-11-15 03:29:061157 now = tm;
Linus Torvalds90290552009-08-22 22:10:071158
1159 tm.tm_year = -1;
1160 tm.tm_mon = -1;
1161 tm.tm_mday = -1;
1162
Linus Torvalds3c07b1d2005-11-15 03:29:061163 for (;;) {
1164 unsigned char c = *date;
1165 if (!c)
1166 break;
1167 date++;
1168 if (isdigit(c)) {
Linus Torvalds90290552009-08-22 22:10:071169 pending_number(&tm, &number);
Jeff King073281e2014-11-13 11:04:521170 date = approxidate_digit(date-1, &tm, &number, time_sec);
Junio C Hamano93cfa7c2010-01-26 19:58:001171 touched = 1;
Linus Torvalds3c07b1d2005-11-15 03:29:061172 continue;
1173 }
1174 if (isalpha(c))
Junio C Hamano93cfa7c2010-01-26 19:58:001175 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
Linus Torvalds3c07b1d2005-11-15 03:29:061176 }
Linus Torvalds90290552009-08-22 22:10:071177 pending_number(&tm, &number);
Junio C Hamano93cfa7c2010-01-26 19:58:001178 if (!touched)
1179 *error_ret = 1;
Johannes Schindelindddbad72017-04-26 19:29:311180 return (timestamp_t)update_tm(&tm, &now, 0);
Linus Torvalds3c07b1d2005-11-15 03:29:061181}
Alex Riesen33012fc2009-08-31 02:26:051182
Johannes Schindelindddbad72017-04-26 19:29:311183timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
Alex Riesen33012fc2009-08-31 02:26:051184{
Johannes Schindelindddbad72017-04-26 19:29:311185 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:551186 int offset;
Junio C Hamano93cfa7c2010-01-26 19:58:001187 int errors = 0;
Alex Riesen33012fc2009-08-31 02:26:051188
Jonathan Nieder9644c062010-07-15 16:22:571189 if (!parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 20:28:551190 return timestamp;
Junio C Hamano93cfa7c2010-01-26 19:58:001191 return approxidate_str(date, tv, &errors);
Alex Riesen33012fc2009-08-31 02:26:051192}
1193
Johannes Schindelindddbad72017-04-26 19:29:311194timestamp_t approxidate_careful(const char *date, int *error_ret)
Alex Riesen33012fc2009-08-31 02:26:051195{
1196 struct timeval tv;
Johannes Schindelindddbad72017-04-26 19:29:311197 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:551198 int offset;
Junio C Hamano93cfa7c2010-01-26 19:58:001199 int dummy = 0;
1200 if (!error_ret)
1201 error_ret = &dummy;
Alex Riesen33012fc2009-08-31 02:26:051202
Jonathan Nieder9644c062010-07-15 16:22:571203 if (!parse_date_basic(date, &timestamp, &offset)) {
Junio C Hamano93cfa7c2010-01-26 19:58:001204 *error_ret = 0;
Ramkumar Ramachandrac5043cc2010-06-03 20:28:551205 return timestamp;
Junio C Hamano93cfa7c2010-01-26 19:58:001206 }
Alex Riesen33012fc2009-08-31 02:26:051207
1208 gettimeofday(&tv, NULL);
Junio C Hamano93cfa7c2010-01-26 19:58:001209 return approxidate_str(date, &tv, error_ret);
Alex Riesen33012fc2009-08-31 02:26:051210}
Jeff King7ca36d92014-02-24 07:39:451211
Johannes Schindelindddbad72017-04-26 19:29:311212int date_overflows(timestamp_t t)
Jeff King7ca36d92014-02-24 07:39:451213{
1214 time_t sys;
1215
Johannes Schindelindddbad72017-04-26 19:29:311216 /* If we overflowed our timestamp data type, that's bad... */
1217 if ((uintmax_t)t >= TIME_MAX)
Jeff King7ca36d92014-02-24 07:39:451218 return 1;
1219
1220 /*
1221 * ...but we also are going to feed the result to system
1222 * functions that expect time_t, which is often "signed long".
1223 * Make sure that we fit into time_t, as well.
1224 */
1225 sys = t;
1226 return t != sys || (t < 1) != (sys < 1);
1227}