Coverage Report

Created: 2026-01-09 07:10

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