From c4e908915cec2c3dc426417dfc07f2c09da53020 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 14 Jun 2010 07:56:21 -0400 Subject: Support period expressions like "last 4 weeks" --- src/times.cc | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) (limited to 'src/times.cc') diff --git a/src/times.cc b/src/times.cc index 99493f52..c8eda4bf 100644 --- a/src/times.cc +++ b/src/times.cc @@ -681,14 +681,6 @@ date_interval_t date_parser_t::parse() tok.kind != lexer_t::token_t::END_REACHED; tok = lexer.next_token()) { switch (tok.kind) { -#if 0 - case lexer_t::token_t::TOK_INT: - // jww (2009-11-18): NYI - assert(! "Need to allow for expressions like \"4 months ago\""); - tok.unexpected(); - break; -#endif - case lexer_t::token_t::TOK_DATE: if (! inclusion_specifier) inclusion_specifier = date_specifier_t(); @@ -776,11 +768,44 @@ date_interval_t date_parser_t::parse() tok = lexer.next_token(); switch (tok.kind) { - case lexer_t::token_t::TOK_INT: - // jww (2009-11-18): Allow things like "last 5 weeks" - assert(! "Need to allow for expressions like \"last 5 weeks\""); - tok.unexpected(); + case lexer_t::token_t::TOK_INT: { + unsigned short amount = boost::get(*tok.value); + + date_t base(today); + date_t end(today); + + tok = lexer.next_token(); + switch (tok.kind) { + case lexer_t::token_t::TOK_YEARS: + base += gregorian::years(amount * adjust); + break; + case lexer_t::token_t::TOK_QUARTERS: + base += gregorian::months(amount * adjust * 3); + break; + case lexer_t::token_t::TOK_MONTHS: + base += gregorian::months(amount * adjust); + break; + case lexer_t::token_t::TOK_WEEKS: + base += gregorian::weeks(amount * adjust); + break; + case lexer_t::token_t::TOK_DAYS: + base += gregorian::days(amount * adjust); + break; + default: + tok.unexpected(); + break; + } + + if (adjust >= 0) { + date_t temp = base; + base = end; + end = temp; + } + + since_specifier = date_specifier_t(base); + until_specifier = date_specifier_t(end); break; + } case lexer_t::token_t::TOK_A_MONTH: { inclusion_specifier = date_specifier_t(); -- cgit v1.2.3