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 | |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 9 | static time_t my_mktime(struct tm *tm) |
| 10 | { |
| 11 | static const int mdays[] = { |
| 12 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 13 | }; |
| 14 | int year = tm->tm_year - 70; |
| 15 | int month = tm->tm_mon; |
| 16 | int day = tm->tm_mday; |
| 17 | |
| 18 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ |
| 19 | return -1; |
| 20 | if (month < 0 || month > 11) /* array bounds */ |
| 21 | return -1; |
| 22 | if (month < 2 || (year + 2) % 4) |
| 23 | day--; |
| 24 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
| 25 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; |
| 26 | } |
| 27 | |
| 28 | static const char *month_names[] = { |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 29 | "January", "February", "March", "April", "May", "June", |
| 30 | "July", "August", "September", "October", "November", "December" |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static const char *weekday_names[] = { |
Linus Torvalds | 6b7b042 | 2005-11-17 12:36:30 -0800 | [diff] [blame] | 34 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 37 | static time_t gm_time_t(unsigned long time, int tz) |
| 38 | { |
| 39 | int minutes; |
| 40 | |
| 41 | minutes = tz < 0 ? -tz : tz; |
| 42 | minutes = (minutes / 100)*60 + (minutes % 100); |
| 43 | minutes = tz < 0 ? -minutes : minutes; |
| 44 | return time + minutes * 60; |
| 45 | } |
| 46 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 47 | /* |
Linus Torvalds | f80cd78 | 2005-05-06 15:28:59 -0700 | [diff] [blame] | 48 | * The "tz" thing is passed in as this strange "decimal parse of tz" |
| 49 | * thing, which means that tz -0100 is passed in as the integer -100, |
| 50 | * even though it means "sixty minutes off" |
| 51 | */ |
Junio C Hamano | 2a38704 | 2006-05-01 01:44:33 -0700 | [diff] [blame] | 52 | static struct tm *time_to_tm(unsigned long time, int tz) |
Linus Torvalds | f80cd78 | 2005-05-06 15:28:59 -0700 | [diff] [blame] | 53 | { |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 54 | time_t t = gm_time_t(time, tz); |
Junio C Hamano | 2a38704 | 2006-05-01 01:44:33 -0700 | [diff] [blame] | 55 | return gmtime(&t); |
| 56 | } |
| 57 | |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 58 | /* |
| 59 | * What value of "tz" was in effect back then at "time" in the |
| 60 | * local timezone? |
| 61 | */ |
| 62 | static int local_tzoffset(unsigned long time) |
| 63 | { |
| 64 | time_t t, t_local; |
| 65 | struct tm tm; |
| 66 | int offset, eastwest; |
| 67 | |
| 68 | t = time; |
| 69 | localtime_r(&t, &tm); |
| 70 | t_local = my_mktime(&tm); |
| 71 | |
| 72 | if (t_local < t) { |
| 73 | eastwest = -1; |
| 74 | offset = t - t_local; |
| 75 | } else { |
| 76 | eastwest = 1; |
| 77 | offset = t_local - t; |
| 78 | } |
| 79 | offset /= 60; /* in minutes */ |
| 80 | offset = (offset % 60) + ((offset / 60) * 100); |
| 81 | return offset * eastwest; |
| 82 | } |
| 83 | |
Johannes Schindelin | f8493ec | 2007-02-27 16:21:04 +0100 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | struct tm *tm; |
| 87 | static char timebuf[200]; |
| 88 | |
Johannes Schindelin | f8493ec | 2007-02-27 16:21:04 +0100 | [diff] [blame] | 89 | if (mode == DATE_RELATIVE) { |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 90 | unsigned long diff; |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 91 | struct timeval now; |
| 92 | gettimeofday(&now, NULL); |
Johannes Schindelin | da8f070 | 2007-01-20 22:21:38 +0100 | [diff] [blame] | 93 | if (now.tv_sec < time) |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 94 | return "in the future"; |
Johannes Schindelin | da8f070 | 2007-01-20 22:21:38 +0100 | [diff] [blame] | 95 | diff = now.tv_sec - time; |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 96 | if (diff < 90) { |
| 97 | snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff); |
| 98 | return timebuf; |
| 99 | } |
| 100 | /* Turn it into minutes */ |
| 101 | diff = (diff + 30) / 60; |
| 102 | if (diff < 90) { |
| 103 | snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff); |
| 104 | return timebuf; |
| 105 | } |
| 106 | /* Turn it into hours */ |
| 107 | diff = (diff + 30) / 60; |
| 108 | if (diff < 36) { |
| 109 | snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff); |
| 110 | return timebuf; |
| 111 | } |
| 112 | /* We deal with number of days from here on */ |
| 113 | diff = (diff + 12) / 24; |
| 114 | if (diff < 14) { |
| 115 | snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff); |
| 116 | return timebuf; |
| 117 | } |
| 118 | /* Say weeks for the past 10 weeks or so */ |
| 119 | if (diff < 70) { |
| 120 | snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7); |
| 121 | return timebuf; |
| 122 | } |
| 123 | /* Say months for the past 12 months or so */ |
| 124 | if (diff < 360) { |
| 125 | snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30); |
| 126 | return timebuf; |
| 127 | } |
| 128 | /* Else fall back on absolute format.. */ |
| 129 | } |
| 130 | |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 131 | if (mode == DATE_LOCAL) |
| 132 | tz = local_tzoffset(time); |
| 133 | |
Junio C Hamano | 2a38704 | 2006-05-01 01:44:33 -0700 | [diff] [blame] | 134 | tm = time_to_tm(time, tz); |
Linus Torvalds | f80cd78 | 2005-05-06 15:28:59 -0700 | [diff] [blame] | 135 | if (!tm) |
| 136 | return NULL; |
Johannes Schindelin | f8493ec | 2007-02-27 16:21:04 +0100 | [diff] [blame] | 137 | if (mode == DATE_SHORT) |
| 138 | sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, |
| 139 | tm->tm_mon + 1, tm->tm_mday); |
Robin Rosenberg | ee8f838 | 2007-07-14 01:00:42 +0200 | [diff] [blame] | 140 | else if (mode == DATE_ISO8601) |
| 141 | sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", |
| 142 | tm->tm_year + 1900, |
| 143 | tm->tm_mon + 1, |
| 144 | tm->tm_mday, |
| 145 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 146 | tz); |
Junio C Hamano | 73013af | 2007-07-13 23:14:52 -0700 | [diff] [blame] | 147 | else if (mode == DATE_RFC2822) |
| 148 | sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", |
| 149 | weekday_names[tm->tm_wday], tm->tm_mday, |
| 150 | month_names[tm->tm_mon], tm->tm_year + 1900, |
| 151 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); |
Johannes Schindelin | f8493ec | 2007-02-27 16:21:04 +0100 | [diff] [blame] | 152 | else |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 153 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", |
Johannes Schindelin | f8493ec | 2007-02-27 16:21:04 +0100 | [diff] [blame] | 154 | weekday_names[tm->tm_wday], |
| 155 | month_names[tm->tm_mon], |
| 156 | tm->tm_mday, |
| 157 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
Junio C Hamano | a7b02cc | 2007-04-24 23:36:22 -0700 | [diff] [blame] | 158 | tm->tm_year + 1900, |
| 159 | (mode == DATE_LOCAL) ? 0 : ' ', |
| 160 | tz); |
Linus Torvalds | f80cd78 | 2005-05-06 15:28:59 -0700 | [diff] [blame] | 161 | return timebuf; |
| 162 | } |
| 163 | |
| 164 | /* |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 165 | * Check these. And note how it doesn't do the summer-time conversion. |
| 166 | * |
| 167 | * In my world, it's always summer, and things are probably a bit off |
| 168 | * in other ways too. |
| 169 | */ |
| 170 | static const struct { |
| 171 | const char *name; |
| 172 | int offset; |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 173 | int dst; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 174 | } timezone_names[] = { |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 175 | { "IDLW", -12, 0, }, /* International Date Line West */ |
| 176 | { "NT", -11, 0, }, /* Nome */ |
| 177 | { "CAT", -10, 0, }, /* Central Alaska */ |
| 178 | { "HST", -10, 0, }, /* Hawaii Standard */ |
| 179 | { "HDT", -10, 1, }, /* Hawaii Daylight */ |
| 180 | { "YST", -9, 0, }, /* Yukon Standard */ |
| 181 | { "YDT", -9, 1, }, /* Yukon Daylight */ |
| 182 | { "PST", -8, 0, }, /* Pacific Standard */ |
| 183 | { "PDT", -8, 1, }, /* Pacific Daylight */ |
| 184 | { "MST", -7, 0, }, /* Mountain Standard */ |
| 185 | { "MDT", -7, 1, }, /* Mountain Daylight */ |
| 186 | { "CST", -6, 0, }, /* Central Standard */ |
| 187 | { "CDT", -6, 1, }, /* Central Daylight */ |
| 188 | { "EST", -5, 0, }, /* Eastern Standard */ |
| 189 | { "EDT", -5, 1, }, /* Eastern Daylight */ |
| 190 | { "AST", -3, 0, }, /* Atlantic Standard */ |
| 191 | { "ADT", -3, 1, }, /* Atlantic Daylight */ |
| 192 | { "WAT", -1, 0, }, /* West Africa */ |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 193 | |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 194 | { "GMT", 0, 0, }, /* Greenwich Mean */ |
| 195 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 196 | |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 197 | { "WET", 0, 0, }, /* Western European */ |
| 198 | { "BST", 0, 1, }, /* British Summer */ |
| 199 | { "CET", +1, 0, }, /* Central European */ |
| 200 | { "MET", +1, 0, }, /* Middle European */ |
| 201 | { "MEWT", +1, 0, }, /* Middle European Winter */ |
| 202 | { "MEST", +1, 1, }, /* Middle European Summer */ |
| 203 | { "CEST", +1, 1, }, /* Central European Summer */ |
| 204 | { "MESZ", +1, 1, }, /* Middle European Summer */ |
| 205 | { "FWT", +1, 0, }, /* French Winter */ |
| 206 | { "FST", +1, 1, }, /* French Summer */ |
| 207 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ |
Linus Torvalds | 92e2311 | 2005-04-30 15:21:57 -0700 | [diff] [blame] | 208 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 209 | { "WAST", +7, 0, }, /* West Australian Standard */ |
| 210 | { "WADT", +7, 1, }, /* West Australian Daylight */ |
| 211 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ |
| 212 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ |
| 213 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ |
| 214 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ |
| 215 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ |
Steven Drake | 695ed47 | 2008-02-26 13:45:53 +1300 | [diff] [blame] | 216 | { "NZT", +12, 0, }, /* New Zealand */ |
| 217 | { "NZST", +12, 0, }, /* New Zealand Standard */ |
| 218 | { "NZDT", +12, 1, }, /* New Zealand Daylight */ |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 219 | { "IDLE", +12, 0, }, /* International Date Line East */ |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 222 | static int match_string(const char *date, const char *str) |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 223 | { |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 224 | int i = 0; |
| 225 | |
| 226 | for (i = 0; *date; date++, str++, i++) { |
| 227 | if (*date == *str) |
| 228 | continue; |
| 229 | if (toupper(*date) == toupper(*str)) |
| 230 | continue; |
| 231 | if (!isalnum(*date)) |
| 232 | break; |
| 233 | return 0; |
| 234 | } |
| 235 | return i; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 238 | static int skip_alpha(const char *date) |
| 239 | { |
| 240 | int i = 0; |
| 241 | do { |
| 242 | i++; |
| 243 | } while (isalpha(date[i])); |
| 244 | return i; |
| 245 | } |
| 246 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 247 | /* |
| 248 | * Parse month, weekday, or timezone name |
| 249 | */ |
| 250 | static int match_alpha(const char *date, struct tm *tm, int *offset) |
| 251 | { |
| 252 | int i; |
| 253 | |
| 254 | for (i = 0; i < 12; i++) { |
| 255 | int match = match_string(date, month_names[i]); |
| 256 | if (match >= 3) { |
| 257 | tm->tm_mon = i; |
| 258 | return match; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | for (i = 0; i < 7; i++) { |
| 263 | int match = match_string(date, weekday_names[i]); |
| 264 | if (match >= 3) { |
| 265 | tm->tm_wday = i; |
| 266 | return match; |
| 267 | } |
| 268 | } |
| 269 | |
Junio C Hamano | b4f2a6a | 2006-03-09 11:58:05 -0800 | [diff] [blame] | 270 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 271 | int match = match_string(date, timezone_names[i].name); |
| 272 | if (match >= 3) { |
Linus Torvalds | 5e2a78a | 2005-04-30 14:53:12 -0700 | [diff] [blame] | 273 | int off = timezone_names[i].offset; |
| 274 | |
| 275 | /* This is bogus, but we like summer */ |
| 276 | off += timezone_names[i].dst; |
| 277 | |
Linus Torvalds | 92e2311 | 2005-04-30 15:21:57 -0700 | [diff] [blame] | 278 | /* Only use the tz name offset if we don't have anything better */ |
| 279 | if (*offset == -1) |
| 280 | *offset = 60*off; |
| 281 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 282 | return match; |
| 283 | } |
| 284 | } |
| 285 | |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 286 | if (match_string(date, "PM") == 2) { |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 287 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
| 288 | return 2; |
| 289 | } |
| 290 | |
| 291 | if (match_string(date, "AM") == 2) { |
| 292 | tm->tm_hour = (tm->tm_hour % 12) + 0; |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 293 | return 2; |
| 294 | } |
| 295 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 296 | /* BAD CRAP */ |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 297 | return skip_alpha(date); |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 300 | 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] | 301 | { |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 302 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 303 | struct tm check = *tm; |
| 304 | struct tm *r = (now_tm ? &check : tm); |
| 305 | time_t specified; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 306 | |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 307 | r->tm_mon = month - 1; |
| 308 | r->tm_mday = day; |
| 309 | if (year == -1) { |
| 310 | if (!now_tm) |
| 311 | return 1; |
| 312 | r->tm_year = now_tm->tm_year; |
| 313 | } |
| 314 | else if (year >= 1970 && year < 2100) |
| 315 | r->tm_year = year - 1900; |
| 316 | else if (year > 70 && year < 100) |
| 317 | r->tm_year = year; |
| 318 | else if (year < 38) |
| 319 | r->tm_year = year + 100; |
| 320 | else |
| 321 | return 0; |
| 322 | if (!now_tm) |
| 323 | return 1; |
| 324 | |
| 325 | specified = my_mktime(r); |
| 326 | |
| 327 | /* Be it commit time or author time, it does not make |
| 328 | * sense to specify timestamp way into the future. Make |
| 329 | * sure it is not later than ten days from now... |
| 330 | */ |
| 331 | if (now + 10*24*3600 < specified) |
| 332 | return 0; |
| 333 | tm->tm_mon = r->tm_mon; |
| 334 | tm->tm_mday = r->tm_mday; |
| 335 | if (year != -1) |
| 336 | tm->tm_year = r->tm_year; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 337 | return 1; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 338 | } |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | } |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | 26a2d8a | 2005-07-12 10:33:06 -0700 | [diff] [blame] | 342 | 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] | 343 | { |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 344 | time_t now; |
| 345 | struct tm now_tm; |
| 346 | struct tm *refuse_future; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 347 | long num2, num3; |
| 348 | |
| 349 | num2 = strtol(end+1, &end, 10); |
| 350 | num3 = -1; |
| 351 | if (*end == c && isdigit(end[1])) |
| 352 | num3 = strtol(end+1, &end, 10); |
| 353 | |
| 354 | /* Time? Date? */ |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 355 | switch (c) { |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 356 | case ':': |
| 357 | if (num3 < 0) |
| 358 | num3 = 0; |
| 359 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { |
| 360 | tm->tm_hour = num; |
| 361 | tm->tm_min = num2; |
| 362 | tm->tm_sec = num3; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 365 | return 0; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 366 | |
| 367 | case '-': |
| 368 | case '/': |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 369 | case '.': |
| 370 | now = time(NULL); |
| 371 | refuse_future = NULL; |
| 372 | if (gmtime_r(&now, &now_tm)) |
| 373 | refuse_future = &now_tm; |
| 374 | |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 375 | if (num > 70) { |
| 376 | /* yyyy-mm-dd? */ |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 377 | if (is_date(num, num2, num3, refuse_future, now, tm)) |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 378 | break; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 379 | /* yyyy-dd-mm? */ |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 380 | if (is_date(num, num3, num2, refuse_future, now, tm)) |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 381 | break; |
| 382 | } |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 383 | /* Our eastern European friends say dd.mm.yy[yy] |
| 384 | * is the norm there, so giving precedence to |
| 385 | * mm/dd/yy[yy] form only when separator is not '.' |
| 386 | */ |
| 387 | if (c != '.' && |
| 388 | is_date(num3, num, num2, refuse_future, now, tm)) |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 389 | break; |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 390 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ |
| 391 | if (is_date(num3, num2, num, refuse_future, now, tm)) |
| 392 | break; |
| 393 | /* Funny European mm.dd.yy */ |
| 394 | if (c == '.' && |
| 395 | is_date(num3, num, num2, refuse_future, now, tm)) |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 396 | break; |
| 397 | return 0; |
| 398 | } |
| 399 | return end - date; |
| 400 | } |
| 401 | |
| 402 | /* |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 403 | * We've seen a digit. Time? Year? Date? |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 404 | */ |
Linus Torvalds | 26a2d8a | 2005-07-12 10:33:06 -0700 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | int n; |
| 408 | char *end; |
| 409 | unsigned long num; |
| 410 | |
| 411 | num = strtoul(date, &end, 10); |
| 412 | |
| 413 | /* |
Johannes Sixt | a1a5a63 | 2007-06-06 10:11:55 +0200 | [diff] [blame] | 414 | * Seconds since 1970? We trigger on that for any numbers with |
| 415 | * more than 8 digits. This is because we don't want to rule out |
| 416 | * numbers like 20070606 as a YYYYMMDD date. |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 417 | */ |
Johannes Sixt | a1a5a63 | 2007-06-06 10:11:55 +0200 | [diff] [blame] | 418 | if (num >= 100000000) { |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 419 | time_t time = num; |
Junio C Hamano | 9cb480f | 2005-06-25 02:21:16 -0700 | [diff] [blame] | 420 | if (gmtime_r(&time, tm)) { |
| 421 | *tm_gmt = 1; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 422 | return end - date; |
Junio C Hamano | 9cb480f | 2005-06-25 02:21:16 -0700 | [diff] [blame] | 423 | } |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 427 | * Check for special formats: num[-.:/]num[same]num |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 428 | */ |
| 429 | switch (*end) { |
| 430 | case ':': |
Junio C Hamano | 38035cf | 2006-04-05 15:31:12 -0700 | [diff] [blame] | 431 | case '.': |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 432 | case '/': |
| 433 | case '-': |
| 434 | if (isdigit(end[1])) { |
| 435 | int match = match_multi_number(num, *end, date, end, tm); |
| 436 | if (match) |
| 437 | return match; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * None of the special formats? Try to guess what |
| 443 | * the number meant. We use the number of digits |
| 444 | * to make a more educated guess.. |
| 445 | */ |
| 446 | n = 0; |
| 447 | do { |
| 448 | n++; |
| 449 | } while (isdigit(date[n])); |
| 450 | |
| 451 | /* Four-digit year or a timezone? */ |
| 452 | if (n == 4) { |
Paul Eggert | 7122f82 | 2006-06-08 14:54:13 -0700 | [diff] [blame] | 453 | if (num <= 1400 && *offset == -1) { |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 454 | unsigned int minutes = num % 100; |
| 455 | unsigned int hours = num / 100; |
| 456 | *offset = hours*60 + minutes; |
| 457 | } else if (num > 1900 && num < 2100) |
| 458 | tm->tm_year = num - 1900; |
| 459 | return n; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * 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] | 464 | * 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] | 465 | * unless we already have a mday.. |
| 466 | * |
| 467 | * IOW, 01 Apr 05 parses as "April 1st, 2005". |
| 468 | */ |
| 469 | if (num > 0 && num < 32 && tm->tm_mday < 0) { |
| 470 | tm->tm_mday = num; |
| 471 | return n; |
| 472 | } |
| 473 | |
| 474 | /* Two-digit year? */ |
| 475 | if (n == 2 && tm->tm_year < 0) { |
| 476 | if (num < 10 && tm->tm_mday >= 0) { |
| 477 | tm->tm_year = num + 100; |
| 478 | return n; |
| 479 | } |
| 480 | if (num >= 70) { |
| 481 | tm->tm_year = num; |
| 482 | return n; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (num > 0 && num < 32) { |
| 487 | tm->tm_mday = num; |
| 488 | } else if (num > 1900) { |
| 489 | tm->tm_year = num - 1900; |
| 490 | } else if (num > 70) { |
| 491 | tm->tm_year = num; |
| 492 | } else if (num > 0 && num < 13) { |
| 493 | tm->tm_mon = num-1; |
| 494 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 495 | |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 496 | return n; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Linus Torvalds | 26a2d8a | 2005-07-12 10:33:06 -0700 | [diff] [blame] | 499 | static int match_tz(const char *date, int *offp) |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 500 | { |
| 501 | char *end; |
| 502 | int offset = strtoul(date+1, &end, 10); |
| 503 | int min, hour; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 504 | int n = end - date - 1; |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 505 | |
| 506 | min = offset % 100; |
| 507 | hour = offset / 100; |
| 508 | |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 509 | /* |
| 510 | * Don't accept any random crap.. At least 3 digits, and |
| 511 | * a valid minute. We might want to check that the minutes |
| 512 | * are divisible by 30 or something too. |
| 513 | */ |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 514 | if (min < 60 && n > 2) { |
| 515 | offset = hour*60+min; |
| 516 | if (*date == '-') |
| 517 | offset = -offset; |
Linus Torvalds | 198b0fb | 2005-05-01 11:48:34 -0700 | [diff] [blame] | 518 | |
Linus Torvalds | 68849b5 | 2005-05-01 12:34:56 -0700 | [diff] [blame] | 519 | *offp = offset; |
| 520 | } |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 521 | return end - date; |
| 522 | } |
| 523 | |
Linus Torvalds | 01c6ad2 | 2005-09-21 15:50:28 -0700 | [diff] [blame] | 524 | static int date_string(unsigned long date, int offset, char *buf, int len) |
| 525 | { |
| 526 | int sign = '+'; |
| 527 | |
| 528 | if (offset < 0) { |
| 529 | offset = -offset; |
| 530 | sign = '-'; |
| 531 | } |
| 532 | return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); |
| 533 | } |
| 534 | |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 535 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
| 536 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ |
Linus Torvalds | 2a39064 | 2005-09-19 15:53:50 -0700 | [diff] [blame] | 537 | int parse_date(const char *date, char *result, int maxlen) |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 538 | { |
| 539 | struct tm tm; |
Linus Torvalds | 01c6ad2 | 2005-09-21 15:50:28 -0700 | [diff] [blame] | 540 | int offset, tm_gmt; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 541 | time_t then; |
| 542 | |
| 543 | memset(&tm, 0, sizeof(tm)); |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 544 | tm.tm_year = -1; |
| 545 | tm.tm_mon = -1; |
| 546 | tm.tm_mday = -1; |
Linus Torvalds | eaa8512 | 2005-04-30 14:25:02 -0700 | [diff] [blame] | 547 | tm.tm_isdst = -1; |
| 548 | offset = -1; |
Junio C Hamano | 9cb480f | 2005-06-25 02:21:16 -0700 | [diff] [blame] | 549 | tm_gmt = 0; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 550 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 551 | for (;;) { |
| 552 | int match = 0; |
| 553 | unsigned char c = *date; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 554 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 555 | /* Stop at end of string or newline */ |
| 556 | if (!c || c == '\n') |
| 557 | break; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 558 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 559 | if (isalpha(c)) |
| 560 | match = match_alpha(date, &tm, &offset); |
| 561 | else if (isdigit(c)) |
Junio C Hamano | 9cb480f | 2005-06-25 02:21:16 -0700 | [diff] [blame] | 562 | match = match_digit(date, &tm, &offset, &tm_gmt); |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 563 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
| 564 | match = match_tz(date, &offset); |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 565 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 566 | if (!match) { |
| 567 | /* BAD CRAP */ |
| 568 | match = 1; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 569 | } |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 570 | |
Linus Torvalds | 8996702 | 2005-04-30 13:19:56 -0700 | [diff] [blame] | 571 | date += match; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 572 | } |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 573 | |
Linus Torvalds | eaa8512 | 2005-04-30 14:25:02 -0700 | [diff] [blame] | 574 | /* mktime uses local timezone */ |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 575 | then = my_mktime(&tm); |
Linus Torvalds | eaa8512 | 2005-04-30 14:25:02 -0700 | [diff] [blame] | 576 | if (offset == -1) |
| 577 | offset = (then - mktime(&tm)) / 60; |
| 578 | |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 579 | if (then == -1) |
Linus Torvalds | 2a39064 | 2005-09-19 15:53:50 -0700 | [diff] [blame] | 580 | return -1; |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 581 | |
Junio C Hamano | 9cb480f | 2005-06-25 02:21:16 -0700 | [diff] [blame] | 582 | if (!tm_gmt) |
| 583 | then -= offset * 60; |
Linus Torvalds | 01c6ad2 | 2005-09-21 15:50:28 -0700 | [diff] [blame] | 584 | return date_string(then, offset, result, maxlen); |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Andy Parkins | 856665f | 2007-09-28 15:17:31 +0100 | [diff] [blame] | 587 | enum date_mode parse_date_format(const char *format) |
| 588 | { |
| 589 | if (!strcmp(format, "relative")) |
| 590 | return DATE_RELATIVE; |
| 591 | else if (!strcmp(format, "iso8601") || |
| 592 | !strcmp(format, "iso")) |
| 593 | return DATE_ISO8601; |
| 594 | else if (!strcmp(format, "rfc2822") || |
| 595 | !strcmp(format, "rfc")) |
| 596 | return DATE_RFC2822; |
| 597 | else if (!strcmp(format, "short")) |
| 598 | return DATE_SHORT; |
| 599 | else if (!strcmp(format, "local")) |
| 600 | return DATE_LOCAL; |
| 601 | else if (!strcmp(format, "default")) |
| 602 | return DATE_NORMAL; |
| 603 | else |
| 604 | die("unknown date format %s", format); |
| 605 | } |
| 606 | |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 607 | void datestamp(char *buf, int bufsize) |
| 608 | { |
| 609 | time_t now; |
| 610 | int offset; |
| 611 | |
| 612 | time(&now); |
| 613 | |
| 614 | offset = my_mktime(localtime(&now)) - now; |
| 615 | offset /= 60; |
| 616 | |
Linus Torvalds | 01c6ad2 | 2005-09-21 15:50:28 -0700 | [diff] [blame] | 617 | date_string(now, offset, buf, bufsize); |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 618 | } |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 619 | |
| 620 | static void update_tm(struct tm *tm, unsigned long sec) |
| 621 | { |
| 622 | time_t n = mktime(tm) - sec; |
| 623 | localtime_r(&n, tm); |
| 624 | } |
| 625 | |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 626 | static void date_yesterday(struct tm *tm, int *num) |
| 627 | { |
| 628 | update_tm(tm, 24*60*60); |
| 629 | } |
| 630 | |
| 631 | static void date_time(struct tm *tm, int hour) |
| 632 | { |
| 633 | if (tm->tm_hour < hour) |
| 634 | date_yesterday(tm, NULL); |
| 635 | tm->tm_hour = hour; |
| 636 | tm->tm_min = 0; |
| 637 | tm->tm_sec = 0; |
| 638 | } |
| 639 | |
| 640 | static void date_midnight(struct tm *tm, int *num) |
| 641 | { |
| 642 | date_time(tm, 0); |
| 643 | } |
| 644 | |
| 645 | static void date_noon(struct tm *tm, int *num) |
| 646 | { |
| 647 | date_time(tm, 12); |
| 648 | } |
| 649 | |
| 650 | static void date_tea(struct tm *tm, int *num) |
| 651 | { |
| 652 | date_time(tm, 17); |
| 653 | } |
| 654 | |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 655 | static void date_pm(struct tm *tm, int *num) |
| 656 | { |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 657 | int hour, n = *num; |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 658 | *num = 0; |
| 659 | |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 660 | hour = tm->tm_hour; |
| 661 | if (n) { |
| 662 | hour = n; |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 663 | tm->tm_min = 0; |
| 664 | tm->tm_sec = 0; |
| 665 | } |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 666 | tm->tm_hour = (hour % 12) + 12; |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static void date_am(struct tm *tm, int *num) |
| 670 | { |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 671 | int hour, n = *num; |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 672 | *num = 0; |
| 673 | |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 674 | hour = tm->tm_hour; |
| 675 | if (n) { |
| 676 | hour = n; |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 677 | tm->tm_min = 0; |
| 678 | tm->tm_sec = 0; |
| 679 | } |
Linus Torvalds | 18b633c | 2006-09-29 12:36:13 -0700 | [diff] [blame] | 680 | tm->tm_hour = (hour % 12); |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Johannes Schindelin | af66366 | 2007-07-24 19:18:34 +0100 | [diff] [blame] | 683 | static void date_never(struct tm *tm, int *num) |
| 684 | { |
| 685 | tm->tm_mon = tm->tm_wday = tm->tm_yday |
| 686 | = tm->tm_hour = tm->tm_min = tm->tm_sec = 0; |
| 687 | tm->tm_year = 70; |
| 688 | tm->tm_mday = 1; |
| 689 | } |
| 690 | |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 691 | static const struct special { |
| 692 | const char *name; |
| 693 | void (*fn)(struct tm *, int *); |
| 694 | } special[] = { |
| 695 | { "yesterday", date_yesterday }, |
| 696 | { "noon", date_noon }, |
| 697 | { "midnight", date_midnight }, |
| 698 | { "tea", date_tea }, |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 699 | { "PM", date_pm }, |
| 700 | { "AM", date_am }, |
Johannes Schindelin | af66366 | 2007-07-24 19:18:34 +0100 | [diff] [blame] | 701 | { "never", date_never }, |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 702 | { NULL } |
| 703 | }; |
| 704 | |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 705 | static const char *number_name[] = { |
| 706 | "zero", "one", "two", "three", "four", |
| 707 | "five", "six", "seven", "eight", "nine", "ten", |
| 708 | }; |
| 709 | |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 710 | static const struct typelen { |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 711 | const char *type; |
| 712 | int length; |
| 713 | } typelen[] = { |
| 714 | { "seconds", 1 }, |
| 715 | { "minutes", 60 }, |
| 716 | { "hours", 60*60 }, |
| 717 | { "days", 24*60*60 }, |
| 718 | { "weeks", 7*24*60*60 }, |
| 719 | { NULL } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 720 | }; |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 721 | |
| 722 | static const char *approxidate_alpha(const char *date, struct tm *tm, int *num) |
| 723 | { |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 724 | const struct typelen *tl; |
| 725 | const struct special *s; |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 726 | const char *end = date; |
Pierre Habouzit | 5df7dbb | 2006-08-23 12:39:16 +0200 | [diff] [blame] | 727 | int i; |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 728 | |
Pierre Habouzit | 5df7dbb | 2006-08-23 12:39:16 +0200 | [diff] [blame] | 729 | while (isalpha(*++end)); |
| 730 | ; |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 731 | |
| 732 | for (i = 0; i < 12; i++) { |
| 733 | int match = match_string(date, month_names[i]); |
| 734 | if (match >= 3) { |
| 735 | tm->tm_mon = i; |
| 736 | return end; |
| 737 | } |
| 738 | } |
| 739 | |
Linus Torvalds | a8aca41 | 2005-11-18 08:56:40 -0800 | [diff] [blame] | 740 | for (s = special; s->name; s++) { |
| 741 | int len = strlen(s->name); |
| 742 | if (match_string(date, s->name) == len) { |
| 743 | s->fn(tm, num); |
| 744 | return end; |
| 745 | } |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | if (!*num) { |
| 749 | for (i = 1; i < 11; i++) { |
| 750 | int len = strlen(number_name[i]); |
| 751 | if (match_string(date, number_name[i]) == len) { |
| 752 | *num = i; |
| 753 | return end; |
| 754 | } |
| 755 | } |
| 756 | if (match_string(date, "last") == 4) |
| 757 | *num = 1; |
| 758 | return end; |
| 759 | } |
| 760 | |
| 761 | tl = typelen; |
| 762 | while (tl->type) { |
| 763 | int len = strlen(tl->type); |
| 764 | if (match_string(date, tl->type) >= len-1) { |
| 765 | update_tm(tm, tl->length * *num); |
| 766 | *num = 0; |
| 767 | return end; |
| 768 | } |
| 769 | tl++; |
| 770 | } |
| 771 | |
Linus Torvalds | 6b7b042 | 2005-11-17 12:36:30 -0800 | [diff] [blame] | 772 | for (i = 0; i < 7; i++) { |
| 773 | int match = match_string(date, weekday_names[i]); |
| 774 | if (match >= 3) { |
| 775 | int diff, n = *num -1; |
| 776 | *num = 0; |
| 777 | |
| 778 | diff = tm->tm_wday - i; |
| 779 | if (diff <= 0) |
| 780 | n++; |
| 781 | diff += 7*n; |
| 782 | |
| 783 | update_tm(tm, diff * 24 * 60 * 60); |
| 784 | return end; |
| 785 | } |
| 786 | } |
| 787 | |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 788 | if (match_string(date, "months") >= 5) { |
| 789 | int n = tm->tm_mon - *num; |
| 790 | *num = 0; |
| 791 | while (n < 0) { |
| 792 | n += 12; |
| 793 | tm->tm_year--; |
| 794 | } |
| 795 | tm->tm_mon = n; |
| 796 | return end; |
| 797 | } |
| 798 | |
| 799 | if (match_string(date, "years") >= 4) { |
| 800 | tm->tm_year -= *num; |
| 801 | *num = 0; |
| 802 | return end; |
| 803 | } |
| 804 | |
| 805 | return end; |
| 806 | } |
| 807 | |
Linus Torvalds | e92a54d | 2006-09-28 12:12:28 -0700 | [diff] [blame] | 808 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num) |
| 809 | { |
| 810 | char *end; |
| 811 | unsigned long number = strtoul(date, &end, 10); |
| 812 | |
Linus Torvalds | 393d340 | 2006-09-28 12:14:27 -0700 | [diff] [blame] | 813 | switch (*end) { |
| 814 | case ':': |
| 815 | case '.': |
| 816 | case '/': |
| 817 | case '-': |
| 818 | if (isdigit(end[1])) { |
| 819 | int match = match_multi_number(number, *end, date, end, tm); |
| 820 | if (match) |
| 821 | return date + match; |
| 822 | } |
| 823 | } |
| 824 | |
Linus Torvalds | e92a54d | 2006-09-28 12:12:28 -0700 | [diff] [blame] | 825 | *num = number; |
| 826 | return end; |
| 827 | } |
| 828 | |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 829 | unsigned long approxidate(const char *date) |
| 830 | { |
| 831 | int number = 0; |
| 832 | struct tm tm, now; |
| 833 | struct timeval tv; |
| 834 | char buffer[50]; |
| 835 | |
| 836 | if (parse_date(date, buffer, sizeof(buffer)) > 0) |
| 837 | return strtoul(buffer, NULL, 10); |
| 838 | |
| 839 | gettimeofday(&tv, NULL); |
| 840 | localtime_r(&tv.tv_sec, &tm); |
| 841 | now = tm; |
| 842 | for (;;) { |
| 843 | unsigned char c = *date; |
| 844 | if (!c) |
| 845 | break; |
| 846 | date++; |
| 847 | if (isdigit(c)) { |
Linus Torvalds | e92a54d | 2006-09-28 12:12:28 -0700 | [diff] [blame] | 848 | date = approxidate_digit(date-1, &tm, &number); |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 849 | continue; |
| 850 | } |
| 851 | if (isalpha(c)) |
| 852 | date = approxidate_alpha(date-1, &tm, &number); |
| 853 | } |
| 854 | if (number > 0 && number < 32) |
| 855 | tm.tm_mday = number; |
Linus Torvalds | b73cebf | 2006-01-04 19:33:55 -0800 | [diff] [blame] | 856 | if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year) |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 857 | tm.tm_year--; |
| 858 | return mktime(&tm); |
| 859 | } |