blob: 9c5870e102951ed25f5def2103dab15bf8ed5c99 [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
Johannes Schindelindddbad72017-04-26 21:29:31 +020042static time_t gm_time_t(timestamp_t time, int tz)
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070043{
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 21:29:36 +020049
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 15:45:26 -070060}
61
Linus Torvalds89967022005-04-30 13:19:56 -070062/*
Linus Torvaldsf80cd782005-05-06 15:28:59 -070063 * 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 21:29:31 +020067static struct tm *time_to_tm(timestamp_t time, int tz)
Linus Torvaldsf80cd782005-05-06 15:28:59 -070068{
Linus Torvalds9a8e35e2006-08-26 15:45:26 -070069 time_t t = gm_time_t(time, tz);
Junio C Hamano2a387042006-05-01 01:44:33 -070070 return gmtime(&t);
71}
72
Junio C Hamano9eafe862017-06-22 14:15:25 -070073static struct tm *time_to_tm_local(timestamp_t time)
Jeff King6eced3e2017-06-15 09:52:17 -040074{
75 time_t t = time;
76 return localtime(&t);
77}
78
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070079/*
Linus Torvaldsacdd3772019-01-17 23:18:01 -070080 * Fill in the localtime 'struct tm' for the supplied time,
81 * and return the local tz.
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070082 */
Linus Torvaldsacdd3772019-01-17 23:18:01 -070083static int local_time_tzoffset(time_t t, struct tm *tm)
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070084{
Linus Torvaldsacdd3772019-01-17 23:18:01 -070085 time_t t_local;
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070086 int offset, eastwest;
87
Linus Torvaldsacdd3772019-01-17 23:18:01 -070088 localtime_r(&t, tm);
89 t_local = tm_to_time_t(tm);
Jeff Kingbab74832016-06-20 17:14:14 -040090 if (t_local == -1)
91 return 0; /* error; just use +0000 */
Junio C Hamanoa7b02cc2007-04-24 23:36:22 -070092 if (t_local < t) {
93 eastwest = -1;
94 offset = t - t_local;
95 } else {
96 eastwest = 1;
97 offset = t_local - t;
98 }
99 offset /= 60; /* in minutes */
100 offset = (offset % 60) + ((offset / 60) * 100);
101 return offset * eastwest;
102}
103
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700104/*
105 * What value of "tz" was in effect back then at "time" in the
106 * local timezone?
107 */
108static int local_tzoffset(timestamp_t time)
109{
110 struct tm tm;
111
112 if (date_overflows(time))
113 die("Timestamp too large for this system: %"PRItime, time);
114
115 return local_time_tzoffset((time_t)time, &tm);
116}
117
Stephen P. Smithb841d4f2019-01-28 20:50:15 -0700118static void get_time(struct timeval *now)
119{
120 const char *x;
121
122 x = getenv("GIT_TEST_DATE_NOW");
123 if (x) {
124 now->tv_sec = atoi(x);
125 now->tv_usec = 0;
126 }
127 else
128 gettimeofday(now, NULL);
129}
130
Jeff King3d420342019-01-24 08:12:21 -0500131void show_date_relative(timestamp_t time,
132 const struct timeval *now,
133 struct strbuf *timebuf)
Alex Riesen33012fc2009-08-30 22:26:05 -0400134{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200135 timestamp_t diff;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700136 if (now->tv_sec < time) {
137 strbuf_addstr(timebuf, _("in the future"));
138 return;
139 }
Alex Riesen33012fc2009-08-30 22:26:05 -0400140 diff = now->tv_sec - time;
141 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700142 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200143 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700144 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400145 }
146 /* Turn it into minutes */
147 diff = (diff + 30) / 60;
148 if (diff < 90) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700149 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200150 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700151 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400152 }
153 /* Turn it into hours */
154 diff = (diff + 30) / 60;
155 if (diff < 36) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700156 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200157 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700158 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400159 }
160 /* We deal with number of days from here on */
161 diff = (diff + 12) / 24;
162 if (diff < 14) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700163 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200164 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700165 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400166 }
167 /* Say weeks for the past 10 weeks or so */
168 if (diff < 70) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700169 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200170 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700171 (diff + 3) / 7);
172 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400173 }
174 /* Say months for the past 12 months or so */
Johan Sageryddbc1b1f2009-10-03 13:20:18 +0900175 if (diff < 365) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700176 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200177 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700178 (diff + 15) / 30);
179 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400180 }
181 /* Give years and months for 5 years or so */
182 if (diff < 1825) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200183 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
184 timestamp_t years = totalmonths / 12;
185 timestamp_t months = totalmonths % 12;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700186 if (months) {
187 struct strbuf sb = STRBUF_INIT;
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200188 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700189 strbuf_addf(timebuf,
Jiang Xinfcaed042014-04-17 13:37:17 +0800190 /* TRANSLATORS: "%s" is "<n> years" */
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200191 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700192 sb.buf, months);
193 strbuf_release(&sb);
194 } else
195 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200196 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700197 return;
Alex Riesen33012fc2009-08-30 22:26:05 -0400198 }
199 /* Otherwise, just years. Centuries is probably overkill. */
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700200 strbuf_addf(timebuf,
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200201 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700202 (diff + 183) / 365);
Alex Riesen33012fc2009-08-30 22:26:05 -0400203}
204
Jeff Kinga5481a62015-06-25 12:55:02 -0400205struct date_mode *date_mode_from_type(enum date_mode_type type)
206{
207 static struct date_mode mode;
Jeff Kingaa1462c2015-06-25 12:55:45 -0400208 if (type == DATE_STRFTIME)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200209 BUG("cannot create anonymous strftime date_mode struct");
Jeff Kinga5481a62015-06-25 12:55:02 -0400210 mode.type = type;
Jeff Kingadd00ba2015-09-03 22:48:59 +0100211 mode.local = 0;
Jeff Kinga5481a62015-06-25 12:55:02 -0400212 return &mode;
213}
214
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700215static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)
216{
217 struct {
218 unsigned int year:1,
219 date:1,
220 wday:1,
221 time:1,
222 seconds:1,
223 tz:1;
224 } hide = { 0 };
225
226 hide.tz = local || tz == human_tz;
227 hide.year = tm->tm_year == human_tm->tm_year;
228 if (hide.year) {
229 if (tm->tm_mon == human_tm->tm_mon) {
230 if (tm->tm_mday > human_tm->tm_mday) {
231 /* Future date: think timezones */
232 } else if (tm->tm_mday == human_tm->tm_mday) {
233 hide.date = hide.wday = 1;
234 } else if (tm->tm_mday + 5 > human_tm->tm_mday) {
235 /* Leave just weekday if it was a few days ago */
236 hide.date = 1;
237 }
238 }
239 }
240
241 /* Show "today" times as just relative times */
242 if (hide.wday) {
243 struct timeval now;
Stephen P. Smithb841d4f2019-01-28 20:50:15 -0700244 get_time(&now);
Junio C Hamanoecbe1be2019-02-06 22:05:24 -0800245 show_date_relative(time, &now, buf);
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700246 return;
247 }
248
249 /*
250 * Always hide seconds for human-readable.
251 * Hide timezone if showing date.
252 * Hide weekday and time if showing year.
253 *
254 * The logic here is two-fold:
255 * (a) only show details when recent enough to matter
256 * (b) keep the maximum length "similar", and in check
257 */
258 if (human_tm->tm_year) {
259 hide.seconds = 1;
260 hide.tz |= !hide.date;
261 hide.wday = hide.time = !hide.year;
262 }
263
264 if (!hide.wday)
265 strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);
266 if (!hide.date)
267 strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);
268
269 /* Do we want AM/PM depending on locale? */
270 if (!hide.time) {
271 strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);
272 if (!hide.seconds)
273 strbuf_addf(buf, ":%02d", tm->tm_sec);
274 } else
275 strbuf_rtrim(buf);
276
277 if (!hide.year)
278 strbuf_addf(buf, " %d", tm->tm_year + 1900);
279
280 if (!hide.tz)
281 strbuf_addf(buf, " %+05d", tz);
282}
283
Johannes Schindelindddbad72017-04-26 21:29:31 +0200284const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
Junio C Hamano2a387042006-05-01 01:44:33 -0700285{
286 struct tm *tm;
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700287 struct tm human_tm = { 0 };
288 int human_tz = -1;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700289 static struct strbuf timebuf = STRBUF_INIT;
Junio C Hamano2a387042006-05-01 01:44:33 -0700290
Jeff King642833d2016-07-22 15:51:49 -0400291 if (mode->type == DATE_UNIX) {
292 strbuf_reset(&timebuf);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200293 strbuf_addf(&timebuf, "%"PRItime, time);
Jeff King642833d2016-07-22 15:51:49 -0400294 return timebuf.buf;
295 }
296
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700297 if (mode->type == DATE_HUMAN) {
298 struct timeval now;
299
Stephen P. Smithb841d4f2019-01-28 20:50:15 -0700300 get_time(&now);
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700301
302 /* Fill in the data for "current time" in human_tz and human_tm */
303 human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
304 }
305
Jeff Kingadd00ba2015-09-03 22:48:59 +0100306 if (mode->local)
John Keepingdc6d7822015-09-03 22:48:58 +0100307 tz = local_tzoffset(time);
308
Jeff Kinga5481a62015-06-25 12:55:02 -0400309 if (mode->type == DATE_RAW) {
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700310 strbuf_reset(&timebuf);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200311 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700312 return timebuf.buf;
Linus Torvalds7dff9b32009-02-20 14:15:22 -0800313 }
314
Jeff Kinga5481a62015-06-25 12:55:02 -0400315 if (mode->type == DATE_RELATIVE) {
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700316 struct timeval now;
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700317
318 strbuf_reset(&timebuf);
Stephen P. Smithb841d4f2019-01-28 20:50:15 -0700319 get_time(&now);
Jeff King3d420342019-01-24 08:12:21 -0500320 show_date_relative(time, &now, &timebuf);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700321 return timebuf.buf;
Linus Torvalds9a8e35e2006-08-26 15:45:26 -0700322 }
323
Jeff King6eced3e2017-06-15 09:52:17 -0400324 if (mode->local)
325 tm = time_to_tm_local(time);
326 else
327 tm = time_to_tm(time, tz);
Jeff King2b158462014-02-24 02:49:05 -0500328 if (!tm) {
329 tm = time_to_tm(0, 0);
330 tz = 0;
331 }
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700332
333 strbuf_reset(&timebuf);
Jeff Kinga5481a62015-06-25 12:55:02 -0400334 if (mode->type == DATE_SHORT)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700335 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100336 tm->tm_mon + 1, tm->tm_mday);
Jeff Kinga5481a62015-06-25 12:55:02 -0400337 else if (mode->type == DATE_ISO8601)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700338 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
Robin Rosenbergee8f8382007-07-14 01:00:42 +0200339 tm->tm_year + 1900,
340 tm->tm_mon + 1,
341 tm->tm_mday,
342 tm->tm_hour, tm->tm_min, tm->tm_sec,
343 tz);
Jeff Kinga5481a62015-06-25 12:55:02 -0400344 else if (mode->type == DATE_ISO8601_STRICT) {
Beat Bolli466fb672014-08-29 18:58:42 +0200345 char sign = (tz >= 0) ? '+' : '-';
346 tz = abs(tz);
347 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
348 tm->tm_year + 1900,
349 tm->tm_mon + 1,
350 tm->tm_mday,
351 tm->tm_hour, tm->tm_min, tm->tm_sec,
352 sign, tz / 100, tz % 100);
Jeff Kinga5481a62015-06-25 12:55:02 -0400353 } else if (mode->type == DATE_RFC2822)
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700354 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
Junio C Hamano73013af2007-07-13 23:14:52 -0700355 weekday_names[tm->tm_wday], tm->tm_mday,
356 month_names[tm->tm_mon], tm->tm_year + 1900,
357 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
Jeff Kingaa1462c2015-06-25 12:55:45 -0400358 else if (mode->type == DATE_STRFTIME)
Jeff King6eced3e2017-06-15 09:52:17 -0400359 strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
Ævar Arnfjörð Bjarmason3b702232017-07-01 13:15:47 +0000360 !mode->local);
Johannes Schindelinf8493ec2007-02-27 16:21:04 +0100361 else
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700362 show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
Jonathan Nieder7d29afd2012-04-23 19:30:23 +0700363 return timebuf.buf;
Linus Torvaldsf80cd782005-05-06 15:28:59 -0700364}
365
366/*
Linus Torvalds89967022005-04-30 13:19:56 -0700367 * Check these. And note how it doesn't do the summer-time conversion.
368 *
369 * In my world, it's always summer, and things are probably a bit off
370 * in other ways too.
371 */
372static const struct {
373 const char *name;
374 int offset;
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700375 int dst;
Linus Torvalds89967022005-04-30 13:19:56 -0700376} timezone_names[] = {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700377 { "IDLW", -12, 0, }, /* International Date Line West */
378 { "NT", -11, 0, }, /* Nome */
379 { "CAT", -10, 0, }, /* Central Alaska */
380 { "HST", -10, 0, }, /* Hawaii Standard */
381 { "HDT", -10, 1, }, /* Hawaii Daylight */
382 { "YST", -9, 0, }, /* Yukon Standard */
383 { "YDT", -9, 1, }, /* Yukon Daylight */
384 { "PST", -8, 0, }, /* Pacific Standard */
385 { "PDT", -8, 1, }, /* Pacific Daylight */
386 { "MST", -7, 0, }, /* Mountain Standard */
387 { "MDT", -7, 1, }, /* Mountain Daylight */
388 { "CST", -6, 0, }, /* Central Standard */
389 { "CDT", -6, 1, }, /* Central Daylight */
390 { "EST", -5, 0, }, /* Eastern Standard */
391 { "EDT", -5, 1, }, /* Eastern Daylight */
392 { "AST", -3, 0, }, /* Atlantic Standard */
393 { "ADT", -3, 1, }, /* Atlantic Daylight */
394 { "WAT", -1, 0, }, /* West Africa */
Edgar Toernigecee9d92005-04-30 09:46:49 -0700395
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700396 { "GMT", 0, 0, }, /* Greenwich Mean */
397 { "UTC", 0, 0, }, /* Universal (Coordinated) */
Marcus Comstedt75b37e72010-05-17 21:07:10 +0200398 { "Z", 0, 0, }, /* Zulu, alias for UTC */
Linus Torvalds89967022005-04-30 13:19:56 -0700399
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700400 { "WET", 0, 0, }, /* Western European */
401 { "BST", 0, 1, }, /* British Summer */
402 { "CET", +1, 0, }, /* Central European */
403 { "MET", +1, 0, }, /* Middle European */
404 { "MEWT", +1, 0, }, /* Middle European Winter */
405 { "MEST", +1, 1, }, /* Middle European Summer */
406 { "CEST", +1, 1, }, /* Central European Summer */
407 { "MESZ", +1, 1, }, /* Middle European Summer */
408 { "FWT", +1, 0, }, /* French Winter */
409 { "FST", +1, 1, }, /* French Summer */
410 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
Linus Torvalds92e23112005-04-30 15:21:57 -0700411 { "EEST", +2, 1, }, /* Eastern European Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700412 { "WAST", +7, 0, }, /* West Australian Standard */
413 { "WADT", +7, 1, }, /* West Australian Daylight */
414 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
415 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
416 { "EAST", +10, 0, }, /* Eastern Australian Standard */
417 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
418 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
Steven Drake695ed472008-02-26 13:45:53 +1300419 { "NZT", +12, 0, }, /* New Zealand */
420 { "NZST", +12, 0, }, /* New Zealand Standard */
421 { "NZDT", +12, 1, }, /* New Zealand Daylight */
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700422 { "IDLE", +12, 0, }, /* International Date Line East */
Linus Torvalds89967022005-04-30 13:19:56 -0700423};
424
Linus Torvalds89967022005-04-30 13:19:56 -0700425static int match_string(const char *date, const char *str)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700426{
Linus Torvalds89967022005-04-30 13:19:56 -0700427 int i = 0;
428
429 for (i = 0; *date; date++, str++, i++) {
430 if (*date == *str)
431 continue;
432 if (toupper(*date) == toupper(*str))
433 continue;
434 if (!isalnum(*date))
435 break;
436 return 0;
437 }
438 return i;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700439}
440
Linus Torvalds68849b52005-05-01 12:34:56 -0700441static int skip_alpha(const char *date)
442{
443 int i = 0;
444 do {
445 i++;
446 } while (isalpha(date[i]));
447 return i;
448}
449
Linus Torvalds89967022005-04-30 13:19:56 -0700450/*
451* Parse month, weekday, or timezone name
452*/
453static int match_alpha(const char *date, struct tm *tm, int *offset)
454{
455 int i;
456
457 for (i = 0; i < 12; i++) {
458 int match = match_string(date, month_names[i]);
459 if (match >= 3) {
460 tm->tm_mon = i;
461 return match;
462 }
463 }
464
465 for (i = 0; i < 7; i++) {
466 int match = match_string(date, weekday_names[i]);
467 if (match >= 3) {
468 tm->tm_wday = i;
469 return match;
470 }
471 }
472
Junio C Hamanob4f2a6a2006-03-09 11:58:05 -0800473 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
Linus Torvalds89967022005-04-30 13:19:56 -0700474 int match = match_string(date, timezone_names[i].name);
Marcus Comstedt75b37e72010-05-17 21:07:10 +0200475 if (match >= 3 || match == strlen(timezone_names[i].name)) {
Linus Torvalds5e2a78a2005-04-30 14:53:12 -0700476 int off = timezone_names[i].offset;
477
478 /* This is bogus, but we like summer */
479 off += timezone_names[i].dst;
480
Linus Torvalds92e23112005-04-30 15:21:57 -0700481 /* Only use the tz name offset if we don't have anything better */
482 if (*offset == -1)
483 *offset = 60*off;
484
Linus Torvalds89967022005-04-30 13:19:56 -0700485 return match;
486 }
487 }
488
Linus Torvalds68849b52005-05-01 12:34:56 -0700489 if (match_string(date, "PM") == 2) {
Linus Torvalds18b633c2006-09-29 12:36:13 -0700490 tm->tm_hour = (tm->tm_hour % 12) + 12;
491 return 2;
492 }
493
494 if (match_string(date, "AM") == 2) {
495 tm->tm_hour = (tm->tm_hour % 12) + 0;
Linus Torvalds68849b52005-05-01 12:34:56 -0700496 return 2;
497 }
498
Linus Torvalds89967022005-04-30 13:19:56 -0700499 /* BAD CRAP */
Linus Torvalds68849b52005-05-01 12:34:56 -0700500 return skip_alpha(date);
Linus Torvalds89967022005-04-30 13:19:56 -0700501}
502
Junio C Hamano38035cf2006-04-05 15:31:12 -0700503static 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 -0700504{
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700505 if (month > 0 && month < 13 && day > 0 && day < 32) {
Junio C Hamano38035cf2006-04-05 15:31:12 -0700506 struct tm check = *tm;
507 struct tm *r = (now_tm ? &check : tm);
508 time_t specified;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700509
Junio C Hamano38035cf2006-04-05 15:31:12 -0700510 r->tm_mon = month - 1;
511 r->tm_mday = day;
512 if (year == -1) {
513 if (!now_tm)
514 return 1;
515 r->tm_year = now_tm->tm_year;
516 }
517 else if (year >= 1970 && year < 2100)
518 r->tm_year = year - 1900;
519 else if (year > 70 && year < 100)
520 r->tm_year = year;
521 else if (year < 38)
522 r->tm_year = year + 100;
523 else
524 return 0;
525 if (!now_tm)
526 return 1;
527
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200528 specified = tm_to_time_t(r);
Junio C Hamano38035cf2006-04-05 15:31:12 -0700529
530 /* Be it commit time or author time, it does not make
531 * sense to specify timestamp way into the future. Make
532 * sure it is not later than ten days from now...
533 */
Mike Gorchake6e87512013-02-25 23:51:16 +0200534 if ((specified != -1) && (now + 10*24*3600 < specified))
Junio C Hamano38035cf2006-04-05 15:31:12 -0700535 return 0;
536 tm->tm_mon = r->tm_mon;
537 tm->tm_mday = r->tm_mday;
538 if (year != -1)
539 tm->tm_year = r->tm_year;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700540 return 1;
Linus Torvalds89967022005-04-30 13:19:56 -0700541 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700542 return 0;
543}
Linus Torvalds89967022005-04-30 13:19:56 -0700544
Johannes Schindelindddbad72017-04-26 21:29:31 +0200545static int match_multi_number(timestamp_t num, char c, const char *date,
Jeff King073281e2014-11-13 06:04:52 -0500546 char *end, struct tm *tm, time_t now)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700547{
Junio C Hamano38035cf2006-04-05 15:31:12 -0700548 struct tm now_tm;
549 struct tm *refuse_future;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700550 long num2, num3;
551
552 num2 = strtol(end+1, &end, 10);
553 num3 = -1;
554 if (*end == c && isdigit(end[1]))
555 num3 = strtol(end+1, &end, 10);
556
557 /* Time? Date? */
Linus Torvalds89967022005-04-30 13:19:56 -0700558 switch (c) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700559 case ':':
560 if (num3 < 0)
561 num3 = 0;
562 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
563 tm->tm_hour = num;
564 tm->tm_min = num2;
565 tm->tm_sec = num3;
Linus Torvalds89967022005-04-30 13:19:56 -0700566 break;
567 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700568 return 0;
Linus Torvalds89967022005-04-30 13:19:56 -0700569
570 case '-':
571 case '/':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700572 case '.':
Jeff King073281e2014-11-13 06:04:52 -0500573 if (!now)
574 now = time(NULL);
Junio C Hamano38035cf2006-04-05 15:31:12 -0700575 refuse_future = NULL;
576 if (gmtime_r(&now, &now_tm))
577 refuse_future = &now_tm;
578
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700579 if (num > 70) {
580 /* yyyy-mm-dd? */
Jeff Kingd3723952014-11-13 16:43:31 -0500581 if (is_date(num, num2, num3, NULL, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700582 break;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700583 /* yyyy-dd-mm? */
Jeff Kingd3723952014-11-13 16:43:31 -0500584 if (is_date(num, num3, num2, NULL, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700585 break;
586 }
Junio C Hamano38035cf2006-04-05 15:31:12 -0700587 /* Our eastern European friends say dd.mm.yy[yy]
588 * is the norm there, so giving precedence to
589 * mm/dd/yy[yy] form only when separator is not '.'
590 */
591 if (c != '.' &&
592 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds89967022005-04-30 13:19:56 -0700593 break;
Junio C Hamano38035cf2006-04-05 15:31:12 -0700594 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
595 if (is_date(num3, num2, num, refuse_future, now, tm))
596 break;
597 /* Funny European mm.dd.yy */
598 if (c == '.' &&
599 is_date(num3, num, num2, refuse_future, now, tm))
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700600 break;
601 return 0;
602 }
603 return end - date;
604}
605
Linus Torvalds36e49862009-08-22 18:11:44 -0700606/*
607 * Have we filled in any part of the time/date yet?
608 * We just do a binary 'and' to see if the sign bit
609 * is set in all the values.
610 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700611static inline int nodate(struct tm *tm)
612{
Linus Torvalds36e49862009-08-22 18:11:44 -0700613 return (tm->tm_year &
614 tm->tm_mon &
615 tm->tm_mday &
616 tm->tm_hour &
617 tm->tm_min &
618 tm->tm_sec) < 0;
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700619}
620
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700621/*
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700622 * We've seen a digit. Time? Year? Date?
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700623 */
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700624static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700625{
626 int n;
627 char *end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200628 timestamp_t num;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700629
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200630 num = parse_timestamp(date, &end, 10);
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700631
632 /*
Johannes Sixta1a5a632007-06-06 10:11:55 +0200633 * Seconds since 1970? We trigger on that for any numbers with
634 * more than 8 digits. This is because we don't want to rule out
635 * numbers like 20070606 as a YYYYMMDD date.
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700636 */
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700637 if (num >= 100000000 && nodate(tm)) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700638 time_t time = num;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700639 if (gmtime_r(&time, tm)) {
640 *tm_gmt = 1;
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700641 return end - date;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700642 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700643 }
644
645 /*
Junio C Hamano38035cf2006-04-05 15:31:12 -0700646 * Check for special formats: num[-.:/]num[same]num
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700647 */
648 switch (*end) {
649 case ':':
Junio C Hamano38035cf2006-04-05 15:31:12 -0700650 case '.':
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700651 case '/':
652 case '-':
653 if (isdigit(end[1])) {
Jeff King073281e2014-11-13 06:04:52 -0500654 int match = match_multi_number(num, *end, date, end, tm, 0);
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700655 if (match)
656 return match;
Linus Torvalds89967022005-04-30 13:19:56 -0700657 }
658 }
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700659
660 /*
661 * None of the special formats? Try to guess what
662 * the number meant. We use the number of digits
663 * to make a more educated guess..
664 */
665 n = 0;
666 do {
667 n++;
668 } while (isdigit(date[n]));
669
670 /* Four-digit year or a timezone? */
671 if (n == 4) {
Paul Eggert7122f822006-06-08 14:54:13 -0700672 if (num <= 1400 && *offset == -1) {
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700673 unsigned int minutes = num % 100;
674 unsigned int hours = num / 100;
675 *offset = hours*60 + minutes;
676 } else if (num > 1900 && num < 2100)
677 tm->tm_year = num - 1900;
678 return n;
679 }
680
681 /*
Linus Torvalds9f2b6d22008-08-16 21:25:40 -0700682 * Ignore lots of numerals. We took care of 4-digit years above.
683 * Days or months must be one or two digits.
684 */
685 if (n > 2)
686 return n;
687
688 /*
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700689 * NOTE! We will give precedence to day-of-month over month or
Junio C Hamano82f9d582005-12-29 01:30:08 -0800690 * year numbers in the 1-12 range. So 05 is always "mday 5",
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700691 * unless we already have a mday..
692 *
693 * IOW, 01 Apr 05 parses as "April 1st, 2005".
694 */
695 if (num > 0 && num < 32 && tm->tm_mday < 0) {
696 tm->tm_mday = num;
697 return n;
698 }
699
700 /* Two-digit year? */
701 if (n == 2 && tm->tm_year < 0) {
702 if (num < 10 && tm->tm_mday >= 0) {
703 tm->tm_year = num + 100;
704 return n;
705 }
706 if (num >= 70) {
707 tm->tm_year = num;
708 return n;
709 }
710 }
711
Linus Torvalds90290552009-08-22 15:10:07 -0700712 if (num > 0 && num < 13 && tm->tm_mon < 0)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700713 tm->tm_mon = num-1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700714
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700715 return n;
Linus Torvalds89967022005-04-30 13:19:56 -0700716}
717
Linus Torvalds26a2d8a2005-07-12 10:33:06 -0700718static int match_tz(const char *date, int *offp)
Linus Torvalds89967022005-04-30 13:19:56 -0700719{
720 char *end;
Haitao Liee646eb2011-09-09 18:10:33 +0800721 int hour = strtoul(date + 1, &end, 10);
722 int n = end - (date + 1);
723 int min = 0;
Linus Torvalds89967022005-04-30 13:19:56 -0700724
Haitao Liee646eb2011-09-09 18:10:33 +0800725 if (n == 4) {
726 /* hhmm */
727 min = hour % 100;
728 hour = hour / 100;
729 } else if (n != 2) {
730 min = 99; /* random crap */
731 } else if (*end == ':') {
732 /* hh:mm? */
733 min = strtoul(end + 1, &end, 10);
734 if (end - (date + 1) != 5)
735 min = 99; /* random crap */
736 } /* otherwise we parsed "hh" */
Linus Torvalds89967022005-04-30 13:19:56 -0700737
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700738 /*
Haitao Liee646eb2011-09-09 18:10:33 +0800739 * Don't accept any random crap. Even though some places have
740 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
741 * UTC+14), there is something wrong if hour part is much
742 * larger than that. We might also want to check that the
743 * minutes are divisible by 15 or something too. (Offset of
744 * Kathmandu, Nepal is UTC+5:45)
Linus Torvalds198b0fb2005-05-01 11:48:34 -0700745 */
Haitao Liee646eb2011-09-09 18:10:33 +0800746 if (min < 60 && hour < 24) {
747 int offset = hour * 60 + min;
Linus Torvalds68849b52005-05-01 12:34:56 -0700748 if (*date == '-')
749 offset = -offset;
Linus Torvalds68849b52005-05-01 12:34:56 -0700750 *offp = offset;
751 }
Linus Torvalds89967022005-04-30 13:19:56 -0700752 return end - date;
753}
754
Johannes Schindelindddbad72017-04-26 21:29:31 +0200755static void date_string(timestamp_t date, int offset, struct strbuf *buf)
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700756{
757 int sign = '+';
758
759 if (offset < 0) {
760 offset = -offset;
761 sign = '-';
762 }
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200763 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
Linus Torvalds01c6ad22005-09-21 15:50:28 -0700764}
765
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800766/*
767 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
768 * only when it appears not as part of any other string.
769 */
Johannes Schindelindddbad72017-04-26 21:29:31 +0200770static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800771{
772 char *end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200773 timestamp_t stamp;
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800774 int ofs;
775
Junio C Hamanobe21d162012-07-12 13:46:49 -0700776 if (*date < '0' || '9' < *date)
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800777 return -1;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200778 stamp = parse_timestamp(date, &end, 10);
Johannes Schindelindddbad72017-04-26 21:29:31 +0200779 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800780 return -1;
781 date = end + 2;
782 ofs = strtol(date, &end, 10);
783 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
784 return -1;
785 ofs = (ofs / 100) * 60 + (ofs % 100);
786 if (date[-1] == '-')
787 ofs = -ofs;
788 *timestamp = stamp;
789 *offset = ofs;
790 return 0;
791}
792
Edgar Toernigecee9d92005-04-30 09:46:49 -0700793/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
794 (i.e. English) day/month names, and it doesn't work correctly with %z. */
Johannes Schindelindddbad72017-04-26 21:29:31 +0200795int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700796{
797 struct tm tm;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200798 int tm_gmt;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200799 timestamp_t dummy_timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200800 int dummy_offset;
801
802 if (!timestamp)
803 timestamp = &dummy_timestamp;
804 if (!offset)
805 offset = &dummy_offset;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700806
807 memset(&tm, 0, sizeof(tm));
Linus Torvalds89967022005-04-30 13:19:56 -0700808 tm.tm_year = -1;
809 tm.tm_mon = -1;
810 tm.tm_mday = -1;
Linus Torvaldseaa85122005-04-30 14:25:02 -0700811 tm.tm_isdst = -1;
Linus Torvalds36e49862009-08-22 18:11:44 -0700812 tm.tm_hour = -1;
813 tm.tm_min = -1;
814 tm.tm_sec = -1;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200815 *offset = -1;
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700816 tm_gmt = 0;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700817
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800818 if (*date == '@' &&
819 !match_object_header_date(date + 1, timestamp, offset))
Junio C Hamano116eb3a2012-02-02 13:41:42 -0800820 return 0; /* success */
Linus Torvalds89967022005-04-30 13:19:56 -0700821 for (;;) {
822 int match = 0;
823 unsigned char c = *date;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700824
Linus Torvalds89967022005-04-30 13:19:56 -0700825 /* Stop at end of string or newline */
826 if (!c || c == '\n')
827 break;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700828
Linus Torvalds89967022005-04-30 13:19:56 -0700829 if (isalpha(c))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200830 match = match_alpha(date, &tm, offset);
Linus Torvalds89967022005-04-30 13:19:56 -0700831 else if (isdigit(c))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200832 match = match_digit(date, &tm, offset, &tm_gmt);
Linus Torvalds89967022005-04-30 13:19:56 -0700833 else if ((c == '-' || c == '+') && isdigit(date[1]))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200834 match = match_tz(date, offset);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700835
Linus Torvalds89967022005-04-30 13:19:56 -0700836 if (!match) {
837 /* BAD CRAP */
838 match = 1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700839 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700840
Linus Torvalds89967022005-04-30 13:19:56 -0700841 date += match;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700842 }
Edgar Toernigecee9d92005-04-30 09:46:49 -0700843
Junio C Hamanof6e63622015-04-15 08:47:48 -0700844 /* do not use mktime(), which uses local timezone, here */
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200845 *timestamp = tm_to_time_t(&tm);
Junio C Hamano7fcec482015-04-15 08:43:58 -0700846 if (*timestamp == -1)
847 return -1;
848
Mike Gorchake1033da2013-02-25 23:53:40 +0200849 if (*offset == -1) {
Junio C Hamanof6e63622015-04-15 08:47:48 -0700850 time_t temp_time;
851
852 /* gmtime_r() in match_digit() may have clobbered it */
853 tm.tm_isdst = -1;
854 temp_time = mktime(&tm);
Mike Gorchake1033da2013-02-25 23:53:40 +0200855 if ((time_t)*timestamp > temp_time) {
856 *offset = ((time_t)*timestamp - temp_time) / 60;
857 } else {
858 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
859 }
860 }
Linus Torvaldseaa85122005-04-30 14:25:02 -0700861
Junio C Hamano9cb480f2005-06-25 02:21:16 -0700862 if (!tm_gmt)
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200863 *timestamp -= *offset * 60;
Jonathan Nieder9644c062010-07-15 18:22:57 +0200864 return 0; /* success */
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200865}
866
Johannes Schindelindddbad72017-04-26 21:29:31 +0200867int parse_expiry_date(const char *date, timestamp_t *timestamp)
Junio C Hamano3d27b9b2013-04-17 15:38:08 -0700868{
869 int errors = 0;
870
871 if (!strcmp(date, "never") || !strcmp(date, "false"))
872 *timestamp = 0;
873 else if (!strcmp(date, "all") || !strcmp(date, "now"))
874 /*
875 * We take over "now" here, which usually translates
876 * to the current timestamp. This is because the user
877 * really means to expire everything she has done in
878 * the past, and by definition reflogs are the record
879 * of the past, and there is nothing from the future
880 * to be kept.
881 */
Johannes Schindelindddbad72017-04-26 21:29:31 +0200882 *timestamp = TIME_MAX;
Junio C Hamano3d27b9b2013-04-17 15:38:08 -0700883 else
884 *timestamp = approxidate_careful(date, &errors);
885
886 return errors;
887}
888
Jeff Kingc33ddc22014-08-27 03:57:08 -0400889int parse_date(const char *date, struct strbuf *result)
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200890{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200891 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200892 int offset;
Jonathan Nieder9644c062010-07-15 18:22:57 +0200893 if (parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +0200894 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400895 date_string(timestamp, offset, result);
896 return 0;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700897}
898
Jeff Kingadd00ba2015-09-03 22:48:59 +0100899static enum date_mode_type parse_date_type(const char *format, const char **end)
900{
901 if (skip_prefix(format, "relative", end))
902 return DATE_RELATIVE;
903 if (skip_prefix(format, "iso8601-strict", end) ||
904 skip_prefix(format, "iso-strict", end))
905 return DATE_ISO8601_STRICT;
906 if (skip_prefix(format, "iso8601", end) ||
907 skip_prefix(format, "iso", end))
908 return DATE_ISO8601;
909 if (skip_prefix(format, "rfc2822", end) ||
910 skip_prefix(format, "rfc", end))
911 return DATE_RFC2822;
912 if (skip_prefix(format, "short", end))
913 return DATE_SHORT;
914 if (skip_prefix(format, "default", end))
915 return DATE_NORMAL;
Linus Torvaldsacdd3772019-01-17 23:18:01 -0700916 if (skip_prefix(format, "human", end))
917 return DATE_HUMAN;
Jeff Kingadd00ba2015-09-03 22:48:59 +0100918 if (skip_prefix(format, "raw", end))
919 return DATE_RAW;
Jeff King642833d2016-07-22 15:51:49 -0400920 if (skip_prefix(format, "unix", end))
921 return DATE_UNIX;
Jeff Kingadd00ba2015-09-03 22:48:59 +0100922 if (skip_prefix(format, "format", end))
923 return DATE_STRFTIME;
924
925 die("unknown date format %s", format);
926}
927
Jeff Kinga5481a62015-06-25 12:55:02 -0400928void parse_date_format(const char *format, struct date_mode *mode)
Andy Parkins856665f2007-09-28 15:17:31 +0100929{
Jeff Kingadd00ba2015-09-03 22:48:59 +0100930 const char *p;
931
Stephen P. Smith2fd7c222019-01-20 22:31:09 -0700932 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
933 if (skip_prefix(format, "auto:", &p)) {
934 if (isatty(1) || pager_in_use())
935 format = p;
936 else
937 format = "default";
938 }
939
Jeff Kingadd00ba2015-09-03 22:48:59 +0100940 /* historical alias */
941 if (!strcmp(format, "local"))
942 format = "default-local";
943
944 mode->type = parse_date_type(format, &p);
945 mode->local = 0;
946
947 if (skip_prefix(p, "-local", &p))
948 mode->local = 1;
949
950 if (mode->type == DATE_STRFTIME) {
951 if (!skip_prefix(p, ":", &p))
952 die("date format missing colon separator: %s", format);
953 mode->strftime_fmt = xstrdup(p);
954 } else if (*p)
Andy Parkins856665f2007-09-28 15:17:31 +0100955 die("unknown date format %s", format);
956}
957
Jeff Kingc33ddc22014-08-27 03:57:08 -0400958void datestamp(struct strbuf *out)
Edgar Toernigecee9d92005-04-30 09:46:49 -0700959{
960 time_t now;
961 int offset;
962
963 time(&now);
964
Johannes Sixtbb5799d2008-06-23 08:31:41 +0200965 offset = tm_to_time_t(localtime(&now)) - now;
Edgar Toernigecee9d92005-04-30 09:46:49 -0700966 offset /= 60;
967
Jeff Kingc33ddc22014-08-27 03:57:08 -0400968 date_string(now, offset, out);
Edgar Toernigecee9d92005-04-30 09:46:49 -0700969}
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800970
Linus Torvalds90290552009-08-22 15:10:07 -0700971/*
972 * Relative time update (eg "2 days ago"). If we haven't set the time
973 * yet, we need to set it from current time.
974 */
Johannes Schindelindddbad72017-04-26 21:29:31 +0200975static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800976{
Linus Torvalds90290552009-08-22 15:10:07 -0700977 time_t n;
978
979 if (tm->tm_mday < 0)
980 tm->tm_mday = now->tm_mday;
981 if (tm->tm_mon < 0)
982 tm->tm_mon = now->tm_mon;
983 if (tm->tm_year < 0) {
984 tm->tm_year = now->tm_year;
985 if (tm->tm_mon > now->tm_mon)
986 tm->tm_year--;
987 }
988
989 n = mktime(tm) - sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800990 localtime_r(&n, tm);
Linus Torvalds90290552009-08-22 15:10:07 -0700991 return n;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800992}
993
Jeff Kingc27cc942018-11-02 01:23:09 -0400994/*
995 * Do we have a pending number at the end, or when
996 * we see a new one? Let's assume it's a month day,
997 * as in "Dec 6, 1992"
998 */
999static void pending_number(struct tm *tm, int *num)
1000{
1001 int number = *num;
1002
1003 if (number) {
1004 *num = 0;
1005 if (tm->tm_mday < 0 && number < 32)
1006 tm->tm_mday = number;
1007 else if (tm->tm_mon < 0 && number < 13)
1008 tm->tm_mon = number-1;
1009 else if (tm->tm_year < 0) {
1010 if (number > 1969 && number < 2100)
1011 tm->tm_year = number - 1900;
1012 else if (number > 69 && number < 100)
1013 tm->tm_year = number;
1014 else if (number < 38)
1015 tm->tm_year = 100 + number;
1016 /* We screw up for number = 00 ? */
1017 }
1018 }
1019}
1020
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001021static void date_now(struct tm *tm, struct tm *now, int *num)
1022{
Jeff Kingc27cc942018-11-02 01:23:09 -04001023 *num = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001024 update_tm(tm, now, 0);
1025}
1026
Linus Torvalds90290552009-08-22 15:10:07 -07001027static void date_yesterday(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001028{
Jeff Kingc27cc942018-11-02 01:23:09 -04001029 *num = 0;
Linus Torvalds90290552009-08-22 15:10:07 -07001030 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001031}
1032
Linus Torvalds90290552009-08-22 15:10:07 -07001033static void date_time(struct tm *tm, struct tm *now, int hour)
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001034{
1035 if (tm->tm_hour < hour)
Jeff Kingaa097b82018-11-06 20:12:53 -05001036 update_tm(tm, now, 24*60*60);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001037 tm->tm_hour = hour;
1038 tm->tm_min = 0;
1039 tm->tm_sec = 0;
1040}
1041
Linus Torvalds90290552009-08-22 15:10:07 -07001042static void date_midnight(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001043{
Jeff Kingc27cc942018-11-02 01:23:09 -04001044 pending_number(tm, num);
Linus Torvalds90290552009-08-22 15:10:07 -07001045 date_time(tm, now, 0);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001046}
1047
Linus Torvalds90290552009-08-22 15:10:07 -07001048static void date_noon(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001049{
Jeff Kingc27cc942018-11-02 01:23:09 -04001050 pending_number(tm, num);
Linus Torvalds90290552009-08-22 15:10:07 -07001051 date_time(tm, now, 12);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001052}
1053
Linus Torvalds90290552009-08-22 15:10:07 -07001054static void date_tea(struct tm *tm, struct tm *now, int *num)
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001055{
Jeff Kingc27cc942018-11-02 01:23:09 -04001056 pending_number(tm, num);
Linus Torvalds90290552009-08-22 15:10:07 -07001057 date_time(tm, now, 17);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001058}
1059
Linus Torvalds90290552009-08-22 15:10:07 -07001060static void date_pm(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -07001061{
Linus Torvalds18b633c2006-09-29 12:36:13 -07001062 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -07001063 *num = 0;
1064
Linus Torvalds18b633c2006-09-29 12:36:13 -07001065 hour = tm->tm_hour;
1066 if (n) {
1067 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -07001068 tm->tm_min = 0;
1069 tm->tm_sec = 0;
1070 }
Linus Torvalds18b633c2006-09-29 12:36:13 -07001071 tm->tm_hour = (hour % 12) + 12;
Linus Torvalds393d3402006-09-28 12:14:27 -07001072}
1073
Linus Torvalds90290552009-08-22 15:10:07 -07001074static void date_am(struct tm *tm, struct tm *now, int *num)
Linus Torvalds393d3402006-09-28 12:14:27 -07001075{
Linus Torvalds18b633c2006-09-29 12:36:13 -07001076 int hour, n = *num;
Linus Torvalds393d3402006-09-28 12:14:27 -07001077 *num = 0;
1078
Linus Torvalds18b633c2006-09-29 12:36:13 -07001079 hour = tm->tm_hour;
1080 if (n) {
1081 hour = n;
Linus Torvalds393d3402006-09-28 12:14:27 -07001082 tm->tm_min = 0;
1083 tm->tm_sec = 0;
1084 }
Linus Torvalds18b633c2006-09-29 12:36:13 -07001085 tm->tm_hour = (hour % 12);
Linus Torvalds393d3402006-09-28 12:14:27 -07001086}
1087
Linus Torvalds90290552009-08-22 15:10:07 -07001088static void date_never(struct tm *tm, struct tm *now, int *num)
Johannes Schindelinaf663662007-07-24 19:18:34 +01001089{
Olivier Marin8c6b5782008-06-17 18:34:57 +02001090 time_t n = 0;
1091 localtime_r(&n, tm);
Jeff Kingc27cc942018-11-02 01:23:09 -04001092 *num = 0;
Johannes Schindelinaf663662007-07-24 19:18:34 +01001093}
1094
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001095static const struct special {
1096 const char *name;
Linus Torvalds90290552009-08-22 15:10:07 -07001097 void (*fn)(struct tm *, struct tm *, int *);
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001098} special[] = {
1099 { "yesterday", date_yesterday },
1100 { "noon", date_noon },
1101 { "midnight", date_midnight },
1102 { "tea", date_tea },
Linus Torvalds393d3402006-09-28 12:14:27 -07001103 { "PM", date_pm },
1104 { "AM", date_am },
Johannes Schindelinaf663662007-07-24 19:18:34 +01001105 { "never", date_never },
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001106 { "now", date_now },
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001107 { NULL }
1108};
1109
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001110static const char *number_name[] = {
1111 "zero", "one", "two", "three", "four",
1112 "five", "six", "seven", "eight", "nine", "ten",
1113};
1114
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001115static const struct typelen {
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001116 const char *type;
1117 int length;
1118} typelen[] = {
1119 { "seconds", 1 },
1120 { "minutes", 60 },
1121 { "hours", 60*60 },
1122 { "days", 24*60*60 },
1123 { "weeks", 7*24*60*60 },
1124 { NULL }
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001125};
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001126
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001127static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001128{
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001129 const struct typelen *tl;
1130 const struct special *s;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001131 const char *end = date;
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +02001132 int i;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001133
Jeff King38db01b2013-10-24 04:42:17 -04001134 while (isalpha(*++end))
Pierre Habouzit5df7dbb2006-08-23 12:39:16 +02001135 ;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001136
1137 for (i = 0; i < 12; i++) {
1138 int match = match_string(date, month_names[i]);
1139 if (match >= 3) {
1140 tm->tm_mon = i;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001141 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001142 return end;
1143 }
1144 }
1145
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001146 for (s = special; s->name; s++) {
1147 int len = strlen(s->name);
1148 if (match_string(date, s->name) == len) {
Linus Torvalds90290552009-08-22 15:10:07 -07001149 s->fn(tm, now, num);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001150 *touched = 1;
Linus Torvaldsa8aca412005-11-18 08:56:40 -08001151 return end;
1152 }
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001153 }
1154
1155 if (!*num) {
1156 for (i = 1; i < 11; i++) {
1157 int len = strlen(number_name[i]);
1158 if (match_string(date, number_name[i]) == len) {
1159 *num = i;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001160 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001161 return end;
1162 }
1163 }
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001164 if (match_string(date, "last") == 4) {
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001165 *num = 1;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001166 *touched = 1;
1167 }
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001168 return end;
1169 }
1170
1171 tl = typelen;
1172 while (tl->type) {
1173 int len = strlen(tl->type);
1174 if (match_string(date, tl->type) >= len-1) {
Linus Torvalds90290552009-08-22 15:10:07 -07001175 update_tm(tm, now, tl->length * *num);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001176 *num = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001177 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001178 return end;
1179 }
1180 tl++;
1181 }
1182
Linus Torvalds6b7b0422005-11-17 12:36:30 -08001183 for (i = 0; i < 7; i++) {
1184 int match = match_string(date, weekday_names[i]);
1185 if (match >= 3) {
1186 int diff, n = *num -1;
1187 *num = 0;
1188
1189 diff = tm->tm_wday - i;
1190 if (diff <= 0)
1191 n++;
1192 diff += 7*n;
1193
Linus Torvalds90290552009-08-22 15:10:07 -07001194 update_tm(tm, now, diff * 24 * 60 * 60);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001195 *touched = 1;
Linus Torvalds6b7b0422005-11-17 12:36:30 -08001196 return end;
1197 }
1198 }
1199
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001200 if (match_string(date, "months") >= 5) {
Jeff King931e8e22009-08-30 22:31:42 -04001201 int n;
1202 update_tm(tm, now, 0); /* fill in date fields if needed */
1203 n = tm->tm_mon - *num;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001204 *num = 0;
1205 while (n < 0) {
1206 n += 12;
1207 tm->tm_year--;
1208 }
1209 tm->tm_mon = n;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001210 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001211 return end;
1212 }
1213
1214 if (match_string(date, "years") >= 4) {
Jeff King931e8e22009-08-30 22:31:42 -04001215 update_tm(tm, now, 0); /* fill in date fields if needed */
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001216 tm->tm_year -= *num;
1217 *num = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001218 *touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001219 return end;
1220 }
1221
1222 return end;
1223}
1224
Jeff King073281e2014-11-13 06:04:52 -05001225static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1226 time_t now)
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001227{
1228 char *end;
Johannes Schindelindddbad72017-04-26 21:29:31 +02001229 timestamp_t number = parse_timestamp(date, &end, 10);
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001230
Linus Torvalds393d3402006-09-28 12:14:27 -07001231 switch (*end) {
1232 case ':':
1233 case '.':
1234 case '/':
1235 case '-':
1236 if (isdigit(end[1])) {
Jeff King073281e2014-11-13 06:04:52 -05001237 int match = match_multi_number(number, *end, date, end,
1238 tm, now);
Linus Torvalds393d3402006-09-28 12:14:27 -07001239 if (match)
1240 return date + match;
1241 }
1242 }
1243
Linus Torvalds9f2b6d22008-08-16 21:25:40 -07001244 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1245 if (date[0] != '0' || end - date <= 2)
1246 *num = number;
Linus Torvaldse92a54d2006-09-28 12:12:28 -07001247 return end;
1248}
1249
Johannes Schindelindddbad72017-04-26 21:29:31 +02001250static timestamp_t approxidate_str(const char *date,
1251 const struct timeval *tv,
1252 int *error_ret)
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001253{
1254 int number = 0;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001255 int touched = 0;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001256 struct tm tm, now;
Bernd Ahlersf697b332009-04-06 19:26:37 +02001257 time_t time_sec;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001258
Alex Riesen33012fc2009-08-30 22:26:05 -04001259 time_sec = tv->tv_sec;
Bernd Ahlersf697b332009-04-06 19:26:37 +02001260 localtime_r(&time_sec, &tm);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001261 now = tm;
Linus Torvalds90290552009-08-22 15:10:07 -07001262
1263 tm.tm_year = -1;
1264 tm.tm_mon = -1;
1265 tm.tm_mday = -1;
1266
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001267 for (;;) {
1268 unsigned char c = *date;
1269 if (!c)
1270 break;
1271 date++;
1272 if (isdigit(c)) {
Linus Torvalds90290552009-08-22 15:10:07 -07001273 pending_number(&tm, &number);
Jeff King073281e2014-11-13 06:04:52 -05001274 date = approxidate_digit(date-1, &tm, &number, time_sec);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001275 touched = 1;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001276 continue;
1277 }
1278 if (isalpha(c))
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001279 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001280 }
Linus Torvalds90290552009-08-22 15:10:07 -07001281 pending_number(&tm, &number);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001282 if (!touched)
1283 *error_ret = 1;
Johannes Schindelindddbad72017-04-26 21:29:31 +02001284 return (timestamp_t)update_tm(&tm, &now, 0);
Linus Torvalds3c07b1d2005-11-14 19:29:06 -08001285}
Alex Riesen33012fc2009-08-30 22:26:05 -04001286
Johannes Schindelindddbad72017-04-26 21:29:31 +02001287timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
Alex Riesen33012fc2009-08-30 22:26:05 -04001288{
Johannes Schindelindddbad72017-04-26 21:29:31 +02001289 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001290 int offset;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001291 int errors = 0;
Alex Riesen33012fc2009-08-30 22:26:05 -04001292
Jonathan Nieder9644c062010-07-15 18:22:57 +02001293 if (!parse_date_basic(date, &timestamp, &offset))
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001294 return timestamp;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001295 return approxidate_str(date, tv, &errors);
Alex Riesen33012fc2009-08-30 22:26:05 -04001296}
1297
Johannes Schindelindddbad72017-04-26 21:29:31 +02001298timestamp_t approxidate_careful(const char *date, int *error_ret)
Alex Riesen33012fc2009-08-30 22:26:05 -04001299{
1300 struct timeval tv;
Johannes Schindelindddbad72017-04-26 21:29:31 +02001301 timestamp_t timestamp;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001302 int offset;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001303 int dummy = 0;
1304 if (!error_ret)
1305 error_ret = &dummy;
Alex Riesen33012fc2009-08-30 22:26:05 -04001306
Jonathan Nieder9644c062010-07-15 18:22:57 +02001307 if (!parse_date_basic(date, &timestamp, &offset)) {
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001308 *error_ret = 0;
Ramkumar Ramachandrac5043cc2010-06-03 22:28:55 +02001309 return timestamp;
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001310 }
Alex Riesen33012fc2009-08-30 22:26:05 -04001311
Stephen P. Smithb841d4f2019-01-28 20:50:15 -07001312 get_time(&tv);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -08001313 return approxidate_str(date, &tv, error_ret);
Alex Riesen33012fc2009-08-30 22:26:05 -04001314}
Jeff King7ca36d92014-02-24 02:39:45 -05001315
Johannes Schindelindddbad72017-04-26 21:29:31 +02001316int date_overflows(timestamp_t t)
Jeff King7ca36d92014-02-24 02:39:45 -05001317{
1318 time_t sys;
1319
Johannes Schindelindddbad72017-04-26 21:29:31 +02001320 /* If we overflowed our timestamp data type, that's bad... */
1321 if ((uintmax_t)t >= TIME_MAX)
Jeff King7ca36d92014-02-24 02:39:45 -05001322 return 1;
1323
1324 /*
1325 * ...but we also are going to feed the result to system
1326 * functions that expect time_t, which is often "signed long".
1327 * Make sure that we fit into time_t, as well.
1328 */
1329 sys = t;
1330 return t != sys || (t < 1) != (sys < 1);
1331}