THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1335 lines
30KB

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