diff options
Diffstat (limited to 'src/times.cc')
-rw-r--r-- | src/times.cc | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/src/times.cc b/src/times.cc index be488baf..a7906aee 100644 --- a/src/times.cc +++ b/src/times.cc @@ -197,25 +197,33 @@ namespace { optional_year year, date_traits_t * traits = NULL) { - date_t when; + VERIFY(std::strlen(date_str) < 127); - if (std::strchr(date_str, '/')) { - when = io.parse(date_str); - } else { - char buf[128]; - VERIFY(std::strlen(date_str) < 127); - std::strcpy(buf, date_str); + char buf[128]; + std::strcpy(buf, date_str); - for (char * p = buf; *p; p++) - if (*p == '.' || *p == '-') - *p = '/'; + for (char * p = buf; *p; p++) + if (*p == '.' || *p == '-') + *p = '/'; - when = io.parse(buf); - } + date_t when = io.parse(buf); if (! when.is_not_a_date()) { - DEBUG("times.parse", "Parsed date string: " << date_str); - DEBUG("times.parse", "Parsed result is: " << when); + DEBUG("times.parse", "Passed date string: " << date_str); + DEBUG("times.parse", "Parsed date string: " << buf); + DEBUG("times.parse", "Parsed result is: " << when); + DEBUG("times.parse", "Formatted result is: " << io.format(when)); + + string when_str = io.format(when); + + const char * p = when_str.c_str(); + const char * q = buf; + for (; *p && *q; p++, q++) { + if (*p != *q && *p == '0') p++; + if (! *p || *p != *q) break; + } + if (*p != '\0' || *q != '\0') + throw_(date_error, _("Invalid date: %1") << date_str); if (traits) *traits = io.traits; @@ -1299,14 +1307,14 @@ date_parser_t::lexer_t::token_t date_parser_t::lexer_t::next_token() // "2009/08/01", but also dates that fit the user's --input-date-format, // assuming their format fits in one argument and begins with a digit. if (std::isdigit(*begin)) { - try { - string::const_iterator i = begin; - for (i = begin; i != end && ! std::isspace(*i); i++) {} - assert(i != begin); + string::const_iterator i = begin; + for (i = begin; i != end && ! std::isspace(*i); i++) {} + assert(i != begin); - string possible_date(start, i); - date_traits_t traits; + string possible_date(start, i); + try { + date_traits_t traits; date_t when = parse_date_mask(possible_date.c_str(), none, &traits); if (! when.is_not_a_date()) { begin = i; @@ -1314,7 +1322,12 @@ date_parser_t::lexer_t::token_t date_parser_t::lexer_t::next_token() token_t::content_t(date_specifier_t(when, traits))); } } - catch (...) {} + catch (date_error&) { + if (contains(possible_date, "/") || + contains(possible_date, "-") || + contains(possible_date, ".")) + throw; + } } start = begin; @@ -1457,7 +1470,7 @@ std::string format_datetime(const datetime_t& when, if (format_type == FMT_WRITTEN) { return written_datetime_io->format(when); } - else if (format_type == FMT_CUSTOM || format) { + else if (format_type == FMT_CUSTOM && format) { datetime_io_map::iterator i = temp_datetime_io.find(*format); if (i != temp_datetime_io.end()) { return (*i).second->format(when); @@ -1483,7 +1496,7 @@ std::string format_date(const date_t& when, if (format_type == FMT_WRITTEN) { return written_date_io->format(when); } - else if (format_type == FMT_CUSTOM || format) { + else if (format_type == FMT_CUSTOM && format) { date_io_map::iterator i = temp_date_io.find(*format); if (i != temp_date_io.end()) { return (*i).second->format(when); |