]>
Commit | Line | Data |
---|---|---|
ecee9d9e ET |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
6 | ||
e99d59ff | 7 | #include "cache.h" |
88c7b4c3 | 8 | #include "date.h" |
e99d59ff | 9 | |
bb5799d6 JS |
10 | /* |
11 | * This is like mktime, but without normalization of tm_wday and tm_yday. | |
12 | */ | |
9b591b94 | 13 | time_t tm_to_time_t(const struct tm *tm) |
ecee9d9e ET |
14 | { |
15 | static const int mdays[] = { | |
16 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | |
17 | }; | |
18 | int year = tm->tm_year - 70; | |
19 | int month = tm->tm_mon; | |
20 | int day = tm->tm_mday; | |
21 | ||
22 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
23 | return -1; | |
24 | if (month < 0 || month > 11) /* array bounds */ | |
25 | return -1; | |
26 | if (month < 2 || (year + 2) % 4) | |
27 | day--; | |
36e4986f LT |
28 | if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) |
29 | return -1; | |
ecee9d9e ET |
30 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
31 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
32 | } | |
33 | ||
34 | static const char *month_names[] = { | |
89967023 LT |
35 | "January", "February", "March", "April", "May", "June", |
36 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
37 | }; |
38 | ||
39 | static const char *weekday_names[] = { | |
6b7b0427 | 40 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
ecee9d9e ET |
41 | }; |
42 | ||
dddbad72 | 43 | static time_t gm_time_t(timestamp_t time, int tz) |
9a8e35e9 LT |
44 | { |
45 | int minutes; | |
46 | ||
47 | minutes = tz < 0 ? -tz : tz; | |
48 | minutes = (minutes / 100)*60 + (minutes % 100); | |
49 | minutes = tz < 0 ? -minutes : minutes; | |
1e65a982 JS |
50 | |
51 | if (minutes > 0) { | |
52 | if (unsigned_add_overflows(time, minutes * 60)) | |
53 | die("Timestamp+tz too large: %"PRItime" +%04d", | |
54 | time, tz); | |
55 | } else if (time < -minutes * 60) | |
56 | die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz); | |
57 | time += minutes * 60; | |
58 | if (date_overflows(time)) | |
59 | die("Timestamp too large for this system: %"PRItime, time); | |
60 | return (time_t)time; | |
9a8e35e9 LT |
61 | } |
62 | ||
f80cd783 LT |
63 | /* |
64 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
65 | * thing, which means that tz -0100 is passed in as the integer -100, | |
66 | * even though it means "sixty minutes off" | |
67 | */ | |
ccd46945 | 68 | static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm) |
f80cd783 | 69 | { |
9a8e35e9 | 70 | time_t t = gm_time_t(time, tz); |
ccd46945 | 71 | return gmtime_r(&t, tm); |
2a387043 JH |
72 | } |
73 | ||
ccd46945 | 74 | static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm) |
6eced3ec JK |
75 | { |
76 | time_t t = time; | |
ccd46945 | 77 | return localtime_r(&t, tm); |
6eced3ec JK |
78 | } |
79 | ||
a7b02ccf | 80 | /* |
acdd3776 LT |
81 | * Fill in the localtime 'struct tm' for the supplied time, |
82 | * and return the local tz. | |
a7b02ccf | 83 | */ |
acdd3776 | 84 | static int local_time_tzoffset(time_t t, struct tm *tm) |
a7b02ccf | 85 | { |
acdd3776 | 86 | time_t t_local; |
a7b02ccf JH |
87 | int offset, eastwest; |
88 | ||
acdd3776 LT |
89 | localtime_r(&t, tm); |
90 | t_local = tm_to_time_t(tm); | |
bab74837 JK |
91 | if (t_local == -1) |
92 | return 0; /* error; just use +0000 */ | |
a7b02ccf JH |
93 | if (t_local < t) { |
94 | eastwest = -1; | |
95 | offset = t - t_local; | |
96 | } else { | |
97 | eastwest = 1; | |
98 | offset = t_local - t; | |
99 | } | |
100 | offset /= 60; /* in minutes */ | |
101 | offset = (offset % 60) + ((offset / 60) * 100); | |
102 | return offset * eastwest; | |
103 | } | |
104 | ||
acdd3776 LT |
105 | /* |
106 | * What value of "tz" was in effect back then at "time" in the | |
107 | * local timezone? | |
108 | */ | |
109 | static int local_tzoffset(timestamp_t time) | |
110 | { | |
111 | struct tm tm; | |
112 | ||
113 | if (date_overflows(time)) | |
114 | die("Timestamp too large for this system: %"PRItime, time); | |
115 | ||
116 | return local_time_tzoffset((time_t)time, &tm); | |
117 | } | |
118 | ||
b841d4ff SS |
119 | static void get_time(struct timeval *now) |
120 | { | |
121 | const char *x; | |
122 | ||
123 | x = getenv("GIT_TEST_DATE_NOW"); | |
124 | if (x) { | |
125 | now->tv_sec = atoi(x); | |
126 | now->tv_usec = 0; | |
127 | } | |
128 | else | |
129 | gettimeofday(now, NULL); | |
130 | } | |
131 | ||
29f4332e | 132 | void show_date_relative(timestamp_t time, struct strbuf *timebuf) |
33012fc4 | 133 | { |
29f4332e | 134 | struct timeval now; |
dddbad72 | 135 | timestamp_t diff; |
29f4332e SS |
136 | |
137 | get_time(&now); | |
138 | if (now.tv_sec < time) { | |
7d29afd4 JN |
139 | strbuf_addstr(timebuf, _("in the future")); |
140 | return; | |
141 | } | |
29f4332e | 142 | diff = now.tv_sec - time; |
33012fc4 | 143 | if (diff < 90) { |
7d29afd4 | 144 | strbuf_addf(timebuf, |
cb71f8bd | 145 | Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff); |
7d29afd4 | 146 | return; |
33012fc4 AR |
147 | } |
148 | /* Turn it into minutes */ | |
149 | diff = (diff + 30) / 60; | |
150 | if (diff < 90) { | |
7d29afd4 | 151 | strbuf_addf(timebuf, |
cb71f8bd | 152 | Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff); |
7d29afd4 | 153 | return; |
33012fc4 AR |
154 | } |
155 | /* Turn it into hours */ | |
156 | diff = (diff + 30) / 60; | |
157 | if (diff < 36) { | |
7d29afd4 | 158 | strbuf_addf(timebuf, |
cb71f8bd | 159 | Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff); |
7d29afd4 | 160 | return; |
33012fc4 AR |
161 | } |
162 | /* We deal with number of days from here on */ | |
163 | diff = (diff + 12) / 24; | |
164 | if (diff < 14) { | |
7d29afd4 | 165 | strbuf_addf(timebuf, |
cb71f8bd | 166 | Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff); |
7d29afd4 | 167 | return; |
33012fc4 AR |
168 | } |
169 | /* Say weeks for the past 10 weeks or so */ | |
170 | if (diff < 70) { | |
7d29afd4 | 171 | strbuf_addf(timebuf, |
cb71f8bd | 172 | Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7), |
7d29afd4 JN |
173 | (diff + 3) / 7); |
174 | return; | |
33012fc4 AR |
175 | } |
176 | /* Say months for the past 12 months or so */ | |
dbc1b1f7 | 177 | if (diff < 365) { |
7d29afd4 | 178 | strbuf_addf(timebuf, |
cb71f8bd | 179 | Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30), |
7d29afd4 JN |
180 | (diff + 15) / 30); |
181 | return; | |
33012fc4 AR |
182 | } |
183 | /* Give years and months for 5 years or so */ | |
184 | if (diff < 1825) { | |
dddbad72 JS |
185 | timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2); |
186 | timestamp_t years = totalmonths / 12; | |
187 | timestamp_t months = totalmonths % 12; | |
7d29afd4 JN |
188 | if (months) { |
189 | struct strbuf sb = STRBUF_INIT; | |
cb71f8bd | 190 | strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years); |
7d29afd4 | 191 | strbuf_addf(timebuf, |
fcaed04d | 192 | /* TRANSLATORS: "%s" is "<n> years" */ |
cb71f8bd | 193 | Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months), |
7d29afd4 JN |
194 | sb.buf, months); |
195 | strbuf_release(&sb); | |
196 | } else | |
197 | strbuf_addf(timebuf, | |
cb71f8bd | 198 | Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years); |
7d29afd4 | 199 | return; |
33012fc4 AR |
200 | } |
201 | /* Otherwise, just years. Centuries is probably overkill. */ | |
7d29afd4 | 202 | strbuf_addf(timebuf, |
cb71f8bd | 203 | Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365), |
7d29afd4 | 204 | (diff + 183) / 365); |
33012fc4 AR |
205 | } |
206 | ||
a5481a6c JK |
207 | struct date_mode *date_mode_from_type(enum date_mode_type type) |
208 | { | |
f1842898 | 209 | static struct date_mode mode = DATE_MODE_INIT; |
aa1462cc | 210 | if (type == DATE_STRFTIME) |
033abf97 | 211 | BUG("cannot create anonymous strftime date_mode struct"); |
a5481a6c JK |
212 | mode.type = type; |
213 | return &mode; | |
214 | } | |
215 | ||
acdd3776 LT |
216 | static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local) |
217 | { | |
218 | struct { | |
219 | unsigned int year:1, | |
220 | date:1, | |
221 | wday:1, | |
222 | time:1, | |
223 | seconds:1, | |
224 | tz:1; | |
225 | } hide = { 0 }; | |
226 | ||
227 | hide.tz = local || tz == human_tz; | |
228 | hide.year = tm->tm_year == human_tm->tm_year; | |
229 | if (hide.year) { | |
230 | if (tm->tm_mon == human_tm->tm_mon) { | |
231 | if (tm->tm_mday > human_tm->tm_mday) { | |
232 | /* Future date: think timezones */ | |
233 | } else if (tm->tm_mday == human_tm->tm_mday) { | |
234 | hide.date = hide.wday = 1; | |
235 | } else if (tm->tm_mday + 5 > human_tm->tm_mday) { | |
236 | /* Leave just weekday if it was a few days ago */ | |
237 | hide.date = 1; | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | /* Show "today" times as just relative times */ | |
243 | if (hide.wday) { | |
29f4332e | 244 | show_date_relative(time, buf); |
acdd3776 LT |
245 | return; |
246 | } | |
247 | ||
248 | /* | |
249 | * Always hide seconds for human-readable. | |
250 | * Hide timezone if showing date. | |
251 | * Hide weekday and time if showing year. | |
252 | * | |
253 | * The logic here is two-fold: | |
254 | * (a) only show details when recent enough to matter | |
255 | * (b) keep the maximum length "similar", and in check | |
256 | */ | |
257 | if (human_tm->tm_year) { | |
258 | hide.seconds = 1; | |
259 | hide.tz |= !hide.date; | |
260 | hide.wday = hide.time = !hide.year; | |
261 | } | |
262 | ||
263 | if (!hide.wday) | |
264 | strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]); | |
265 | if (!hide.date) | |
266 | strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday); | |
267 | ||
268 | /* Do we want AM/PM depending on locale? */ | |
269 | if (!hide.time) { | |
270 | strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min); | |
271 | if (!hide.seconds) | |
272 | strbuf_addf(buf, ":%02d", tm->tm_sec); | |
273 | } else | |
274 | strbuf_rtrim(buf); | |
275 | ||
276 | if (!hide.year) | |
277 | strbuf_addf(buf, " %d", tm->tm_year + 1900); | |
278 | ||
279 | if (!hide.tz) | |
280 | strbuf_addf(buf, " %+05d", tz); | |
281 | } | |
282 | ||
dddbad72 | 283 | const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) |
2a387043 JH |
284 | { |
285 | struct tm *tm; | |
ccd46945 | 286 | struct tm tmbuf = { 0 }; |
acdd3776 LT |
287 | struct tm human_tm = { 0 }; |
288 | int human_tz = -1; | |
7d29afd4 | 289 | static struct strbuf timebuf = STRBUF_INIT; |
2a387043 | 290 | |
642833db JK |
291 | if (mode->type == DATE_UNIX) { |
292 | strbuf_reset(&timebuf); | |
cb71f8bd | 293 | strbuf_addf(&timebuf, "%"PRItime, time); |
642833db JK |
294 | return timebuf.buf; |
295 | } | |
296 | ||
acdd3776 LT |
297 | if (mode->type == DATE_HUMAN) { |
298 | struct timeval now; | |
299 | ||
b841d4ff | 300 | get_time(&now); |
acdd3776 LT |
301 | |
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 | ||
add00ba2 | 306 | if (mode->local) |
dc6d782c JK |
307 | tz = local_tzoffset(time); |
308 | ||
a5481a6c | 309 | if (mode->type == DATE_RAW) { |
7d29afd4 | 310 | strbuf_reset(&timebuf); |
cb71f8bd | 311 | strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz); |
7d29afd4 | 312 | return timebuf.buf; |
7dff9b30 LT |
313 | } |
314 | ||
a5481a6c | 315 | if (mode->type == DATE_RELATIVE) { |
7d29afd4 | 316 | strbuf_reset(&timebuf); |
29f4332e | 317 | show_date_relative(time, &timebuf); |
7d29afd4 | 318 | return timebuf.buf; |
9a8e35e9 LT |
319 | } |
320 | ||
6eced3ec | 321 | if (mode->local) |
ccd46945 | 322 | tm = time_to_tm_local(time, &tmbuf); |
6eced3ec | 323 | else |
ccd46945 | 324 | tm = time_to_tm(time, tz, &tmbuf); |
2b15846d | 325 | if (!tm) { |
ccd46945 | 326 | tm = time_to_tm(0, 0, &tmbuf); |
2b15846d JK |
327 | tz = 0; |
328 | } | |
7d29afd4 JN |
329 | |
330 | strbuf_reset(&timebuf); | |
a5481a6c | 331 | if (mode->type == DATE_SHORT) |
7d29afd4 | 332 | strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, |
f8493ec0 | 333 | tm->tm_mon + 1, tm->tm_mday); |
a5481a6c | 334 | else if (mode->type == DATE_ISO8601) |
7d29afd4 | 335 | strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", |
ee8f838e RR |
336 | tm->tm_year + 1900, |
337 | tm->tm_mon + 1, | |
338 | tm->tm_mday, | |
339 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
340 | tz); | |
a5481a6c | 341 | else if (mode->type == DATE_ISO8601_STRICT) { |
466fb674 BB |
342 | char sign = (tz >= 0) ? '+' : '-'; |
343 | tz = abs(tz); | |
344 | strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", | |
345 | tm->tm_year + 1900, | |
346 | tm->tm_mon + 1, | |
347 | tm->tm_mday, | |
348 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
349 | sign, tz / 100, tz % 100); | |
a5481a6c | 350 | } else if (mode->type == DATE_RFC2822) |
7d29afd4 | 351 | strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", |
73013afd JH |
352 | weekday_names[tm->tm_wday], tm->tm_mday, |
353 | month_names[tm->tm_mon], tm->tm_year + 1900, | |
354 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); | |
aa1462cc | 355 | else if (mode->type == DATE_STRFTIME) |
6eced3ec | 356 | strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz, |
3b702239 | 357 | !mode->local); |
f8493ec0 | 358 | else |
acdd3776 | 359 | show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local); |
7d29afd4 | 360 | return timebuf.buf; |
f80cd783 LT |
361 | } |
362 | ||
89967023 LT |
363 | /* |
364 | * Check these. And note how it doesn't do the summer-time conversion. | |
365 | * | |
366 | * In my world, it's always summer, and things are probably a bit off | |
367 | * in other ways too. | |
368 | */ | |
369 | static const struct { | |
370 | const char *name; | |
371 | int offset; | |
5e2a78a4 | 372 | int dst; |
89967023 | 373 | } timezone_names[] = { |
5e2a78a4 LT |
374 | { "IDLW", -12, 0, }, /* International Date Line West */ |
375 | { "NT", -11, 0, }, /* Nome */ | |
376 | { "CAT", -10, 0, }, /* Central Alaska */ | |
377 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
378 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
379 | { "YST", -9, 0, }, /* Yukon Standard */ | |
380 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
381 | { "PST", -8, 0, }, /* Pacific Standard */ | |
382 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
383 | { "MST", -7, 0, }, /* Mountain Standard */ | |
384 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
385 | { "CST", -6, 0, }, /* Central Standard */ | |
386 | { "CDT", -6, 1, }, /* Central Daylight */ | |
387 | { "EST", -5, 0, }, /* Eastern Standard */ | |
388 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
389 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
390 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
391 | { "WAT", -1, 0, }, /* West Africa */ | |
392 | ||
393 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
394 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
75b37e70 | 395 | { "Z", 0, 0, }, /* Zulu, alias for UTC */ |
5e2a78a4 LT |
396 | |
397 | { "WET", 0, 0, }, /* Western European */ | |
398 | { "BST", 0, 1, }, /* British Summer */ | |
399 | { "CET", +1, 0, }, /* Central European */ | |
400 | { "MET", +1, 0, }, /* Middle European */ | |
401 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
402 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
403 | { "CEST", +1, 1, }, /* Central European Summer */ | |
404 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
405 | { "FWT", +1, 0, }, /* French Winter */ | |
406 | { "FST", +1, 1, }, /* French Summer */ | |
407 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 408 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
409 | { "WAST", +7, 0, }, /* West Australian Standard */ |
410 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
411 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
412 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
413 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
414 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
415 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
695ed472 SD |
416 | { "NZT", +12, 0, }, /* New Zealand */ |
417 | { "NZST", +12, 0, }, /* New Zealand Standard */ | |
418 | { "NZDT", +12, 1, }, /* New Zealand Daylight */ | |
5e2a78a4 | 419 | { "IDLE", +12, 0, }, /* International Date Line East */ |
89967023 | 420 | }; |
ecee9d9e | 421 | |
89967023 | 422 | static int match_string(const char *date, const char *str) |
ecee9d9e | 423 | { |
89967023 LT |
424 | int i = 0; |
425 | ||
426 | for (i = 0; *date; date++, str++, i++) { | |
427 | if (*date == *str) | |
428 | continue; | |
429 | if (toupper(*date) == toupper(*str)) | |
430 | continue; | |
431 | if (!isalnum(*date)) | |
432 | break; | |
433 | return 0; | |
434 | } | |
435 | return i; | |
ecee9d9e ET |
436 | } |
437 | ||
68849b54 LT |
438 | static int skip_alpha(const char *date) |
439 | { | |
440 | int i = 0; | |
441 | do { | |
442 | i++; | |
443 | } while (isalpha(date[i])); | |
444 | return i; | |
445 | } | |
446 | ||
89967023 LT |
447 | /* |
448 | * Parse month, weekday, or timezone name | |
449 | */ | |
450 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 451 | { |
89967023 | 452 | int i; |
ecee9d9e | 453 | |
89967023 LT |
454 | for (i = 0; i < 12; i++) { |
455 | int match = match_string(date, month_names[i]); | |
456 | if (match >= 3) { | |
457 | tm->tm_mon = i; | |
458 | return match; | |
ecee9d9e | 459 | } |
89967023 | 460 | } |
ecee9d9e | 461 | |
89967023 LT |
462 | for (i = 0; i < 7; i++) { |
463 | int match = match_string(date, weekday_names[i]); | |
464 | if (match >= 3) { | |
465 | tm->tm_wday = i; | |
466 | return match; | |
467 | } | |
468 | } | |
ecee9d9e | 469 | |
b4f2a6ac | 470 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
89967023 | 471 | int match = match_string(date, timezone_names[i].name); |
75b37e70 | 472 | if (match >= 3 || match == strlen(timezone_names[i].name)) { |
5e2a78a4 LT |
473 | int off = timezone_names[i].offset; |
474 | ||
475 | /* This is bogus, but we like summer */ | |
476 | off += timezone_names[i].dst; | |
477 | ||
92e2311b LT |
478 | /* Only use the tz name offset if we don't have anything better */ |
479 | if (*offset == -1) | |
480 | *offset = 60*off; | |
481 | ||
89967023 | 482 | return match; |
ecee9d9e ET |
483 | } |
484 | } | |
ecee9d9e | 485 | |
68849b54 | 486 | if (match_string(date, "PM") == 2) { |
18b633ca LT |
487 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
488 | return 2; | |
489 | } | |
490 | ||
491 | if (match_string(date, "AM") == 2) { | |
492 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
68849b54 LT |
493 | return 2; |
494 | } | |
495 | ||
89967023 | 496 | /* BAD CRAP */ |
68849b54 | 497 | return skip_alpha(date); |
89967023 | 498 | } |
ecee9d9e | 499 | |
c933b28d | 500 | static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
89967023 | 501 | { |
198b0fb6 | 502 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
38035cf4 JH |
503 | struct tm check = *tm; |
504 | struct tm *r = (now_tm ? &check : tm); | |
505 | time_t specified; | |
506 | ||
507 | r->tm_mon = month - 1; | |
508 | r->tm_mday = day; | |
198b0fb6 | 509 | if (year == -1) { |
38035cf4 JH |
510 | if (!now_tm) |
511 | return 1; | |
512 | r->tm_year = now_tm->tm_year; | |
89967023 | 513 | } |
38035cf4 JH |
514 | else if (year >= 1970 && year < 2100) |
515 | r->tm_year = year - 1900; | |
516 | else if (year > 70 && year < 100) | |
517 | r->tm_year = year; | |
518 | else if (year < 38) | |
519 | r->tm_year = year + 100; | |
520 | else | |
c933b28d | 521 | return -1; |
38035cf4 | 522 | if (!now_tm) |
c933b28d | 523 | return 0; |
38035cf4 | 524 | |
bb5799d6 | 525 | specified = tm_to_time_t(r); |
198b0fb6 | 526 | |
38035cf4 JH |
527 | /* Be it commit time or author time, it does not make |
528 | * sense to specify timestamp way into the future. Make | |
529 | * sure it is not later than ten days from now... | |
530 | */ | |
e6e87516 | 531 | if ((specified != -1) && (now + 10*24*3600 < specified)) |
c933b28d | 532 | return -1; |
38035cf4 JH |
533 | tm->tm_mon = r->tm_mon; |
534 | tm->tm_mday = r->tm_mday; | |
535 | if (year != -1) | |
536 | tm->tm_year = r->tm_year; | |
c933b28d | 537 | return 0; |
89967023 | 538 | } |
c933b28d | 539 | return -1; |
198b0fb6 | 540 | } |
ecee9d9e | 541 | |
4f89f4fc ĐTCD |
542 | static int set_time(long hour, long minute, long second, struct tm *tm) |
543 | { | |
544 | /* We accept 61st second because of leap second */ | |
545 | if (0 <= hour && hour <= 24 && | |
546 | 0 <= minute && minute < 60 && | |
547 | 0 <= second && second <= 60) { | |
548 | tm->tm_hour = hour; | |
549 | tm->tm_min = minute; | |
550 | tm->tm_sec = second; | |
551 | return 0; | |
552 | } | |
553 | return -1; | |
554 | } | |
555 | ||
b784840c ĐTCD |
556 | static int is_date_known(struct tm *tm) |
557 | { | |
558 | return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1; | |
559 | } | |
560 | ||
dddbad72 | 561 | static int match_multi_number(timestamp_t num, char c, const char *date, |
073281e2 | 562 | char *end, struct tm *tm, time_t now) |
198b0fb6 | 563 | { |
38035cf4 JH |
564 | struct tm now_tm; |
565 | struct tm *refuse_future; | |
198b0fb6 LT |
566 | long num2, num3; |
567 | ||
568 | num2 = strtol(end+1, &end, 10); | |
569 | num3 = -1; | |
570 | if (*end == c && isdigit(end[1])) | |
571 | num3 = strtol(end+1, &end, 10); | |
572 | ||
573 | /* Time? Date? */ | |
89967023 | 574 | switch (c) { |
198b0fb6 LT |
575 | case ':': |
576 | if (num3 < 0) | |
577 | num3 = 0; | |
4f89f4fc | 578 | if (set_time(num, num2, num3, tm) == 0) { |
b784840c ĐTCD |
579 | /* |
580 | * If %H:%M:%S was just parsed followed by: .<num4> | |
581 | * Consider (& discard) it as fractional second | |
582 | * if %Y%m%d is parsed before. | |
583 | */ | |
584 | if (*end == '.' && isdigit(end[1]) && is_date_known(tm)) | |
585 | strtol(end + 1, &end, 10); | |
89967023 LT |
586 | break; |
587 | } | |
198b0fb6 | 588 | return 0; |
89967023 LT |
589 | |
590 | case '-': | |
591 | case '/': | |
38035cf4 | 592 | case '.': |
073281e2 JK |
593 | if (!now) |
594 | now = time(NULL); | |
38035cf4 JH |
595 | refuse_future = NULL; |
596 | if (gmtime_r(&now, &now_tm)) | |
597 | refuse_future = &now_tm; | |
598 | ||
198b0fb6 LT |
599 | if (num > 70) { |
600 | /* yyyy-mm-dd? */ | |
c933b28d | 601 | if (set_date(num, num2, num3, NULL, now, tm) == 0) |
198b0fb6 LT |
602 | break; |
603 | /* yyyy-dd-mm? */ | |
c933b28d | 604 | if (set_date(num, num3, num2, NULL, now, tm) == 0) |
89967023 | 605 | break; |
198b0fb6 | 606 | } |
38035cf4 JH |
607 | /* Our eastern European friends say dd.mm.yy[yy] |
608 | * is the norm there, so giving precedence to | |
609 | * mm/dd/yy[yy] form only when separator is not '.' | |
610 | */ | |
611 | if (c != '.' && | |
c933b28d | 612 | set_date(num3, num, num2, refuse_future, now, tm) == 0) |
38035cf4 JH |
613 | break; |
614 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | |
c933b28d | 615 | if (set_date(num3, num2, num, refuse_future, now, tm) == 0) |
89967023 | 616 | break; |
38035cf4 JH |
617 | /* Funny European mm.dd.yy */ |
618 | if (c == '.' && | |
c933b28d | 619 | set_date(num3, num, num2, refuse_future, now, tm) == 0) |
198b0fb6 LT |
620 | break; |
621 | return 0; | |
622 | } | |
623 | return end - date; | |
624 | } | |
625 | ||
36e4986f LT |
626 | /* |
627 | * Have we filled in any part of the time/date yet? | |
628 | * We just do a binary 'and' to see if the sign bit | |
629 | * is set in all the values. | |
630 | */ | |
9f2b6d29 LT |
631 | static inline int nodate(struct tm *tm) |
632 | { | |
36e4986f LT |
633 | return (tm->tm_year & |
634 | tm->tm_mon & | |
635 | tm->tm_mday & | |
636 | tm->tm_hour & | |
637 | tm->tm_min & | |
638 | tm->tm_sec) < 0; | |
9f2b6d29 LT |
639 | } |
640 | ||
198b0fb6 | 641 | /* |
a6080a0a | 642 | * We've seen a digit. Time? Year? Date? |
198b0fb6 | 643 | */ |
26a2d8ae | 644 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
198b0fb6 LT |
645 | { |
646 | int n; | |
647 | char *end; | |
dddbad72 | 648 | timestamp_t num; |
198b0fb6 | 649 | |
1aeb7e75 | 650 | num = parse_timestamp(date, &end, 10); |
198b0fb6 LT |
651 | |
652 | /* | |
a1a5a634 JS |
653 | * Seconds since 1970? We trigger on that for any numbers with |
654 | * more than 8 digits. This is because we don't want to rule out | |
655 | * numbers like 20070606 as a YYYYMMDD date. | |
198b0fb6 | 656 | */ |
9f2b6d29 | 657 | if (num >= 100000000 && nodate(tm)) { |
198b0fb6 | 658 | time_t time = num; |
9cb480f2 JH |
659 | if (gmtime_r(&time, tm)) { |
660 | *tm_gmt = 1; | |
198b0fb6 | 661 | return end - date; |
9cb480f2 | 662 | } |
198b0fb6 LT |
663 | } |
664 | ||
665 | /* | |
38035cf4 | 666 | * Check for special formats: num[-.:/]num[same]num |
198b0fb6 LT |
667 | */ |
668 | switch (*end) { | |
669 | case ':': | |
38035cf4 | 670 | case '.': |
198b0fb6 LT |
671 | case '/': |
672 | case '-': | |
673 | if (isdigit(end[1])) { | |
073281e2 | 674 | int match = match_multi_number(num, *end, date, end, tm, 0); |
198b0fb6 LT |
675 | if (match) |
676 | return match; | |
89967023 LT |
677 | } |
678 | } | |
198b0fb6 LT |
679 | |
680 | /* | |
681 | * None of the special formats? Try to guess what | |
682 | * the number meant. We use the number of digits | |
683 | * to make a more educated guess.. | |
684 | */ | |
685 | n = 0; | |
686 | do { | |
687 | n++; | |
688 | } while (isdigit(date[n])); | |
689 | ||
544ed961 ĐTCD |
690 | /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */ |
691 | /* 6 digits, compact style of ISO-8601's time: HHMMSS */ | |
692 | if (n == 8 || n == 6) { | |
693 | unsigned int num1 = num / 10000; | |
694 | unsigned int num2 = (num % 10000) / 100; | |
695 | unsigned int num3 = num % 100; | |
696 | if (n == 8) | |
697 | set_date(num1, num2, num3, NULL, time(NULL), tm); | |
698 | else if (n == 6 && set_time(num1, num2, num3, tm) == 0 && | |
699 | *end == '.' && isdigit(end[1])) | |
700 | strtoul(end + 1, &end, 10); | |
701 | return end - date; | |
702 | } | |
703 | ||
198b0fb6 LT |
704 | /* Four-digit year or a timezone? */ |
705 | if (n == 4) { | |
7122f82f | 706 | if (num <= 1400 && *offset == -1) { |
198b0fb6 LT |
707 | unsigned int minutes = num % 100; |
708 | unsigned int hours = num / 100; | |
709 | *offset = hours*60 + minutes; | |
710 | } else if (num > 1900 && num < 2100) | |
711 | tm->tm_year = num - 1900; | |
712 | return n; | |
713 | } | |
714 | ||
9f2b6d29 LT |
715 | /* |
716 | * Ignore lots of numerals. We took care of 4-digit years above. | |
717 | * Days or months must be one or two digits. | |
718 | */ | |
719 | if (n > 2) | |
720 | return n; | |
721 | ||
198b0fb6 LT |
722 | /* |
723 | * NOTE! We will give precedence to day-of-month over month or | |
82f9d58a | 724 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
198b0fb6 LT |
725 | * unless we already have a mday.. |
726 | * | |
727 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
728 | */ | |
729 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
730 | tm->tm_mday = num; | |
731 | return n; | |
732 | } | |
733 | ||
734 | /* Two-digit year? */ | |
735 | if (n == 2 && tm->tm_year < 0) { | |
736 | if (num < 10 && tm->tm_mday >= 0) { | |
737 | tm->tm_year = num + 100; | |
738 | return n; | |
739 | } | |
740 | if (num >= 70) { | |
741 | tm->tm_year = num; | |
742 | return n; | |
743 | } | |
744 | } | |
745 | ||
90290552 | 746 | if (num > 0 && num < 13 && tm->tm_mon < 0) |
198b0fb6 | 747 | tm->tm_mon = num-1; |
a6080a0a | 748 | |
198b0fb6 | 749 | return n; |
89967023 | 750 | } |
ecee9d9e | 751 | |
26a2d8ae | 752 | static int match_tz(const char *date, int *offp) |
89967023 LT |
753 | { |
754 | char *end; | |
ee646eb4 HL |
755 | int hour = strtoul(date + 1, &end, 10); |
756 | int n = end - (date + 1); | |
757 | int min = 0; | |
ecee9d9e | 758 | |
ee646eb4 HL |
759 | if (n == 4) { |
760 | /* hhmm */ | |
761 | min = hour % 100; | |
762 | hour = hour / 100; | |
763 | } else if (n != 2) { | |
764 | min = 99; /* random crap */ | |
765 | } else if (*end == ':') { | |
766 | /* hh:mm? */ | |
767 | min = strtoul(end + 1, &end, 10); | |
768 | if (end - (date + 1) != 5) | |
769 | min = 99; /* random crap */ | |
770 | } /* otherwise we parsed "hh" */ | |
ecee9d9e | 771 | |
198b0fb6 | 772 | /* |
ee646eb4 HL |
773 | * Don't accept any random crap. Even though some places have |
774 | * offset larger than 12 hours (e.g. Pacific/Kiritimati is at | |
775 | * UTC+14), there is something wrong if hour part is much | |
776 | * larger than that. We might also want to check that the | |
777 | * minutes are divisible by 15 or something too. (Offset of | |
778 | * Kathmandu, Nepal is UTC+5:45) | |
198b0fb6 | 779 | */ |
ee646eb4 HL |
780 | if (min < 60 && hour < 24) { |
781 | int offset = hour * 60 + min; | |
68849b54 LT |
782 | if (*date == '-') |
783 | offset = -offset; | |
68849b54 LT |
784 | *offp = offset; |
785 | } | |
89967023 LT |
786 | return end - date; |
787 | } | |
ecee9d9e | 788 | |
dddbad72 | 789 | static void date_string(timestamp_t date, int offset, struct strbuf *buf) |
01c6ad29 LT |
790 | { |
791 | int sign = '+'; | |
792 | ||
793 | if (offset < 0) { | |
794 | offset = -offset; | |
795 | sign = '-'; | |
796 | } | |
cb71f8bd | 797 | strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60); |
01c6ad29 LT |
798 | } |
799 | ||
116eb3ab JH |
800 | /* |
801 | * Parse a string like "0 +0000" as ancient timestamp near epoch, but | |
802 | * only when it appears not as part of any other string. | |
803 | */ | |
dddbad72 | 804 | static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset) |
116eb3ab JH |
805 | { |
806 | char *end; | |
dddbad72 | 807 | timestamp_t stamp; |
116eb3ab JH |
808 | int ofs; |
809 | ||
be21d167 | 810 | if (*date < '0' || '9' < *date) |
116eb3ab | 811 | return -1; |
1aeb7e75 | 812 | stamp = parse_timestamp(date, &end, 10); |
dddbad72 | 813 | if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-')) |
116eb3ab JH |
814 | return -1; |
815 | date = end + 2; | |
816 | ofs = strtol(date, &end, 10); | |
817 | if ((*end != '\0' && (*end != '\n')) || end != date + 4) | |
818 | return -1; | |
819 | ofs = (ofs / 100) * 60 + (ofs % 100); | |
820 | if (date[-1] == '-') | |
821 | ofs = -ofs; | |
822 | *timestamp = stamp; | |
823 | *offset = ofs; | |
824 | return 0; | |
825 | } | |
826 | ||
89967023 LT |
827 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
828 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
dddbad72 | 829 | int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset) |
89967023 LT |
830 | { |
831 | struct tm tm; | |
c5043cc1 | 832 | int tm_gmt; |
dddbad72 | 833 | timestamp_t dummy_timestamp; |
c5043cc1 RR |
834 | int dummy_offset; |
835 | ||
836 | if (!timestamp) | |
837 | timestamp = &dummy_timestamp; | |
838 | if (!offset) | |
839 | offset = &dummy_offset; | |
ecee9d9e | 840 | |
89967023 LT |
841 | memset(&tm, 0, sizeof(tm)); |
842 | tm.tm_year = -1; | |
843 | tm.tm_mon = -1; | |
844 | tm.tm_mday = -1; | |
eaa85129 | 845 | tm.tm_isdst = -1; |
36e4986f LT |
846 | tm.tm_hour = -1; |
847 | tm.tm_min = -1; | |
848 | tm.tm_sec = -1; | |
c5043cc1 | 849 | *offset = -1; |
9cb480f2 | 850 | tm_gmt = 0; |
89967023 | 851 | |
2c733fb2 JH |
852 | if (*date == '@' && |
853 | !match_object_header_date(date + 1, timestamp, offset)) | |
116eb3ab | 854 | return 0; /* success */ |
89967023 LT |
855 | for (;;) { |
856 | int match = 0; | |
857 | unsigned char c = *date; | |
858 | ||
859 | /* Stop at end of string or newline */ | |
860 | if (!c || c == '\n') | |
861 | break; | |
862 | ||
863 | if (isalpha(c)) | |
c5043cc1 | 864 | match = match_alpha(date, &tm, offset); |
89967023 | 865 | else if (isdigit(c)) |
c5043cc1 | 866 | match = match_digit(date, &tm, offset, &tm_gmt); |
89967023 | 867 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
c5043cc1 | 868 | match = match_tz(date, offset); |
89967023 LT |
869 | |
870 | if (!match) { | |
871 | /* BAD CRAP */ | |
872 | match = 1; | |
a6080a0a | 873 | } |
89967023 LT |
874 | |
875 | date += match; | |
876 | } | |
ecee9d9e | 877 | |
f6e63621 | 878 | /* do not use mktime(), which uses local timezone, here */ |
c5043cc1 | 879 | *timestamp = tm_to_time_t(&tm); |
7fcec48d JH |
880 | if (*timestamp == -1) |
881 | return -1; | |
882 | ||
e1033da6 | 883 | if (*offset == -1) { |
f6e63621 JH |
884 | time_t temp_time; |
885 | ||
886 | /* gmtime_r() in match_digit() may have clobbered it */ | |
887 | tm.tm_isdst = -1; | |
888 | temp_time = mktime(&tm); | |
e1033da6 MG |
889 | if ((time_t)*timestamp > temp_time) { |
890 | *offset = ((time_t)*timestamp - temp_time) / 60; | |
891 | } else { | |
892 | *offset = -(int)((temp_time - (time_t)*timestamp) / 60); | |
893 | } | |
894 | } | |
eaa85129 | 895 | |
9cb480f2 | 896 | if (!tm_gmt) |
c5043cc1 | 897 | *timestamp -= *offset * 60; |
9644c061 | 898 | return 0; /* success */ |
c5043cc1 RR |
899 | } |
900 | ||
dddbad72 | 901 | int parse_expiry_date(const char *date, timestamp_t *timestamp) |
3d27b9b0 JH |
902 | { |
903 | int errors = 0; | |
904 | ||
905 | if (!strcmp(date, "never") || !strcmp(date, "false")) | |
906 | *timestamp = 0; | |
907 | else if (!strcmp(date, "all") || !strcmp(date, "now")) | |
908 | /* | |
909 | * We take over "now" here, which usually translates | |
910 | * to the current timestamp. This is because the user | |
0e20b229 | 911 | * really means to expire everything that was done in |
3d27b9b0 JH |
912 | * the past, and by definition reflogs are the record |
913 | * of the past, and there is nothing from the future | |
914 | * to be kept. | |
915 | */ | |
dddbad72 | 916 | *timestamp = TIME_MAX; |
3d27b9b0 JH |
917 | else |
918 | *timestamp = approxidate_careful(date, &errors); | |
919 | ||
920 | return errors; | |
921 | } | |
922 | ||
c33ddc2e | 923 | int parse_date(const char *date, struct strbuf *result) |
c5043cc1 | 924 | { |
dddbad72 | 925 | timestamp_t timestamp; |
c5043cc1 | 926 | int offset; |
9644c061 | 927 | if (parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 928 | return -1; |
c33ddc2e JK |
929 | date_string(timestamp, offset, result); |
930 | return 0; | |
ecee9d9e ET |
931 | } |
932 | ||
add00ba2 JK |
933 | static enum date_mode_type parse_date_type(const char *format, const char **end) |
934 | { | |
935 | if (skip_prefix(format, "relative", end)) | |
936 | return DATE_RELATIVE; | |
937 | if (skip_prefix(format, "iso8601-strict", end) || | |
938 | skip_prefix(format, "iso-strict", end)) | |
939 | return DATE_ISO8601_STRICT; | |
940 | if (skip_prefix(format, "iso8601", end) || | |
941 | skip_prefix(format, "iso", end)) | |
942 | return DATE_ISO8601; | |
943 | if (skip_prefix(format, "rfc2822", end) || | |
944 | skip_prefix(format, "rfc", end)) | |
945 | return DATE_RFC2822; | |
946 | if (skip_prefix(format, "short", end)) | |
947 | return DATE_SHORT; | |
948 | if (skip_prefix(format, "default", end)) | |
949 | return DATE_NORMAL; | |
acdd3776 LT |
950 | if (skip_prefix(format, "human", end)) |
951 | return DATE_HUMAN; | |
add00ba2 JK |
952 | if (skip_prefix(format, "raw", end)) |
953 | return DATE_RAW; | |
642833db JK |
954 | if (skip_prefix(format, "unix", end)) |
955 | return DATE_UNIX; | |
add00ba2 JK |
956 | if (skip_prefix(format, "format", end)) |
957 | return DATE_STRFTIME; | |
5a59a230 NTND |
958 | /* |
959 | * Please update $__git_log_date_formats in | |
960 | * git-completion.bash when you add new formats. | |
961 | */ | |
add00ba2 JK |
962 | |
963 | die("unknown date format %s", format); | |
964 | } | |
965 | ||
a5481a6c | 966 | void parse_date_format(const char *format, struct date_mode *mode) |
856665f8 | 967 | { |
add00ba2 JK |
968 | const char *p; |
969 | ||
2fd7c229 SS |
970 | /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */ |
971 | if (skip_prefix(format, "auto:", &p)) { | |
972 | if (isatty(1) || pager_in_use()) | |
973 | format = p; | |
974 | else | |
975 | format = "default"; | |
976 | } | |
977 | ||
add00ba2 JK |
978 | /* historical alias */ |
979 | if (!strcmp(format, "local")) | |
980 | format = "default-local"; | |
981 | ||
982 | mode->type = parse_date_type(format, &p); | |
983 | mode->local = 0; | |
984 | ||
985 | if (skip_prefix(p, "-local", &p)) | |
986 | mode->local = 1; | |
987 | ||
988 | if (mode->type == DATE_STRFTIME) { | |
989 | if (!skip_prefix(p, ":", &p)) | |
990 | die("date format missing colon separator: %s", format); | |
991 | mode->strftime_fmt = xstrdup(p); | |
992 | } else if (*p) | |
856665f8 AP |
993 | die("unknown date format %s", format); |
994 | } | |
995 | ||
974c919d ÆAB |
996 | void date_mode_release(struct date_mode *mode) |
997 | { | |
998 | free((char *)mode->strftime_fmt); | |
999 | } | |
1000 | ||
c33ddc2e | 1001 | void datestamp(struct strbuf *out) |
ecee9d9e ET |
1002 | { |
1003 | time_t now; | |
1004 | int offset; | |
ccd46945 | 1005 | struct tm tm = { 0 }; |
ecee9d9e ET |
1006 | |
1007 | time(&now); | |
1008 | ||
ccd46945 | 1009 | offset = tm_to_time_t(localtime_r(&now, &tm)) - now; |
ecee9d9e ET |
1010 | offset /= 60; |
1011 | ||
c33ddc2e | 1012 | date_string(now, offset, out); |
ecee9d9e | 1013 | } |
3c07b1d1 | 1014 | |
90290552 LT |
1015 | /* |
1016 | * Relative time update (eg "2 days ago"). If we haven't set the time | |
1017 | * yet, we need to set it from current time. | |
1018 | */ | |
dddbad72 | 1019 | static time_t update_tm(struct tm *tm, struct tm *now, time_t sec) |
3c07b1d1 | 1020 | { |
90290552 LT |
1021 | time_t n; |
1022 | ||
1023 | if (tm->tm_mday < 0) | |
1024 | tm->tm_mday = now->tm_mday; | |
1025 | if (tm->tm_mon < 0) | |
1026 | tm->tm_mon = now->tm_mon; | |
1027 | if (tm->tm_year < 0) { | |
1028 | tm->tm_year = now->tm_year; | |
1029 | if (tm->tm_mon > now->tm_mon) | |
1030 | tm->tm_year--; | |
1031 | } | |
1032 | ||
1033 | n = mktime(tm) - sec; | |
3c07b1d1 | 1034 | localtime_r(&n, tm); |
90290552 | 1035 | return n; |
3c07b1d1 LT |
1036 | } |
1037 | ||
c27cc94f JK |
1038 | /* |
1039 | * Do we have a pending number at the end, or when | |
1040 | * we see a new one? Let's assume it's a month day, | |
1041 | * as in "Dec 6, 1992" | |
1042 | */ | |
1043 | static void pending_number(struct tm *tm, int *num) | |
1044 | { | |
1045 | int number = *num; | |
1046 | ||
1047 | if (number) { | |
1048 | *num = 0; | |
1049 | if (tm->tm_mday < 0 && number < 32) | |
1050 | tm->tm_mday = number; | |
1051 | else if (tm->tm_mon < 0 && number < 13) | |
1052 | tm->tm_mon = number-1; | |
1053 | else if (tm->tm_year < 0) { | |
1054 | if (number > 1969 && number < 2100) | |
1055 | tm->tm_year = number - 1900; | |
1056 | else if (number > 69 && number < 100) | |
1057 | tm->tm_year = number; | |
1058 | else if (number < 38) | |
1059 | tm->tm_year = 100 + number; | |
1060 | /* We screw up for number = 00 ? */ | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | ||
93cfa7c7 JH |
1065 | static void date_now(struct tm *tm, struct tm *now, int *num) |
1066 | { | |
c27cc94f | 1067 | *num = 0; |
93cfa7c7 JH |
1068 | update_tm(tm, now, 0); |
1069 | } | |
1070 | ||
90290552 | 1071 | static void date_yesterday(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 1072 | { |
c27cc94f | 1073 | *num = 0; |
90290552 | 1074 | update_tm(tm, now, 24*60*60); |
a8aca418 LT |
1075 | } |
1076 | ||
90290552 | 1077 | static void date_time(struct tm *tm, struct tm *now, int hour) |
a8aca418 LT |
1078 | { |
1079 | if (tm->tm_hour < hour) | |
aa097b88 | 1080 | update_tm(tm, now, 24*60*60); |
a8aca418 LT |
1081 | tm->tm_hour = hour; |
1082 | tm->tm_min = 0; | |
1083 | tm->tm_sec = 0; | |
1084 | } | |
1085 | ||
90290552 | 1086 | static void date_midnight(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 1087 | { |
c27cc94f | 1088 | pending_number(tm, num); |
90290552 | 1089 | date_time(tm, now, 0); |
a8aca418 LT |
1090 | } |
1091 | ||
90290552 | 1092 | static void date_noon(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 1093 | { |
c27cc94f | 1094 | pending_number(tm, num); |
90290552 | 1095 | date_time(tm, now, 12); |
a8aca418 LT |
1096 | } |
1097 | ||
90290552 | 1098 | static void date_tea(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 1099 | { |
c27cc94f | 1100 | pending_number(tm, num); |
90290552 | 1101 | date_time(tm, now, 17); |
a8aca418 LT |
1102 | } |
1103 | ||
90290552 | 1104 | static void date_pm(struct tm *tm, struct tm *now, int *num) |
393d340e | 1105 | { |
18b633ca | 1106 | int hour, n = *num; |
393d340e LT |
1107 | *num = 0; |
1108 | ||
18b633ca LT |
1109 | hour = tm->tm_hour; |
1110 | if (n) { | |
1111 | hour = n; | |
393d340e LT |
1112 | tm->tm_min = 0; |
1113 | tm->tm_sec = 0; | |
1114 | } | |
18b633ca | 1115 | tm->tm_hour = (hour % 12) + 12; |
393d340e LT |
1116 | } |
1117 | ||
90290552 | 1118 | static void date_am(struct tm *tm, struct tm *now, int *num) |
393d340e | 1119 | { |
18b633ca | 1120 | int hour, n = *num; |
393d340e LT |
1121 | *num = 0; |
1122 | ||
18b633ca LT |
1123 | hour = tm->tm_hour; |
1124 | if (n) { | |
1125 | hour = n; | |
393d340e LT |
1126 | tm->tm_min = 0; |
1127 | tm->tm_sec = 0; | |
1128 | } | |
18b633ca | 1129 | tm->tm_hour = (hour % 12); |
393d340e LT |
1130 | } |
1131 | ||
90290552 | 1132 | static void date_never(struct tm *tm, struct tm *now, int *num) |
af66366a | 1133 | { |
8c6b5786 OM |
1134 | time_t n = 0; |
1135 | localtime_r(&n, tm); | |
c27cc94f | 1136 | *num = 0; |
af66366a JS |
1137 | } |
1138 | ||
a8aca418 LT |
1139 | static const struct special { |
1140 | const char *name; | |
90290552 | 1141 | void (*fn)(struct tm *, struct tm *, int *); |
a8aca418 LT |
1142 | } special[] = { |
1143 | { "yesterday", date_yesterday }, | |
1144 | { "noon", date_noon }, | |
1145 | { "midnight", date_midnight }, | |
1146 | { "tea", date_tea }, | |
393d340e LT |
1147 | { "PM", date_pm }, |
1148 | { "AM", date_am }, | |
af66366a | 1149 | { "never", date_never }, |
93cfa7c7 | 1150 | { "now", date_now }, |
a8aca418 LT |
1151 | { NULL } |
1152 | }; | |
1153 | ||
3c07b1d1 LT |
1154 | static const char *number_name[] = { |
1155 | "zero", "one", "two", "three", "four", | |
1156 | "five", "six", "seven", "eight", "nine", "ten", | |
1157 | }; | |
1158 | ||
a8aca418 | 1159 | static const struct typelen { |
3c07b1d1 LT |
1160 | const char *type; |
1161 | int length; | |
1162 | } typelen[] = { | |
1163 | { "seconds", 1 }, | |
1164 | { "minutes", 60 }, | |
1165 | { "hours", 60*60 }, | |
1166 | { "days", 24*60*60 }, | |
1167 | { "weeks", 7*24*60*60 }, | |
1168 | { NULL } | |
a6080a0a | 1169 | }; |
3c07b1d1 | 1170 | |
93cfa7c7 | 1171 | static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) |
3c07b1d1 | 1172 | { |
a8aca418 LT |
1173 | const struct typelen *tl; |
1174 | const struct special *s; | |
3c07b1d1 | 1175 | const char *end = date; |
5df7dbba | 1176 | int i; |
3c07b1d1 | 1177 | |
38db01b7 | 1178 | while (isalpha(*++end)) |
5df7dbba | 1179 | ; |
3c07b1d1 LT |
1180 | |
1181 | for (i = 0; i < 12; i++) { | |
1182 | int match = match_string(date, month_names[i]); | |
1183 | if (match >= 3) { | |
1184 | tm->tm_mon = i; | |
93cfa7c7 | 1185 | *touched = 1; |
3c07b1d1 LT |
1186 | return end; |
1187 | } | |
1188 | } | |
1189 | ||
a8aca418 LT |
1190 | for (s = special; s->name; s++) { |
1191 | int len = strlen(s->name); | |
1192 | if (match_string(date, s->name) == len) { | |
90290552 | 1193 | s->fn(tm, now, num); |
93cfa7c7 | 1194 | *touched = 1; |
a8aca418 LT |
1195 | return end; |
1196 | } | |
3c07b1d1 LT |
1197 | } |
1198 | ||
1199 | if (!*num) { | |
1200 | for (i = 1; i < 11; i++) { | |
1201 | int len = strlen(number_name[i]); | |
1202 | if (match_string(date, number_name[i]) == len) { | |
1203 | *num = i; | |
93cfa7c7 | 1204 | *touched = 1; |
3c07b1d1 LT |
1205 | return end; |
1206 | } | |
1207 | } | |
93cfa7c7 | 1208 | if (match_string(date, "last") == 4) { |
3c07b1d1 | 1209 | *num = 1; |
93cfa7c7 JH |
1210 | *touched = 1; |
1211 | } | |
3c07b1d1 LT |
1212 | return end; |
1213 | } | |
1214 | ||
1215 | tl = typelen; | |
1216 | while (tl->type) { | |
1217 | int len = strlen(tl->type); | |
1218 | if (match_string(date, tl->type) >= len-1) { | |
90290552 | 1219 | update_tm(tm, now, tl->length * *num); |
3c07b1d1 | 1220 | *num = 0; |
93cfa7c7 | 1221 | *touched = 1; |
3c07b1d1 LT |
1222 | return end; |
1223 | } | |
1224 | tl++; | |
1225 | } | |
1226 | ||
6b7b0427 LT |
1227 | for (i = 0; i < 7; i++) { |
1228 | int match = match_string(date, weekday_names[i]); | |
1229 | if (match >= 3) { | |
1230 | int diff, n = *num -1; | |
1231 | *num = 0; | |
1232 | ||
1233 | diff = tm->tm_wday - i; | |
1234 | if (diff <= 0) | |
1235 | n++; | |
1236 | diff += 7*n; | |
1237 | ||
90290552 | 1238 | update_tm(tm, now, diff * 24 * 60 * 60); |
93cfa7c7 | 1239 | *touched = 1; |
6b7b0427 LT |
1240 | return end; |
1241 | } | |
1242 | } | |
1243 | ||
3c07b1d1 | 1244 | if (match_string(date, "months") >= 5) { |
931e8e27 JK |
1245 | int n; |
1246 | update_tm(tm, now, 0); /* fill in date fields if needed */ | |
1247 | n = tm->tm_mon - *num; | |
3c07b1d1 LT |
1248 | *num = 0; |
1249 | while (n < 0) { | |
1250 | n += 12; | |
1251 | tm->tm_year--; | |
1252 | } | |
1253 | tm->tm_mon = n; | |
93cfa7c7 | 1254 | *touched = 1; |
3c07b1d1 LT |
1255 | return end; |
1256 | } | |
1257 | ||
1258 | if (match_string(date, "years") >= 4) { | |
931e8e27 | 1259 | update_tm(tm, now, 0); /* fill in date fields if needed */ |
3c07b1d1 LT |
1260 | tm->tm_year -= *num; |
1261 | *num = 0; | |
93cfa7c7 | 1262 | *touched = 1; |
3c07b1d1 LT |
1263 | return end; |
1264 | } | |
1265 | ||
1266 | return end; | |
1267 | } | |
1268 | ||
073281e2 JK |
1269 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num, |
1270 | time_t now) | |
e92a54d9 LT |
1271 | { |
1272 | char *end; | |
dddbad72 | 1273 | timestamp_t number = parse_timestamp(date, &end, 10); |
e92a54d9 | 1274 | |
393d340e LT |
1275 | switch (*end) { |
1276 | case ':': | |
1277 | case '.': | |
1278 | case '/': | |
1279 | case '-': | |
1280 | if (isdigit(end[1])) { | |
073281e2 JK |
1281 | int match = match_multi_number(number, *end, date, end, |
1282 | tm, now); | |
393d340e LT |
1283 | if (match) |
1284 | return date + match; | |
1285 | } | |
1286 | } | |
1287 | ||
9f2b6d29 LT |
1288 | /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ |
1289 | if (date[0] != '0' || end - date <= 2) | |
1290 | *num = number; | |
e92a54d9 LT |
1291 | return end; |
1292 | } | |
1293 | ||
dddbad72 JS |
1294 | static timestamp_t approxidate_str(const char *date, |
1295 | const struct timeval *tv, | |
1296 | int *error_ret) | |
3c07b1d1 LT |
1297 | { |
1298 | int number = 0; | |
93cfa7c7 | 1299 | int touched = 0; |
3c07b1d1 | 1300 | struct tm tm, now; |
f697b33b | 1301 | time_t time_sec; |
3c07b1d1 | 1302 | |
33012fc4 | 1303 | time_sec = tv->tv_sec; |
f697b33b | 1304 | localtime_r(&time_sec, &tm); |
3c07b1d1 | 1305 | now = tm; |
90290552 LT |
1306 | |
1307 | tm.tm_year = -1; | |
1308 | tm.tm_mon = -1; | |
1309 | tm.tm_mday = -1; | |
1310 | ||
3c07b1d1 LT |
1311 | for (;;) { |
1312 | unsigned char c = *date; | |
1313 | if (!c) | |
1314 | break; | |
1315 | date++; | |
1316 | if (isdigit(c)) { | |
90290552 | 1317 | pending_number(&tm, &number); |
073281e2 | 1318 | date = approxidate_digit(date-1, &tm, &number, time_sec); |
93cfa7c7 | 1319 | touched = 1; |
3c07b1d1 LT |
1320 | continue; |
1321 | } | |
1322 | if (isalpha(c)) | |
93cfa7c7 | 1323 | date = approxidate_alpha(date-1, &tm, &now, &number, &touched); |
3c07b1d1 | 1324 | } |
90290552 | 1325 | pending_number(&tm, &number); |
93cfa7c7 JH |
1326 | if (!touched) |
1327 | *error_ret = 1; | |
dddbad72 | 1328 | return (timestamp_t)update_tm(&tm, &now, 0); |
3c07b1d1 | 1329 | } |
33012fc4 | 1330 | |
29f4332e | 1331 | timestamp_t approxidate_relative(const char *date) |
33012fc4 | 1332 | { |
29f4332e | 1333 | struct timeval tv; |
dddbad72 | 1334 | timestamp_t timestamp; |
c5043cc1 | 1335 | int offset; |
93cfa7c7 | 1336 | int errors = 0; |
33012fc4 | 1337 | |
9644c061 | 1338 | if (!parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 1339 | return timestamp; |
29f4332e SS |
1340 | |
1341 | get_time(&tv); | |
1342 | return approxidate_str(date, (const struct timeval *) &tv, &errors); | |
33012fc4 AR |
1343 | } |
1344 | ||
dddbad72 | 1345 | timestamp_t approxidate_careful(const char *date, int *error_ret) |
33012fc4 AR |
1346 | { |
1347 | struct timeval tv; | |
dddbad72 | 1348 | timestamp_t timestamp; |
c5043cc1 | 1349 | int offset; |
93cfa7c7 JH |
1350 | int dummy = 0; |
1351 | if (!error_ret) | |
1352 | error_ret = &dummy; | |
33012fc4 | 1353 | |
9644c061 | 1354 | if (!parse_date_basic(date, ×tamp, &offset)) { |
93cfa7c7 | 1355 | *error_ret = 0; |
c5043cc1 | 1356 | return timestamp; |
93cfa7c7 | 1357 | } |
33012fc4 | 1358 | |
b841d4ff | 1359 | get_time(&tv); |
93cfa7c7 | 1360 | return approxidate_str(date, &tv, error_ret); |
33012fc4 | 1361 | } |
7ca36d93 | 1362 | |
dddbad72 | 1363 | int date_overflows(timestamp_t t) |
7ca36d93 JK |
1364 | { |
1365 | time_t sys; | |
1366 | ||
dddbad72 JS |
1367 | /* If we overflowed our timestamp data type, that's bad... */ |
1368 | if ((uintmax_t)t >= TIME_MAX) | |
7ca36d93 JK |
1369 | return 1; |
1370 | ||
1371 | /* | |
1372 | * ...but we also are going to feed the result to system | |
1373 | * functions that expect time_t, which is often "signed long". | |
1374 | * Make sure that we fit into time_t, as well. | |
1375 | */ | |
1376 | sys = t; | |
1377 | return t != sys || (t < 1) != (sys < 1); | |
1378 | } |