date.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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, struct tm *tm)
  59. {
  60. time_t t = gm_time_t(time, tz);
  61. return gmtime_r(&t, tm);
  62. }
  63. static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
  64. {
  65. time_t t = time;
  66. return localtime_r(&t, tm);
  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 tmbuf = { 0 };
  258. struct tm human_tm = { 0 };
  259. int human_tz = -1;
  260. static struct strbuf timebuf = STRBUF_INIT;
  261. if (mode->type == DATE_UNIX) {
  262. strbuf_reset(&timebuf);
  263. strbuf_addf(&timebuf, "%"PRItime, time);
  264. return timebuf.buf;
  265. }
  266. if (mode->type == DATE_HUMAN) {
  267. struct timeval now;
  268. get_time(&now);
  269. /* Fill in the data for "current time" in human_tz and human_tm */
  270. human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
  271. }
  272. if (mode->local)
  273. tz = local_tzoffset(time);
  274. if (mode->type == DATE_RAW) {
  275. strbuf_reset(&timebuf);
  276. strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
  277. return timebuf.buf;
  278. }
  279. if (mode->type == DATE_RELATIVE) {
  280. strbuf_reset(&timebuf);
  281. show_date_relative(time, &timebuf);
  282. return timebuf.buf;
  283. }
  284. if (mode->local)
  285. tm = time_to_tm_local(time, &tmbuf);
  286. else
  287. tm = time_to_tm(time, tz, &tmbuf);
  288. if (!tm) {
  289. tm = time_to_tm(0, 0, &tmbuf);
  290. tz = 0;
  291. }
  292. strbuf_reset(&timebuf);
  293. if (mode->type == DATE_SHORT)
  294. strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
  295. tm->tm_mon + 1, tm->tm_mday);
  296. else if (mode->type == DATE_ISO8601)
  297. strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
  298. tm->tm_year + 1900,
  299. tm->tm_mon + 1,
  300. tm->tm_mday,
  301. tm->tm_hour, tm->tm_min, tm->tm_sec,
  302. tz);
  303. else if (mode->type == DATE_ISO8601_STRICT) {
  304. char sign = (tz >= 0) ? '+' : '-';
  305. tz = abs(tz);
  306. strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
  307. tm->tm_year + 1900,
  308. tm->tm_mon + 1,
  309. tm->tm_mday,
  310. tm->tm_hour, tm->tm_min, tm->tm_sec,
  311. sign, tz / 100, tz % 100);
  312. } else if (mode->type == DATE_RFC2822)
  313. strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
  314. weekday_names[tm->tm_wday], tm->tm_mday,
  315. month_names[tm->tm_mon], tm->tm_year + 1900,
  316. tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
  317. else if (mode->type == DATE_STRFTIME)
  318. strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
  319. !mode->local);
  320. else
  321. show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
  322. return timebuf.buf;
  323. }
  324. /*
  325. * Check these. And note how it doesn't do the summer-time conversion.
  326. *
  327. * In my world, it's always summer, and things are probably a bit off
  328. * in other ways too.
  329. */
  330. static const struct {
  331. const char *name;
  332. int offset;
  333. int dst;
  334. } timezone_names[] = {
  335. { "IDLW", -12, 0, }, /* International Date Line West */
  336. { "NT", -11, 0, }, /* Nome */
  337. { "CAT", -10, 0, }, /* Central Alaska */
  338. { "HST", -10, 0, }, /* Hawaii Standard */
  339. { "HDT", -10, 1, }, /* Hawaii Daylight */
  340. { "YST", -9, 0, }, /* Yukon Standard */
  341. { "YDT", -9, 1, }, /* Yukon Daylight */
  342. { "PST", -8, 0, }, /* Pacific Standard */
  343. { "PDT", -8, 1, }, /* Pacific Daylight */
  344. { "MST", -7, 0, }, /* Mountain Standard */
  345. { "MDT", -7, 1, }, /* Mountain Daylight */
  346. { "CST", -6, 0, }, /* Central Standard */
  347. { "CDT", -6, 1, }, /* Central Daylight */
  348. { "EST", -5, 0, }, /* Eastern Standard */
  349. { "EDT", -5, 1, }, /* Eastern Daylight */
  350. { "AST", -3, 0, }, /* Atlantic Standard */
  351. { "ADT", -3, 1, }, /* Atlantic Daylight */
  352. { "WAT", -1, 0, }, /* West Africa */
  353. { "GMT", 0, 0, }, /* Greenwich Mean */
  354. { "UTC", 0, 0, }, /* Universal (Coordinated) */
  355. { "Z", 0, 0, }, /* Zulu, alias for UTC */
  356. { "WET", 0, 0, }, /* Western European */
  357. { "BST", 0, 1, }, /* British Summer */
  358. { "CET", +1, 0, }, /* Central European */
  359. { "MET", +1, 0, }, /* Middle European */
  360. { "MEWT", +1, 0, }, /* Middle European Winter */
  361. { "MEST", +1, 1, }, /* Middle European Summer */
  362. { "CEST", +1, 1, }, /* Central European Summer */
  363. { "MESZ", +1, 1, }, /* Middle European Summer */
  364. { "FWT", +1, 0, }, /* French Winter */
  365. { "FST", +1, 1, }, /* French Summer */
  366. { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
  367. { "EEST", +2, 1, }, /* Eastern European Daylight */
  368. { "WAST", +7, 0, }, /* West Australian Standard */
  369. { "WADT", +7, 1, }, /* West Australian Daylight */
  370. { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
  371. { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
  372. { "EAST", +10, 0, }, /* Eastern Australian Standard */
  373. { "EADT", +10, 1, }, /* Eastern Australian Daylight */
  374. { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
  375. { "NZT", +12, 0, }, /* New Zealand */
  376. { "NZST", +12, 0, }, /* New Zealand Standard */
  377. { "NZDT", +12, 1, }, /* New Zealand Daylight */
  378. { "IDLE", +12, 0, }, /* International Date Line East */
  379. };
  380. static int match_string(const char *date, const char *str)
  381. {
  382. int i = 0;
  383. for (i = 0; *date; date++, str++, i++) {
  384. if (*date == *str)
  385. continue;
  386. if (toupper(*date) == toupper(*str))
  387. continue;
  388. if (!isalnum(*date))
  389. break;
  390. return 0;
  391. }
  392. return i;
  393. }
  394. static int skip_alpha(const char *date)
  395. {
  396. int i = 0;
  397. do {
  398. i++;
  399. } while (isalpha(date[i]));
  400. return i;
  401. }
  402. /*
  403. * Parse month, weekday, or timezone name
  404. */
  405. static int match_alpha(const char *date, struct tm *tm, int *offset)
  406. {
  407. int i;
  408. for (i = 0; i < 12; i++) {
  409. int match = match_string(date, month_names[i]);
  410. if (match >= 3) {
  411. tm->tm_mon = i;
  412. return match;
  413. }
  414. }
  415. for (i = 0; i < 7; i++) {
  416. int match = match_string(date, weekday_names[i]);
  417. if (match >= 3) {
  418. tm->tm_wday = i;
  419. return match;
  420. }
  421. }
  422. for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
  423. int match = match_string(date, timezone_names[i].name);
  424. if (match >= 3 || match == strlen(timezone_names[i].name)) {
  425. int off = timezone_names[i].offset;
  426. /* This is bogus, but we like summer */
  427. off += timezone_names[i].dst;
  428. /* Only use the tz name offset if we don't have anything better */
  429. if (*offset == -1)
  430. *offset = 60*off;
  431. return match;
  432. }
  433. }
  434. if (match_string(date, "PM") == 2) {
  435. tm->tm_hour = (tm->tm_hour % 12) + 12;
  436. return 2;
  437. }
  438. if (match_string(date, "AM") == 2) {
  439. tm->tm_hour = (tm->tm_hour % 12) + 0;
  440. return 2;
  441. }
  442. /* BAD CRAP */
  443. return skip_alpha(date);
  444. }
  445. static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
  446. {
  447. if (month > 0 && month < 13 && day > 0 && day < 32) {
  448. struct tm check = *tm;
  449. struct tm *r = (now_tm ? &check : tm);
  450. time_t specified;
  451. r->tm_mon = month - 1;
  452. r->tm_mday = day;
  453. if (year == -1) {
  454. if (!now_tm)
  455. return 1;
  456. r->tm_year = now_tm->tm_year;
  457. }
  458. else if (year >= 1970 && year < 2100)
  459. r->tm_year = year - 1900;
  460. else if (year > 70 && year < 100)
  461. r->tm_year = year;
  462. else if (year < 38)
  463. r->tm_year = year + 100;
  464. else
  465. return -1;
  466. if (!now_tm)
  467. return 0;
  468. specified = tm_to_time_t(r);
  469. /* Be it commit time or author time, it does not make
  470. * sense to specify timestamp way into the future. Make
  471. * sure it is not later than ten days from now...
  472. */
  473. if ((specified != -1) && (now + 10*24*3600 < specified))
  474. return -1;
  475. tm->tm_mon = r->tm_mon;
  476. tm->tm_mday = r->tm_mday;
  477. if (year != -1)
  478. tm->tm_year = r->tm_year;
  479. return 0;
  480. }
  481. return -1;
  482. }
  483. static int set_time(long hour, long minute, long second, struct tm *tm)
  484. {
  485. /* We accept 61st second because of leap second */
  486. if (0 <= hour && hour <= 24 &&
  487. 0 <= minute && minute < 60 &&
  488. 0 <= second && second <= 60) {
  489. tm->tm_hour = hour;
  490. tm->tm_min = minute;
  491. tm->tm_sec = second;
  492. return 0;
  493. }
  494. return -1;
  495. }
  496. static int is_date_known(struct tm *tm)
  497. {
  498. return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;
  499. }
  500. static int match_multi_number(timestamp_t num, char c, const char *date,
  501. char *end, struct tm *tm, time_t now)
  502. {
  503. struct tm now_tm;
  504. struct tm *refuse_future;
  505. long num2, num3;
  506. num2 = strtol(end+1, &end, 10);
  507. num3 = -1;
  508. if (*end == c && isdigit(end[1]))
  509. num3 = strtol(end+1, &end, 10);
  510. /* Time? Date? */
  511. switch (c) {
  512. case ':':
  513. if (num3 < 0)
  514. num3 = 0;
  515. if (set_time(num, num2, num3, tm) == 0) {
  516. /*
  517. * If %H:%M:%S was just parsed followed by: .<num4>
  518. * Consider (& discard) it as fractional second
  519. * if %Y%m%d is parsed before.
  520. */
  521. if (*end == '.' && isdigit(end[1]) && is_date_known(tm))
  522. strtol(end + 1, &end, 10);
  523. break;
  524. }
  525. return 0;
  526. case '-':
  527. case '/':
  528. case '.':
  529. if (!now)
  530. now = time(NULL);
  531. refuse_future = NULL;
  532. if (gmtime_r(&now, &now_tm))
  533. refuse_future = &now_tm;
  534. if (num > 70) {
  535. /* yyyy-mm-dd? */
  536. if (set_date(num, num2, num3, NULL, now, tm) == 0)
  537. break;
  538. /* yyyy-dd-mm? */
  539. if (set_date(num, num3, num2, NULL, now, tm) == 0)
  540. break;
  541. }
  542. /* Our eastern European friends say dd.mm.yy[yy]
  543. * is the norm there, so giving precedence to
  544. * mm/dd/yy[yy] form only when separator is not '.'
  545. */
  546. if (c != '.' &&
  547. set_date(num3, num, num2, refuse_future, now, tm) == 0)
  548. break;
  549. /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
  550. if (set_date(num3, num2, num, refuse_future, now, tm) == 0)
  551. break;
  552. /* Funny European mm.dd.yy */
  553. if (c == '.' &&
  554. set_date(num3, num, num2, refuse_future, now, tm) == 0)
  555. break;
  556. return 0;
  557. }
  558. return end - date;
  559. }
  560. /*
  561. * Have we filled in any part of the time/date yet?
  562. * We just do a binary 'and' to see if the sign bit
  563. * is set in all the values.
  564. */
  565. static inline int nodate(struct tm *tm)
  566. {
  567. return (tm->tm_year &
  568. tm->tm_mon &
  569. tm->tm_mday &
  570. tm->tm_hour &
  571. tm->tm_min &
  572. tm->tm_sec) < 0;
  573. }
  574. /*
  575. * We've seen a digit. Time? Year? Date?
  576. */
  577. static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
  578. {
  579. int n;
  580. char *end;
  581. timestamp_t num;
  582. num = parse_timestamp(date, &end, 10);
  583. /*
  584. * Seconds since 1970? We trigger on that for any numbers with
  585. * more than 8 digits. This is because we don't want to rule out
  586. * numbers like 20070606 as a YYYYMMDD date.
  587. */
  588. if (num >= 100000000 && nodate(tm)) {
  589. time_t time = num;
  590. if (gmtime_r(&time, tm)) {
  591. *tm_gmt = 1;
  592. return end - date;
  593. }
  594. }
  595. /*
  596. * Check for special formats: num[-.:/]num[same]num
  597. */
  598. switch (*end) {
  599. case ':':
  600. case '.':
  601. case '/':
  602. case '-':
  603. if (isdigit(end[1])) {
  604. int match = match_multi_number(num, *end, date, end, tm, 0);
  605. if (match)
  606. return match;
  607. }
  608. }
  609. /*
  610. * None of the special formats? Try to guess what
  611. * the number meant. We use the number of digits
  612. * to make a more educated guess..
  613. */
  614. n = 0;
  615. do {
  616. n++;
  617. } while (isdigit(date[n]));
  618. /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
  619. /* 6 digits, compact style of ISO-8601's time: HHMMSS */
  620. if (n == 8 || n == 6) {
  621. unsigned int num1 = num / 10000;
  622. unsigned int num2 = (num % 10000) / 100;
  623. unsigned int num3 = num % 100;
  624. if (n == 8)
  625. set_date(num1, num2, num3, NULL, time(NULL), tm);
  626. else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&
  627. *end == '.' && isdigit(end[1]))
  628. strtoul(end + 1, &end, 10);
  629. return end - date;
  630. }
  631. /* Four-digit year or a timezone? */
  632. if (n == 4) {
  633. if (num <= 1400 && *offset == -1) {
  634. unsigned int minutes = num % 100;
  635. unsigned int hours = num / 100;
  636. *offset = hours*60 + minutes;
  637. } else if (num > 1900 && num < 2100)
  638. tm->tm_year = num - 1900;
  639. return n;
  640. }
  641. /*
  642. * Ignore lots of numerals. We took care of 4-digit years above.
  643. * Days or months must be one or two digits.
  644. */
  645. if (n > 2)
  646. return n;
  647. /*
  648. * NOTE! We will give precedence to day-of-month over month or
  649. * year numbers in the 1-12 range. So 05 is always "mday 5",
  650. * unless we already have a mday..
  651. *
  652. * IOW, 01 Apr 05 parses as "April 1st, 2005".
  653. */
  654. if (num > 0 && num < 32 && tm->tm_mday < 0) {
  655. tm->tm_mday = num;
  656. return n;
  657. }
  658. /* Two-digit year? */
  659. if (n == 2 && tm->tm_year < 0) {
  660. if (num < 10 && tm->tm_mday >= 0) {
  661. tm->tm_year = num + 100;
  662. return n;
  663. }
  664. if (num >= 70) {
  665. tm->tm_year = num;
  666. return n;
  667. }
  668. }
  669. if (num > 0 && num < 13 && tm->tm_mon < 0)
  670. tm->tm_mon = num-1;
  671. return n;
  672. }
  673. static int match_tz(const char *date, int *offp)
  674. {
  675. char *end;
  676. int hour = strtoul(date + 1, &end, 10);
  677. int n = end - (date + 1);
  678. int min = 0;
  679. if (n == 4) {
  680. /* hhmm */
  681. min = hour % 100;
  682. hour = hour / 100;
  683. } else if (n != 2) {
  684. min = 99; /* random crap */
  685. } else if (*end == ':') {
  686. /* hh:mm? */
  687. min = strtoul(end + 1, &end, 10);
  688. if (end - (date + 1) != 5)
  689. min = 99; /* random crap */
  690. } /* otherwise we parsed "hh" */
  691. /*
  692. * Don't accept any random crap. Even though some places have
  693. * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
  694. * UTC+14), there is something wrong if hour part is much
  695. * larger than that. We might also want to check that the
  696. * minutes are divisible by 15 or something too. (Offset of
  697. * Kathmandu, Nepal is UTC+5:45)
  698. */
  699. if (min < 60 && hour < 24) {
  700. int offset = hour * 60 + min;
  701. if (*date == '-')
  702. offset = -offset;
  703. *offp = offset;
  704. }
  705. return end - date;
  706. }
  707. static void date_string(timestamp_t date, int offset, struct strbuf *buf)
  708. {
  709. int sign = '+';
  710. if (offset < 0) {
  711. offset = -offset;
  712. sign = '-';
  713. }
  714. strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
  715. }
  716. /*
  717. * Parse a string like "0 +0000" as ancient timestamp near epoch, but
  718. * only when it appears not as part of any other string.
  719. */
  720. static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
  721. {
  722. char *end;
  723. timestamp_t stamp;
  724. int ofs;
  725. if (*date < '0' || '9' < *date)
  726. return -1;
  727. stamp = parse_timestamp(date, &end, 10);
  728. if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
  729. return -1;
  730. date = end + 2;
  731. ofs = strtol(date, &end, 10);
  732. if ((*end != '\0' && (*end != '\n')) || end != date + 4)
  733. return -1;
  734. ofs = (ofs / 100) * 60 + (ofs % 100);
  735. if (date[-1] == '-')
  736. ofs = -ofs;
  737. *timestamp = stamp;
  738. *offset = ofs;
  739. return 0;
  740. }
  741. /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
  742. (i.e. English) day/month names, and it doesn't work correctly with %z. */
  743. int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
  744. {
  745. struct tm tm;
  746. int tm_gmt;
  747. timestamp_t dummy_timestamp;
  748. int dummy_offset;
  749. if (!timestamp)
  750. timestamp = &dummy_timestamp;
  751. if (!offset)
  752. offset = &dummy_offset;
  753. memset(&tm, 0, sizeof(tm));
  754. tm.tm_year = -1;
  755. tm.tm_mon = -1;
  756. tm.tm_mday = -1;
  757. tm.tm_isdst = -1;
  758. tm.tm_hour = -1;
  759. tm.tm_min = -1;
  760. tm.tm_sec = -1;
  761. *offset = -1;
  762. tm_gmt = 0;
  763. if (*date == '@' &&
  764. !match_object_header_date(date + 1, timestamp, offset))
  765. return 0; /* success */
  766. for (;;) {
  767. int match = 0;
  768. unsigned char c = *date;
  769. /* Stop at end of string or newline */
  770. if (!c || c == '\n')
  771. break;
  772. if (isalpha(c))
  773. match = match_alpha(date, &tm, offset);
  774. else if (isdigit(c))
  775. match = match_digit(date, &tm, offset, &tm_gmt);
  776. else if ((c == '-' || c == '+') && isdigit(date[1]))
  777. match = match_tz(date, offset);
  778. if (!match) {
  779. /* BAD CRAP */
  780. match = 1;
  781. }
  782. date += match;
  783. }
  784. /* do not use mktime(), which uses local timezone, here */
  785. *timestamp = tm_to_time_t(&tm);
  786. if (*timestamp == -1)
  787. return -1;
  788. if (*offset == -1) {
  789. time_t temp_time;
  790. /* gmtime_r() in match_digit() may have clobbered it */
  791. tm.tm_isdst = -1;
  792. temp_time = mktime(&tm);
  793. if ((time_t)*timestamp > temp_time) {
  794. *offset = ((time_t)*timestamp - temp_time) / 60;
  795. } else {
  796. *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
  797. }
  798. }
  799. if (!tm_gmt)
  800. *timestamp -= *offset * 60;
  801. return 0; /* success */
  802. }
  803. int parse_expiry_date(const char *date, timestamp_t *timestamp)
  804. {
  805. int errors = 0;
  806. if (!strcmp(date, "never") || !strcmp(date, "false"))
  807. *timestamp = 0;
  808. else if (!strcmp(date, "all") || !strcmp(date, "now"))
  809. /*
  810. * We take over "now" here, which usually translates
  811. * to the current timestamp. This is because the user
  812. * really means to expire everything she has done in
  813. * the past, and by definition reflogs are the record
  814. * of the past, and there is nothing from the future
  815. * to be kept.
  816. */
  817. *timestamp = TIME_MAX;
  818. else
  819. *timestamp = approxidate_careful(date, &errors);
  820. return errors;
  821. }
  822. int parse_date(const char *date, struct strbuf *result)
  823. {
  824. timestamp_t timestamp;
  825. int offset;
  826. if (parse_date_basic(date, &timestamp, &offset))
  827. return -1;
  828. date_string(timestamp, offset, result);
  829. return 0;
  830. }
  831. static enum date_mode_type parse_date_type(const char *format, const char **end)
  832. {
  833. if (skip_prefix(format, "relative", end))
  834. return DATE_RELATIVE;
  835. if (skip_prefix(format, "iso8601-strict", end) ||
  836. skip_prefix(format, "iso-strict", end))
  837. return DATE_ISO8601_STRICT;
  838. if (skip_prefix(format, "iso8601", end) ||
  839. skip_prefix(format, "iso", end))
  840. return DATE_ISO8601;
  841. if (skip_prefix(format, "rfc2822", end) ||
  842. skip_prefix(format, "rfc", end))
  843. return DATE_RFC2822;
  844. if (skip_prefix(format, "short", end))
  845. return DATE_SHORT;
  846. if (skip_prefix(format, "default", end))
  847. return DATE_NORMAL;
  848. if (skip_prefix(format, "human", end))
  849. return DATE_HUMAN;
  850. if (skip_prefix(format, "raw", end))
  851. return DATE_RAW;
  852. if (skip_prefix(format, "unix", end))
  853. return DATE_UNIX;
  854. if (skip_prefix(format, "format", end))
  855. return DATE_STRFTIME;
  856. /*
  857. * Please update $__git_log_date_formats in
  858. * git-completion.bash when you add new formats.
  859. */
  860. die("unknown date format %s", format);
  861. }
  862. void parse_date_format(const char *format, struct date_mode *mode)
  863. {
  864. const char *p;
  865. /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
  866. if (skip_prefix(format, "auto:", &p)) {
  867. if (isatty(1) || pager_in_use())
  868. format = p;
  869. else
  870. format = "default";
  871. }
  872. /* historical alias */
  873. if (!strcmp(format, "local"))
  874. format = "default-local";
  875. mode->type = parse_date_type(format, &p);
  876. mode->local = 0;
  877. if (skip_prefix(p, "-local", &p))
  878. mode->local = 1;
  879. if (mode->type == DATE_STRFTIME) {
  880. if (!skip_prefix(p, ":", &p))
  881. die("date format missing colon separator: %s", format);
  882. mode->strftime_fmt = xstrdup(p);
  883. } else if (*p)
  884. die("unknown date format %s", format);
  885. }
  886. void datestamp(struct strbuf *out)
  887. {
  888. time_t now;
  889. int offset;
  890. struct tm tm = { 0 };
  891. time(&now);
  892. offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
  893. offset /= 60;
  894. date_string(now, offset, out);
  895. }
  896. /*
  897. * Relative time update (eg "2 days ago"). If we haven't set the time
  898. * yet, we need to set it from current time.
  899. */
  900. static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
  901. {
  902. time_t n;
  903. if (tm->tm_mday < 0)
  904. tm->tm_mday = now->tm_mday;
  905. if (tm->tm_mon < 0)
  906. tm->tm_mon = now->tm_mon;
  907. if (tm->tm_year < 0) {
  908. tm->tm_year = now->tm_year;
  909. if (tm->tm_mon > now->tm_mon)
  910. tm->tm_year--;
  911. }
  912. n = mktime(tm) - sec;
  913. localtime_r(&n, tm);
  914. return n;
  915. }
  916. /*
  917. * Do we have a pending number at the end, or when
  918. * we see a new one? Let's assume it's a month day,
  919. * as in "Dec 6, 1992"
  920. */
  921. static void pending_number(struct tm *tm, int *num)
  922. {
  923. int number = *num;
  924. if (number) {
  925. *num = 0;
  926. if (tm->tm_mday < 0 && number < 32)
  927. tm->tm_mday = number;
  928. else if (tm->tm_mon < 0 && number < 13)
  929. tm->tm_mon = number-1;
  930. else if (tm->tm_year < 0) {
  931. if (number > 1969 && number < 2100)
  932. tm->tm_year = number - 1900;
  933. else if (number > 69 && number < 100)
  934. tm->tm_year = number;
  935. else if (number < 38)
  936. tm->tm_year = 100 + number;
  937. /* We screw up for number = 00 ? */
  938. }
  939. }
  940. }
  941. static void date_now(struct tm *tm, struct tm *now, int *num)
  942. {
  943. *num = 0;
  944. update_tm(tm, now, 0);
  945. }
  946. static void date_yesterday(struct tm *tm, struct tm *now, int *num)
  947. {
  948. *num = 0;
  949. update_tm(tm, now, 24*60*60);
  950. }
  951. static void date_time(struct tm *tm, struct tm *now, int hour)
  952. {
  953. if (tm->tm_hour < hour)
  954. update_tm(tm, now, 24*60*60);
  955. tm->tm_hour = hour;
  956. tm->tm_min = 0;
  957. tm->tm_sec = 0;
  958. }
  959. static void date_midnight(struct tm *tm, struct tm *now, int *num)
  960. {
  961. pending_number(tm, num);
  962. date_time(tm, now, 0);
  963. }
  964. static void date_noon(struct tm *tm, struct tm *now, int *num)
  965. {
  966. pending_number(tm, num);
  967. date_time(tm, now, 12);
  968. }
  969. static void date_tea(struct tm *tm, struct tm *now, int *num)
  970. {
  971. pending_number(tm, num);
  972. date_time(tm, now, 17);
  973. }
  974. static void date_pm(struct tm *tm, struct tm *now, int *num)
  975. {
  976. int hour, n = *num;
  977. *num = 0;
  978. hour = tm->tm_hour;
  979. if (n) {
  980. hour = n;
  981. tm->tm_min = 0;
  982. tm->tm_sec = 0;
  983. }
  984. tm->tm_hour = (hour % 12) + 12;
  985. }
  986. static void date_am(struct tm *tm, struct tm *now, int *num)
  987. {
  988. int hour, n = *num;
  989. *num = 0;
  990. hour = tm->tm_hour;
  991. if (n) {
  992. hour = n;
  993. tm->tm_min = 0;
  994. tm->tm_sec = 0;
  995. }
  996. tm->tm_hour = (hour % 12);
  997. }
  998. static void date_never(struct tm *tm, struct tm *now, int *num)
  999. {
  1000. time_t n = 0;
  1001. localtime_r(&n, tm);
  1002. *num = 0;
  1003. }
  1004. static const struct special {
  1005. const char *name;
  1006. void (*fn)(struct tm *, struct tm *, int *);
  1007. } special[] = {
  1008. { "yesterday", date_yesterday },
  1009. { "noon", date_noon },
  1010. { "midnight", date_midnight },
  1011. { "tea", date_tea },
  1012. { "PM", date_pm },
  1013. { "AM", date_am },
  1014. { "never", date_never },
  1015. { "now", date_now },
  1016. { NULL }
  1017. };
  1018. static const char *number_name[] = {
  1019. "zero", "one", "two", "three", "four",
  1020. "five", "six", "seven", "eight", "nine", "ten",
  1021. };
  1022. static const struct typelen {
  1023. const char *type;
  1024. int length;
  1025. } typelen[] = {
  1026. { "seconds", 1 },
  1027. { "minutes", 60 },
  1028. { "hours", 60*60 },
  1029. { "days", 24*60*60 },
  1030. { "weeks", 7*24*60*60 },
  1031. { NULL }
  1032. };
  1033. static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
  1034. {
  1035. const struct typelen *tl;
  1036. const struct special *s;
  1037. const char *end = date;
  1038. int i;
  1039. while (isalpha(*++end))
  1040. ;
  1041. for (i = 0; i < 12; i++) {
  1042. int match = match_string(date, month_names[i]);
  1043. if (match >= 3) {
  1044. tm->tm_mon = i;
  1045. *touched = 1;
  1046. return end;
  1047. }
  1048. }
  1049. for (s = special; s->name; s++) {
  1050. int len = strlen(s->name);
  1051. if (match_string(date, s->name) == len) {
  1052. s->fn(tm, now, num);
  1053. *touched = 1;
  1054. return end;
  1055. }
  1056. }
  1057. if (!*num) {
  1058. for (i = 1; i < 11; i++) {
  1059. int len = strlen(number_name[i]);
  1060. if (match_string(date, number_name[i]) == len) {
  1061. *num = i;
  1062. *touched = 1;
  1063. return end;
  1064. }
  1065. }
  1066. if (match_string(date, "last") == 4) {
  1067. *num = 1;
  1068. *touched = 1;
  1069. }
  1070. return end;
  1071. }
  1072. tl = typelen;
  1073. while (tl->type) {
  1074. int len = strlen(tl->type);
  1075. if (match_string(date, tl->type) >= len-1) {
  1076. update_tm(tm, now, tl->length * *num);
  1077. *num = 0;
  1078. *touched = 1;
  1079. return end;
  1080. }
  1081. tl++;
  1082. }
  1083. for (i = 0; i < 7; i++) {
  1084. int match = match_string(date, weekday_names[i]);
  1085. if (match >= 3) {
  1086. int diff, n = *num -1;
  1087. *num = 0;
  1088. diff = tm->tm_wday - i;
  1089. if (diff <= 0)
  1090. n++;
  1091. diff += 7*n;
  1092. update_tm(tm, now, diff * 24 * 60 * 60);
  1093. *touched = 1;
  1094. return end;
  1095. }
  1096. }
  1097. if (match_string(date, "months") >= 5) {
  1098. int n;
  1099. update_tm(tm, now, 0); /* fill in date fields if needed */
  1100. n = tm->tm_mon - *num;
  1101. *num = 0;
  1102. while (n < 0) {
  1103. n += 12;
  1104. tm->tm_year--;
  1105. }
  1106. tm->tm_mon = n;
  1107. *touched = 1;
  1108. return end;
  1109. }
  1110. if (match_string(date, "years") >= 4) {
  1111. update_tm(tm, now, 0); /* fill in date fields if needed */
  1112. tm->tm_year -= *num;
  1113. *num = 0;
  1114. *touched = 1;
  1115. return end;
  1116. }
  1117. return end;
  1118. }
  1119. static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
  1120. time_t now)
  1121. {
  1122. char *end;
  1123. timestamp_t number = parse_timestamp(date, &end, 10);
  1124. switch (*end) {
  1125. case ':':
  1126. case '.':
  1127. case '/':
  1128. case '-':
  1129. if (isdigit(end[1])) {
  1130. int match = match_multi_number(number, *end, date, end,
  1131. tm, now);
  1132. if (match)
  1133. return date + match;
  1134. }
  1135. }
  1136. /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
  1137. if (date[0] != '0' || end - date <= 2)
  1138. *num = number;
  1139. return end;
  1140. }
  1141. static timestamp_t approxidate_str(const char *date,
  1142. const struct timeval *tv,
  1143. int *error_ret)
  1144. {
  1145. int number = 0;
  1146. int touched = 0;
  1147. struct tm tm, now;
  1148. time_t time_sec;
  1149. time_sec = tv->tv_sec;
  1150. localtime_r(&time_sec, &tm);
  1151. now = tm;
  1152. tm.tm_year = -1;
  1153. tm.tm_mon = -1;
  1154. tm.tm_mday = -1;
  1155. for (;;) {
  1156. unsigned char c = *date;
  1157. if (!c)
  1158. break;
  1159. date++;
  1160. if (isdigit(c)) {
  1161. pending_number(&tm, &number);
  1162. date = approxidate_digit(date-1, &tm, &number, time_sec);
  1163. touched = 1;
  1164. continue;
  1165. }
  1166. if (isalpha(c))
  1167. date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
  1168. }
  1169. pending_number(&tm, &number);
  1170. if (!touched)
  1171. *error_ret = 1;
  1172. return (timestamp_t)update_tm(&tm, &now, 0);
  1173. }
  1174. timestamp_t approxidate_relative(const char *date)
  1175. {
  1176. struct timeval tv;
  1177. timestamp_t timestamp;
  1178. int offset;
  1179. int errors = 0;
  1180. if (!parse_date_basic(date, &timestamp, &offset))
  1181. return timestamp;
  1182. get_time(&tv);
  1183. return approxidate_str(date, (const struct timeval *) &tv, &errors);
  1184. }
  1185. timestamp_t approxidate_careful(const char *date, int *error_ret)
  1186. {
  1187. struct timeval tv;
  1188. timestamp_t timestamp;
  1189. int offset;
  1190. int dummy = 0;
  1191. if (!error_ret)
  1192. error_ret = &dummy;
  1193. if (!parse_date_basic(date, &timestamp, &offset)) {
  1194. *error_ret = 0;
  1195. return timestamp;
  1196. }
  1197. get_time(&tv);
  1198. return approxidate_str(date, &tv, error_ret);
  1199. }
  1200. int date_overflows(timestamp_t t)
  1201. {
  1202. time_t sys;
  1203. /* If we overflowed our timestamp data type, that's bad... */
  1204. if ((uintmax_t)t >= TIME_MAX)
  1205. return 1;
  1206. /*
  1207. * ...but we also are going to feed the result to system
  1208. * functions that expect time_t, which is often "signed long".
  1209. * Make sure that we fit into time_t, as well.
  1210. */
  1211. sys = t;
  1212. return t != sys || (t < 1) != (sys < 1);
  1213. }