| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 7 | #include <time.h> |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 8 | #include <sys/time.h> |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 9 | |
| Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 10 | #include "cache.h" |
| 11 | |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 12 | static time_t my_mktime(struct tm *tm) |
| 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--; |
| 27 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
| 28 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; |
| 29 | } |
| 30 | |
| 31 | static const char *month_names[] = { |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 32 | "January", "February", "March", "April", "May", "June", |
| 33 | "July", "August", "September", "October", "November", "December" |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static const char *weekday_names[] = { |
| Linus Torvalds | 6b7b042 | 2005-11-17 20:36:30 | [diff] [blame] | 37 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 38 | }; |
| 39 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 40 | /* |
| Linus Torvalds | f80cd78 | 2005-05-06 22:28:59 | [diff] [blame] | 41 | * The "tz" thing is passed in as this strange "decimal parse of tz" |
| 42 | * thing, which means that tz -0100 is passed in as the integer -100, |
| 43 | * even though it means "sixty minutes off" |
| 44 | */ |
| 45 | const char *show_date(unsigned long time, int tz) |
| 46 | { |
| 47 | struct tm *tm; |
| 48 | time_t t; |
| 49 | static char timebuf[200]; |
| 50 | int minutes; |
| 51 | |
| 52 | minutes = tz < 0 ? -tz : tz; |
| Nicolas Pitre | fbab835 | 2005-05-18 21:11:07 | [diff] [blame] | 53 | minutes = (minutes / 100)*60 + (minutes % 100); |
| Linus Torvalds | f80cd78 | 2005-05-06 22:28:59 | [diff] [blame] | 54 | minutes = tz < 0 ? -minutes : minutes; |
| Nicolas Pitre | fbab835 | 2005-05-18 21:11:07 | [diff] [blame] | 55 | t = time + minutes * 60; |
| Linus Torvalds | f80cd78 | 2005-05-06 22:28:59 | [diff] [blame] | 56 | tm = gmtime(&t); |
| 57 | if (!tm) |
| 58 | return NULL; |
| 59 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d", |
| 60 | weekday_names[tm->tm_wday], |
| 61 | month_names[tm->tm_mon], |
| 62 | tm->tm_mday, |
| 63 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 64 | tm->tm_year + 1900, tz); |
| 65 | return timebuf; |
| 66 | } |
| 67 | |
| 68 | /* |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 69 | * Check these. And note how it doesn't do the summer-time conversion. |
| 70 | * |
| 71 | * In my world, it's always summer, and things are probably a bit off |
| 72 | * in other ways too. |
| 73 | */ |
| 74 | static const struct { |
| 75 | const char *name; |
| 76 | int offset; |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 77 | int dst; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 78 | } timezone_names[] = { |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 79 | { "IDLW", -12, 0, }, /* International Date Line West */ |
| 80 | { "NT", -11, 0, }, /* Nome */ |
| 81 | { "CAT", -10, 0, }, /* Central Alaska */ |
| 82 | { "HST", -10, 0, }, /* Hawaii Standard */ |
| 83 | { "HDT", -10, 1, }, /* Hawaii Daylight */ |
| 84 | { "YST", -9, 0, }, /* Yukon Standard */ |
| 85 | { "YDT", -9, 1, }, /* Yukon Daylight */ |
| 86 | { "PST", -8, 0, }, /* Pacific Standard */ |
| 87 | { "PDT", -8, 1, }, /* Pacific Daylight */ |
| 88 | { "MST", -7, 0, }, /* Mountain Standard */ |
| 89 | { "MDT", -7, 1, }, /* Mountain Daylight */ |
| 90 | { "CST", -6, 0, }, /* Central Standard */ |
| 91 | { "CDT", -6, 1, }, /* Central Daylight */ |
| 92 | { "EST", -5, 0, }, /* Eastern Standard */ |
| 93 | { "EDT", -5, 1, }, /* Eastern Daylight */ |
| 94 | { "AST", -3, 0, }, /* Atlantic Standard */ |
| 95 | { "ADT", -3, 1, }, /* Atlantic Daylight */ |
| 96 | { "WAT", -1, 0, }, /* West Africa */ |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 97 | |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 98 | { "GMT", 0, 0, }, /* Greenwich Mean */ |
| 99 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 100 | |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 101 | { "WET", 0, 0, }, /* Western European */ |
| 102 | { "BST", 0, 1, }, /* British Summer */ |
| 103 | { "CET", +1, 0, }, /* Central European */ |
| 104 | { "MET", +1, 0, }, /* Middle European */ |
| 105 | { "MEWT", +1, 0, }, /* Middle European Winter */ |
| 106 | { "MEST", +1, 1, }, /* Middle European Summer */ |
| 107 | { "CEST", +1, 1, }, /* Central European Summer */ |
| 108 | { "MESZ", +1, 1, }, /* Middle European Summer */ |
| 109 | { "FWT", +1, 0, }, /* French Winter */ |
| 110 | { "FST", +1, 1, }, /* French Summer */ |
| 111 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ |
| Linus Torvalds | 92e2311 | 2005-04-30 22:21:57 | [diff] [blame] | 112 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 113 | { "WAST", +7, 0, }, /* West Australian Standard */ |
| 114 | { "WADT", +7, 1, }, /* West Australian Daylight */ |
| 115 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ |
| 116 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ |
| 117 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ |
| 118 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ |
| 119 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ |
| 120 | { "NZT", +11, 0, }, /* New Zealand */ |
| 121 | { "NZST", +11, 0, }, /* New Zealand Standard */ |
| 122 | { "NZDT", +11, 1, }, /* New Zealand Daylight */ |
| 123 | { "IDLE", +12, 0, }, /* International Date Line East */ |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 124 | }; |
| 125 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 126 | static int match_string(const char *date, const char *str) |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 127 | { |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 128 | int i = 0; |
| 129 | |
| 130 | for (i = 0; *date; date++, str++, i++) { |
| 131 | if (*date == *str) |
| 132 | continue; |
| 133 | if (toupper(*date) == toupper(*str)) |
| 134 | continue; |
| 135 | if (!isalnum(*date)) |
| 136 | break; |
| 137 | return 0; |
| 138 | } |
| 139 | return i; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 140 | } |
| 141 | |
| Linus Torvalds | 68849b5 | 2005-05-01 19:34:56 | [diff] [blame] | 142 | static int skip_alpha(const char *date) |
| 143 | { |
| 144 | int i = 0; |
| 145 | do { |
| 146 | i++; |
| 147 | } while (isalpha(date[i])); |
| 148 | return i; |
| 149 | } |
| 150 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 151 | /* |
| 152 | * Parse month, weekday, or timezone name |
| 153 | */ |
| 154 | static int match_alpha(const char *date, struct tm *tm, int *offset) |
| 155 | { |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < 12; i++) { |
| 159 | int match = match_string(date, month_names[i]); |
| 160 | if (match >= 3) { |
| 161 | tm->tm_mon = i; |
| 162 | return match; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < 7; i++) { |
| 167 | int match = match_string(date, weekday_names[i]); |
| 168 | if (match >= 3) { |
| 169 | tm->tm_wday = i; |
| 170 | return match; |
| 171 | } |
| 172 | } |
| 173 | |
| Junio C Hamano | b4f2a6a | 2006-03-09 19:58:05 | [diff] [blame] | 174 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 175 | int match = match_string(date, timezone_names[i].name); |
| 176 | if (match >= 3) { |
| Linus Torvalds | 5e2a78a | 2005-04-30 21:53:12 | [diff] [blame] | 177 | int off = timezone_names[i].offset; |
| 178 | |
| 179 | /* This is bogus, but we like summer */ |
| 180 | off += timezone_names[i].dst; |
| 181 | |
| Linus Torvalds | 92e2311 | 2005-04-30 22:21:57 | [diff] [blame] | 182 | /* Only use the tz name offset if we don't have anything better */ |
| 183 | if (*offset == -1) |
| 184 | *offset = 60*off; |
| 185 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 186 | return match; |
| 187 | } |
| 188 | } |
| 189 | |
| Linus Torvalds | 68849b5 | 2005-05-01 19:34:56 | [diff] [blame] | 190 | if (match_string(date, "PM") == 2) { |
| 191 | if (tm->tm_hour > 0 && tm->tm_hour < 12) |
| 192 | tm->tm_hour += 12; |
| 193 | return 2; |
| 194 | } |
| 195 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 196 | /* BAD CRAP */ |
| Linus Torvalds | 68849b5 | 2005-05-01 19:34:56 | [diff] [blame] | 197 | return skip_alpha(date); |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 198 | } |
| 199 | |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 200 | 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 20:19:56 | [diff] [blame] | 201 | { |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 202 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 203 | struct tm check = *tm; |
| 204 | struct tm *r = (now_tm ? &check : tm); |
| 205 | time_t specified; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 206 | |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 207 | r->tm_mon = month - 1; |
| 208 | r->tm_mday = day; |
| 209 | if (year == -1) { |
| 210 | if (!now_tm) |
| 211 | return 1; |
| 212 | r->tm_year = now_tm->tm_year; |
| 213 | } |
| 214 | else if (year >= 1970 && year < 2100) |
| 215 | r->tm_year = year - 1900; |
| 216 | else if (year > 70 && year < 100) |
| 217 | r->tm_year = year; |
| 218 | else if (year < 38) |
| 219 | r->tm_year = year + 100; |
| 220 | else |
| 221 | return 0; |
| 222 | if (!now_tm) |
| 223 | return 1; |
| 224 | |
| 225 | specified = my_mktime(r); |
| 226 | |
| 227 | /* Be it commit time or author time, it does not make |
| 228 | * sense to specify timestamp way into the future. Make |
| 229 | * sure it is not later than ten days from now... |
| 230 | */ |
| 231 | if (now + 10*24*3600 < specified) |
| 232 | return 0; |
| 233 | tm->tm_mon = r->tm_mon; |
| 234 | tm->tm_mday = r->tm_mday; |
| 235 | if (year != -1) |
| 236 | tm->tm_year = r->tm_year; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 237 | return 1; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 238 | } |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 241 | |
| Linus Torvalds | 26a2d8a | 2005-07-12 17:33:06 | [diff] [blame] | 242 | static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 243 | { |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 244 | time_t now; |
| 245 | struct tm now_tm; |
| 246 | struct tm *refuse_future; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 247 | long num2, num3; |
| 248 | |
| 249 | num2 = strtol(end+1, &end, 10); |
| 250 | num3 = -1; |
| 251 | if (*end == c && isdigit(end[1])) |
| 252 | num3 = strtol(end+1, &end, 10); |
| 253 | |
| 254 | /* Time? Date? */ |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 255 | switch (c) { |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 256 | case ':': |
| 257 | if (num3 < 0) |
| 258 | num3 = 0; |
| 259 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { |
| 260 | tm->tm_hour = num; |
| 261 | tm->tm_min = num2; |
| 262 | tm->tm_sec = num3; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 263 | break; |
| 264 | } |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 265 | return 0; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 266 | |
| 267 | case '-': |
| 268 | case '/': |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 269 | case '.': |
| 270 | now = time(NULL); |
| 271 | refuse_future = NULL; |
| 272 | if (gmtime_r(&now, &now_tm)) |
| 273 | refuse_future = &now_tm; |
| 274 | |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 275 | if (num > 70) { |
| 276 | /* yyyy-mm-dd? */ |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 277 | if (is_date(num, num2, num3, refuse_future, now, tm)) |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 278 | break; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 279 | /* yyyy-dd-mm? */ |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 280 | if (is_date(num, num3, num2, refuse_future, now, tm)) |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 281 | break; |
| 282 | } |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 283 | /* Our eastern European friends say dd.mm.yy[yy] |
| 284 | * is the norm there, so giving precedence to |
| 285 | * mm/dd/yy[yy] form only when separator is not '.' |
| 286 | */ |
| 287 | if (c != '.' && |
| 288 | is_date(num3, num, num2, refuse_future, now, tm)) |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 289 | break; |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 290 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ |
| 291 | if (is_date(num3, num2, num, refuse_future, now, tm)) |
| 292 | break; |
| 293 | /* Funny European mm.dd.yy */ |
| 294 | if (c == '.' && |
| 295 | is_date(num3, num, num2, refuse_future, now, tm)) |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 296 | break; |
| 297 | return 0; |
| 298 | } |
| 299 | return end - date; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * We've seen a digit. Time? Year? Date? |
| 304 | */ |
| Linus Torvalds | 26a2d8a | 2005-07-12 17:33:06 | [diff] [blame] | 305 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 306 | { |
| 307 | int n; |
| 308 | char *end; |
| 309 | unsigned long num; |
| 310 | |
| 311 | num = strtoul(date, &end, 10); |
| 312 | |
| 313 | /* |
| 314 | * Seconds since 1970? We trigger on that for anything after Jan 1, 2000 |
| 315 | */ |
| 316 | if (num > 946684800) { |
| 317 | time_t time = num; |
| Junio C Hamano | 9cb480f | 2005-06-25 09:21:16 | [diff] [blame] | 318 | if (gmtime_r(&time, tm)) { |
| 319 | *tm_gmt = 1; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 320 | return end - date; |
| Junio C Hamano | 9cb480f | 2005-06-25 09:21:16 | [diff] [blame] | 321 | } |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 325 | * Check for special formats: num[-.:/]num[same]num |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 326 | */ |
| 327 | switch (*end) { |
| 328 | case ':': |
| Junio C Hamano | 38035cf | 2006-04-05 22:31:12 | [diff] [blame] | 329 | case '.': |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 330 | case '/': |
| 331 | case '-': |
| 332 | if (isdigit(end[1])) { |
| 333 | int match = match_multi_number(num, *end, date, end, tm); |
| 334 | if (match) |
| 335 | return match; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 336 | } |
| 337 | } |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * None of the special formats? Try to guess what |
| 341 | * the number meant. We use the number of digits |
| 342 | * to make a more educated guess.. |
| 343 | */ |
| 344 | n = 0; |
| 345 | do { |
| 346 | n++; |
| 347 | } while (isdigit(date[n])); |
| 348 | |
| 349 | /* Four-digit year or a timezone? */ |
| 350 | if (n == 4) { |
| 351 | if (num <= 1200 && *offset == -1) { |
| 352 | unsigned int minutes = num % 100; |
| 353 | unsigned int hours = num / 100; |
| 354 | *offset = hours*60 + minutes; |
| 355 | } else if (num > 1900 && num < 2100) |
| 356 | tm->tm_year = num - 1900; |
| 357 | return n; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * NOTE! We will give precedence to day-of-month over month or |
| Junio C Hamano | 82f9d58 | 2005-12-29 09:30:08 | [diff] [blame] | 362 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 363 | * unless we already have a mday.. |
| 364 | * |
| 365 | * IOW, 01 Apr 05 parses as "April 1st, 2005". |
| 366 | */ |
| 367 | if (num > 0 && num < 32 && tm->tm_mday < 0) { |
| 368 | tm->tm_mday = num; |
| 369 | return n; |
| 370 | } |
| 371 | |
| 372 | /* Two-digit year? */ |
| 373 | if (n == 2 && tm->tm_year < 0) { |
| 374 | if (num < 10 && tm->tm_mday >= 0) { |
| 375 | tm->tm_year = num + 100; |
| 376 | return n; |
| 377 | } |
| 378 | if (num >= 70) { |
| 379 | tm->tm_year = num; |
| 380 | return n; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (num > 0 && num < 32) { |
| 385 | tm->tm_mday = num; |
| 386 | } else if (num > 1900) { |
| 387 | tm->tm_year = num - 1900; |
| 388 | } else if (num > 70) { |
| 389 | tm->tm_year = num; |
| 390 | } else if (num > 0 && num < 13) { |
| 391 | tm->tm_mon = num-1; |
| 392 | } |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 393 | |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 394 | return n; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 395 | } |
| 396 | |
| Linus Torvalds | 26a2d8a | 2005-07-12 17:33:06 | [diff] [blame] | 397 | static int match_tz(const char *date, int *offp) |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 398 | { |
| 399 | char *end; |
| 400 | int offset = strtoul(date+1, &end, 10); |
| 401 | int min, hour; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 402 | int n = end - date - 1; |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 403 | |
| 404 | min = offset % 100; |
| 405 | hour = offset / 100; |
| 406 | |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 407 | /* |
| 408 | * Don't accept any random crap.. At least 3 digits, and |
| 409 | * a valid minute. We might want to check that the minutes |
| 410 | * are divisible by 30 or something too. |
| 411 | */ |
| Linus Torvalds | 68849b5 | 2005-05-01 19:34:56 | [diff] [blame] | 412 | if (min < 60 && n > 2) { |
| 413 | offset = hour*60+min; |
| 414 | if (*date == '-') |
| 415 | offset = -offset; |
| Linus Torvalds | 198b0fb | 2005-05-01 18:48:34 | [diff] [blame] | 416 | |
| Linus Torvalds | 68849b5 | 2005-05-01 19:34:56 | [diff] [blame] | 417 | *offp = offset; |
| 418 | } |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 419 | return end - date; |
| 420 | } |
| 421 | |
| Linus Torvalds | 01c6ad2 | 2005-09-21 22:50:28 | [diff] [blame] | 422 | static int date_string(unsigned long date, int offset, char *buf, int len) |
| 423 | { |
| 424 | int sign = '+'; |
| 425 | |
| 426 | if (offset < 0) { |
| 427 | offset = -offset; |
| 428 | sign = '-'; |
| 429 | } |
| 430 | return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); |
| 431 | } |
| 432 | |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 433 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
| 434 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ |
| Linus Torvalds | 2a39064 | 2005-09-19 22:53:50 | [diff] [blame] | 435 | int parse_date(const char *date, char *result, int maxlen) |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 436 | { |
| 437 | struct tm tm; |
| Linus Torvalds | 01c6ad2 | 2005-09-21 22:50:28 | [diff] [blame] | 438 | int offset, tm_gmt; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 439 | time_t then; |
| 440 | |
| 441 | memset(&tm, 0, sizeof(tm)); |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 442 | tm.tm_year = -1; |
| 443 | tm.tm_mon = -1; |
| 444 | tm.tm_mday = -1; |
| Linus Torvalds | eaa8512 | 2005-04-30 21:25:02 | [diff] [blame] | 445 | tm.tm_isdst = -1; |
| 446 | offset = -1; |
| Junio C Hamano | 9cb480f | 2005-06-25 09:21:16 | [diff] [blame] | 447 | tm_gmt = 0; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 448 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 449 | for (;;) { |
| 450 | int match = 0; |
| 451 | unsigned char c = *date; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 452 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 453 | /* Stop at end of string or newline */ |
| 454 | if (!c || c == '\n') |
| 455 | break; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 456 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 457 | if (isalpha(c)) |
| 458 | match = match_alpha(date, &tm, &offset); |
| 459 | else if (isdigit(c)) |
| Junio C Hamano | 9cb480f | 2005-06-25 09:21:16 | [diff] [blame] | 460 | match = match_digit(date, &tm, &offset, &tm_gmt); |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 461 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
| 462 | match = match_tz(date, &offset); |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 463 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 464 | if (!match) { |
| 465 | /* BAD CRAP */ |
| 466 | match = 1; |
| 467 | } |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 468 | |
| Linus Torvalds | 8996702 | 2005-04-30 20:19:56 | [diff] [blame] | 469 | date += match; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 470 | } |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 471 | |
| Linus Torvalds | eaa8512 | 2005-04-30 21:25:02 | [diff] [blame] | 472 | /* mktime uses local timezone */ |
| 473 | then = my_mktime(&tm); |
| 474 | if (offset == -1) |
| 475 | offset = (then - mktime(&tm)) / 60; |
| 476 | |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 477 | if (then == -1) |
| Linus Torvalds | 2a39064 | 2005-09-19 22:53:50 | [diff] [blame] | 478 | return -1; |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 479 | |
| Junio C Hamano | 9cb480f | 2005-06-25 09:21:16 | [diff] [blame] | 480 | if (!tm_gmt) |
| 481 | then -= offset * 60; |
| Linus Torvalds | 01c6ad2 | 2005-09-21 22:50:28 | [diff] [blame] | 482 | return date_string(then, offset, result, maxlen); |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | void datestamp(char *buf, int bufsize) |
| 486 | { |
| 487 | time_t now; |
| 488 | int offset; |
| 489 | |
| 490 | time(&now); |
| 491 | |
| 492 | offset = my_mktime(localtime(&now)) - now; |
| 493 | offset /= 60; |
| 494 | |
| Linus Torvalds | 01c6ad2 | 2005-09-21 22:50:28 | [diff] [blame] | 495 | date_string(now, offset, buf, bufsize); |
| Edgar Toernig | ecee9d9 | 2005-04-30 16:46:49 | [diff] [blame] | 496 | } |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 497 | |
| 498 | static void update_tm(struct tm *tm, unsigned long sec) |
| 499 | { |
| 500 | time_t n = mktime(tm) - sec; |
| 501 | localtime_r(&n, tm); |
| 502 | } |
| 503 | |
| Linus Torvalds | a8aca41 | 2005-11-18 16:56:40 | [diff] [blame] | 504 | static void date_yesterday(struct tm *tm, int *num) |
| 505 | { |
| 506 | update_tm(tm, 24*60*60); |
| 507 | } |
| 508 | |
| 509 | static void date_time(struct tm *tm, int hour) |
| 510 | { |
| 511 | if (tm->tm_hour < hour) |
| 512 | date_yesterday(tm, NULL); |
| 513 | tm->tm_hour = hour; |
| 514 | tm->tm_min = 0; |
| 515 | tm->tm_sec = 0; |
| 516 | } |
| 517 | |
| 518 | static void date_midnight(struct tm *tm, int *num) |
| 519 | { |
| 520 | date_time(tm, 0); |
| 521 | } |
| 522 | |
| 523 | static void date_noon(struct tm *tm, int *num) |
| 524 | { |
| 525 | date_time(tm, 12); |
| 526 | } |
| 527 | |
| 528 | static void date_tea(struct tm *tm, int *num) |
| 529 | { |
| 530 | date_time(tm, 17); |
| 531 | } |
| 532 | |
| 533 | static const struct special { |
| 534 | const char *name; |
| 535 | void (*fn)(struct tm *, int *); |
| 536 | } special[] = { |
| 537 | { "yesterday", date_yesterday }, |
| 538 | { "noon", date_noon }, |
| 539 | { "midnight", date_midnight }, |
| 540 | { "tea", date_tea }, |
| 541 | { NULL } |
| 542 | }; |
| 543 | |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 544 | static const char *number_name[] = { |
| 545 | "zero", "one", "two", "three", "four", |
| 546 | "five", "six", "seven", "eight", "nine", "ten", |
| 547 | }; |
| 548 | |
| Linus Torvalds | a8aca41 | 2005-11-18 16:56:40 | [diff] [blame] | 549 | static const struct typelen { |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 550 | const char *type; |
| 551 | int length; |
| 552 | } typelen[] = { |
| 553 | { "seconds", 1 }, |
| 554 | { "minutes", 60 }, |
| 555 | { "hours", 60*60 }, |
| 556 | { "days", 24*60*60 }, |
| 557 | { "weeks", 7*24*60*60 }, |
| 558 | { NULL } |
| 559 | }; |
| 560 | |
| 561 | static const char *approxidate_alpha(const char *date, struct tm *tm, int *num) |
| 562 | { |
| Linus Torvalds | a8aca41 | 2005-11-18 16:56:40 | [diff] [blame] | 563 | const struct typelen *tl; |
| 564 | const struct special *s; |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 565 | const char *end = date; |
| 566 | int n = 1, i; |
| 567 | |
| 568 | while (isalpha(*++end)) |
| 569 | n++; |
| 570 | |
| 571 | for (i = 0; i < 12; i++) { |
| 572 | int match = match_string(date, month_names[i]); |
| 573 | if (match >= 3) { |
| 574 | tm->tm_mon = i; |
| 575 | return end; |
| 576 | } |
| 577 | } |
| 578 | |
| Linus Torvalds | a8aca41 | 2005-11-18 16:56:40 | [diff] [blame] | 579 | for (s = special; s->name; s++) { |
| 580 | int len = strlen(s->name); |
| 581 | if (match_string(date, s->name) == len) { |
| 582 | s->fn(tm, num); |
| 583 | return end; |
| 584 | } |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (!*num) { |
| 588 | for (i = 1; i < 11; i++) { |
| 589 | int len = strlen(number_name[i]); |
| 590 | if (match_string(date, number_name[i]) == len) { |
| 591 | *num = i; |
| 592 | return end; |
| 593 | } |
| 594 | } |
| 595 | if (match_string(date, "last") == 4) |
| 596 | *num = 1; |
| 597 | return end; |
| 598 | } |
| 599 | |
| 600 | tl = typelen; |
| 601 | while (tl->type) { |
| 602 | int len = strlen(tl->type); |
| 603 | if (match_string(date, tl->type) >= len-1) { |
| 604 | update_tm(tm, tl->length * *num); |
| 605 | *num = 0; |
| 606 | return end; |
| 607 | } |
| 608 | tl++; |
| 609 | } |
| 610 | |
| Linus Torvalds | 6b7b042 | 2005-11-17 20:36:30 | [diff] [blame] | 611 | for (i = 0; i < 7; i++) { |
| 612 | int match = match_string(date, weekday_names[i]); |
| 613 | if (match >= 3) { |
| 614 | int diff, n = *num -1; |
| 615 | *num = 0; |
| 616 | |
| 617 | diff = tm->tm_wday - i; |
| 618 | if (diff <= 0) |
| 619 | n++; |
| 620 | diff += 7*n; |
| 621 | |
| 622 | update_tm(tm, diff * 24 * 60 * 60); |
| 623 | return end; |
| 624 | } |
| 625 | } |
| 626 | |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 627 | if (match_string(date, "months") >= 5) { |
| 628 | int n = tm->tm_mon - *num; |
| 629 | *num = 0; |
| 630 | while (n < 0) { |
| 631 | n += 12; |
| 632 | tm->tm_year--; |
| 633 | } |
| 634 | tm->tm_mon = n; |
| 635 | return end; |
| 636 | } |
| 637 | |
| 638 | if (match_string(date, "years") >= 4) { |
| 639 | tm->tm_year -= *num; |
| 640 | *num = 0; |
| 641 | return end; |
| 642 | } |
| 643 | |
| 644 | return end; |
| 645 | } |
| 646 | |
| 647 | unsigned long approxidate(const char *date) |
| 648 | { |
| 649 | int number = 0; |
| 650 | struct tm tm, now; |
| 651 | struct timeval tv; |
| 652 | char buffer[50]; |
| 653 | |
| 654 | if (parse_date(date, buffer, sizeof(buffer)) > 0) |
| 655 | return strtoul(buffer, NULL, 10); |
| 656 | |
| 657 | gettimeofday(&tv, NULL); |
| 658 | localtime_r(&tv.tv_sec, &tm); |
| 659 | now = tm; |
| 660 | for (;;) { |
| 661 | unsigned char c = *date; |
| 662 | if (!c) |
| 663 | break; |
| 664 | date++; |
| 665 | if (isdigit(c)) { |
| 666 | char *end; |
| 667 | number = strtoul(date-1, &end, 10); |
| 668 | date = end; |
| 669 | continue; |
| 670 | } |
| 671 | if (isalpha(c)) |
| 672 | date = approxidate_alpha(date-1, &tm, &number); |
| 673 | } |
| 674 | if (number > 0 && number < 32) |
| 675 | tm.tm_mday = number; |
| Linus Torvalds | b73cebf | 2006-01-05 03:33:55 | [diff] [blame] | 676 | if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year) |
| Linus Torvalds | 3c07b1d | 2005-11-15 03:29:06 | [diff] [blame] | 677 | tm.tm_year--; |
| 678 | return mktime(&tm); |
| 679 | } |