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