Coverage Report

Created: 2024-09-08 06:23

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