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