X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=date.c;h=00f9eb5d0b9730107a8e08e92eb04d4cc9233595;hb=0c357544b0d4415c464fb8b35593b21db1d1f7f7;hp=6bae49ca330a69df5e2cf29e1e461f2490d99b2f;hpb=6c1c4423e2f83037ef762d63be6edbc1f9e6e867;p=thirdparty%2Fgit.git diff --git a/date.c b/date.c index 6bae49ca33..00f9eb5d0b 100644 --- a/date.c +++ b/date.c @@ -586,11 +586,17 @@ static int date_string(unsigned long date, int offset, char *buf, int len) /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 (i.e. English) day/month names, and it doesn't work correctly with %z. */ -int parse_date(const char *date, char *result, int maxlen) +int parse_date_basic(const char *date, unsigned long *timestamp, int *offset) { struct tm tm; - int offset, tm_gmt; - time_t then; + int tm_gmt; + unsigned long dummy_timestamp; + int dummy_offset; + + if (!timestamp) + timestamp = &dummy_timestamp; + if (!offset) + offset = &dummy_offset; memset(&tm, 0, sizeof(tm)); tm.tm_year = -1; @@ -600,7 +606,7 @@ int parse_date(const char *date, char *result, int maxlen) tm.tm_hour = -1; tm.tm_min = -1; tm.tm_sec = -1; - offset = -1; + *offset = -1; tm_gmt = 0; for (;;) { @@ -612,11 +618,11 @@ int parse_date(const char *date, char *result, int maxlen) break; if (isalpha(c)) - match = match_alpha(date, &tm, &offset); + match = match_alpha(date, &tm, offset); else if (isdigit(c)) - match = match_digit(date, &tm, &offset, &tm_gmt); + match = match_digit(date, &tm, offset, &tm_gmt); else if ((c == '-' || c == '+') && isdigit(date[1])) - match = match_tz(date, &offset); + match = match_tz(date, offset); if (!match) { /* BAD CRAP */ @@ -627,16 +633,25 @@ int parse_date(const char *date, char *result, int maxlen) } /* mktime uses local timezone */ - then = tm_to_time_t(&tm); - if (offset == -1) - offset = (then - mktime(&tm)) / 60; + *timestamp = tm_to_time_t(&tm); + if (*offset == -1) + *offset = ((time_t)*timestamp - mktime(&tm)) / 60; - if (then == -1) + if (*timestamp == -1) return -1; if (!tm_gmt) - then -= offset * 60; - return date_string(then, offset, result, maxlen); + *timestamp -= *offset * 60; + return 0; /* success */ +} + +int parse_date(const char *date, char *result, int maxlen) +{ + unsigned long timestamp; + int offset; + if (parse_date_basic(date, ×tamp, &offset)) + return -1; + return date_string(timestamp, offset, result, maxlen); } enum date_mode parse_date_format(const char *format) @@ -984,26 +999,27 @@ static unsigned long approxidate_str(const char *date, unsigned long approxidate_relative(const char *date, const struct timeval *tv) { - char buffer[50]; + unsigned long timestamp; + int offset; int errors = 0; - if (parse_date(date, buffer, sizeof(buffer)) > 0) - return strtoul(buffer, NULL, 0); - + if (!parse_date_basic(date, ×tamp, &offset)) + return timestamp; return approxidate_str(date, tv, &errors); } unsigned long approxidate_careful(const char *date, int *error_ret) { struct timeval tv; - char buffer[50]; + unsigned long timestamp; + int offset; int dummy = 0; if (!error_ret) error_ret = &dummy; - if (parse_date(date, buffer, sizeof(buffer)) > 0) { + if (!parse_date_basic(date, ×tamp, &offset)) { *error_ret = 0; - return strtoul(buffer, NULL, 0); + return timestamp; } gettimeofday(&tv, NULL);