From 7468a2d81dddb902e57cd7a29c93046ca5cf4956 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 25 Jul 2010 22:11:25 -0400 Subject: [PATCH] Feature #43 - relative dates: +3d, -2w - Now dates (due, wait, limit) are parsed first as Durations, and on error reparsed as Date. When a Duration is found instead of a Date, the Duration is added to the current date/time. --- src/Att.cpp | 34 +++++++++++++++++++++++++++++++++- src/report.cpp | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Att.cpp b/src/Att.cpp index 9d45802fd..90c61877b 100644 --- a/src/Att.cpp +++ b/src/Att.cpp @@ -335,13 +335,45 @@ bool Att::validNameValue ( // modify 'value' here accordingly. } + // Dates can now be either a date, or a duration that is added as an offset + // to the current date. else if (name == "due" || name == "until" || name == "wait") { // Validate and convert to epoch. if (value != "") - value = Date (value, context.config.get ("dateformat")).toEpochString (); + { + // Try parsing as a duration. If unsuccessful, try again, as a date. + try + { + Date now; + Duration dur (value); + + if (dur.negative ()) + value = (now - (time_t)dur).toEpochString (); + else + value = (now + (time_t)dur).toEpochString (); + } + + // If the date parsing failed, try parsing as a duration. If successful, + // add the duration to the current date. If unsuccessful, propagate the + // original date parse error. + + // Try parsing as a date. If unsuccessfull, throw. + catch (...) + { + try + { + value = Date (value, context.config.get ("dateformat")).toEpochString (); + } + + catch (std::string& e) + { + throw e; + } + } + } } else if (name == "recur") diff --git a/src/report.cpp b/src/report.cpp index 8d1580ed1..d6f1c65c7 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -2365,7 +2365,7 @@ int handleReportStats (std::string &outs) } if (it->getStatus () == Task::pending) - daysPending += (now - entry) / 86400.0; + daysPending += (now.toEpoch () - entry) / 86400.0; descLength += it->get ("description").length ();