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