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