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