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