From 63f91c2f88d100beeb2e91c9c7557676eaf1742d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 1 Jul 2011 22:13:34 -0400 Subject: [PATCH] Enhancement - Implemented Record::get_date to eliminated the need to obtain a date as a string, then convert to time_t, then instantiate a Date. --- src/Record.cpp | 10 ++++++++++ src/Record.h | 2 ++ src/TDB.cpp | 2 +- src/TDB2.cpp | 2 +- src/Task.cpp | 12 ++++++------ src/commands/CmdBurndown.cpp | 16 ++++++++-------- src/commands/CmdCalendar.cpp | 2 +- src/commands/CmdEdit.cpp | 12 ++++++------ src/commands/CmdHistory.cpp | 16 ++++++++-------- src/commands/CmdInfo.cpp | 10 +++++----- src/commands/CmdTimesheet.cpp | 4 ++-- src/recur.cpp | 8 ++++---- 12 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/Record.cpp b/src/Record.cpp index 1120c7fd8..45fe7d32a 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -168,6 +168,16 @@ unsigned long Record::get_ulong (const std::string& name) const return 0; } +//////////////////////////////////////////////////////////////////////////////// +time_t Record::get_date (const std::string& name) const +{ + Record::const_iterator i = this->find (name); + if (i != this->end ()) + return (time_t) strtoul (i->second.value ().c_str (), NULL, 10); + + return 0; +} + //////////////////////////////////////////////////////////////////////////////// void Record::set (const std::string& name, const std::string& value) { diff --git a/src/Record.h b/src/Record.h index c09740ea9..61586f47c 100644 --- a/src/Record.h +++ b/src/Record.h @@ -31,6 +31,7 @@ #include #include #include +#include #include class Record : public std::map @@ -49,6 +50,7 @@ public: const std::string get (const std::string&) const; int get_int (const std::string&) const; unsigned long get_ulong (const std::string&) const; + time_t get_date (const std::string&) const; void set (const std::string&, const std::string&); void set (const std::string&, int); void remove (const std::string&); diff --git a/src/TDB.cpp b/src/TDB.cpp index b88b3b909..019811da5 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -650,7 +650,7 @@ int TDB::gc () else if (s == Task::waiting) { // Wake up tasks that need to be woken. - Date wait_date (atoi (task->get ("wait").c_str ())); + Date wait_date (task->get_date ("wait")); if (now > wait_date) { task->setStatus (Task::pending); diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 8b1b68ec7..064c36bbb 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -891,7 +891,7 @@ int TDB::gc () else if (s == Task::waiting) { // Wake up tasks that need to be woken. - Date wait_date (atoi (task->get ("wait").c_str ())); + Date wait_date (task->get_date ("wait")); if (now > wait_date) { task->setStatus (Task::pending); diff --git a/src/Task.cpp b/src/Task.cpp index 68384350d..0e22e3d9c 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -883,28 +883,28 @@ void Task::validate () const if (has ("due")) { - Date due (::atoi (get ("due").c_str ())); + Date due (get_date ("due")); // Verify wait < due if (has ("wait")) { - Date wait (::atoi (get ("wait").c_str ())); + Date wait (get_date ("wait")); if (wait > due) throw std::string (STRING_TASK_VALID_WAIT); } - Date entry (::atoi (get ("entry").c_str ())); + Date entry (get_date ("entry")); if (has ("start")) { - Date start (::atoi (get ("start").c_str ())); + Date start (get_date ("start")); if (entry > start) throw std::string (STRING_TASK_VALID_START); } if (has ("end")) { - Date end (::atoi (get ("end").c_str ())); + Date end (get_date ("end")); if (entry > end) throw std::string (STRING_TASK_VALID_END); } @@ -1129,7 +1129,7 @@ float Task::urgency () if (has ("due")) { Date now; - Date due (get ("due")); + Date due (get_date ("due")); int days_overdue = (now - due) / 86400; if (days_overdue >= 7) term = 1.0; diff --git a/src/commands/CmdBurndown.cpp b/src/commands/CmdBurndown.cpp index 1c9a902e7..d092f649f 100644 --- a/src/commands/CmdBurndown.cpp +++ b/src/commands/CmdBurndown.cpp @@ -227,7 +227,7 @@ void Chart::scan (std::vector & tasks) for (task = tasks.begin (); task != tasks.end (); ++task) { // The entry date is when the counting starts. - Date from = quantize (Date (task->get ("entry"))); + Date from = quantize (Date (task->get_date ("entry"))); epoch = from.toEpoch (); if (bars.find (epoch) != bars.end ()) @@ -241,7 +241,7 @@ void Chart::scan (std::vector & tasks) { if (task->has ("start")) { - Date start = quantize (Date (task->get ("start"))); + Date start = quantize (Date (task->get_date ("start"))); while (from < start) { epoch = from.toEpoch (); @@ -272,7 +272,7 @@ void Chart::scan (std::vector & tasks) else if (status == Task::completed) { // Truncate history so it starts at 'earliest' for completed tasks. - Date end = quantize (Date (task->get ("end"))); + Date end = quantize (Date (task->get_date ("end"))); epoch = end.toEpoch (); if (bars.find (epoch) != bars.end ()) @@ -288,7 +288,7 @@ void Chart::scan (std::vector & tasks) if (task->has ("start")) { - Date start = quantize (Date (task->get ("start"))); + Date start = quantize (Date (task->get_date ("start"))); while (from < start) { epoch = from.toEpoch (); @@ -312,7 +312,7 @@ void Chart::scan (std::vector & tasks) } else { - Date end = quantize (Date (task->get ("end"))); + Date end = quantize (Date (task->get_date ("end"))); while (from < end) { epoch = from.toEpoch (); @@ -334,7 +334,7 @@ void Chart::scan (std::vector & tasks) else if (status == Task::deleted) { // Skip old deleted tasks. - Date end = quantize (Date (task->get ("end"))); + Date end = quantize (Date (task->get_date ("end"))); epoch = end.toEpoch (); if (bars.find (epoch) != bars.end ()) ++bars[epoch].removed; @@ -344,7 +344,7 @@ void Chart::scan (std::vector & tasks) if (task->has ("start")) { - Date start = quantize (Date (task->get ("start"))); + Date start = quantize (Date (task->get_date ("start"))); while (from < start) { epoch = from.toEpoch (); @@ -361,7 +361,7 @@ void Chart::scan (std::vector & tasks) } else { - Date end = quantize (Date (task->get ("end"))); + Date end = quantize (Date (task->get_date ("end"))); while (from < end) { epoch = from.toEpoch (); diff --git a/src/commands/CmdCalendar.cpp b/src/commands/CmdCalendar.cpp index d2acbb84b..101437fd3 100644 --- a/src/commands/CmdCalendar.cpp +++ b/src/commands/CmdCalendar.cpp @@ -197,7 +197,7 @@ int CmdCalendar::execute (std::string& output) !task->hasTag ("nocal")) { ++countDueDates; - Date d (strtol (task->get ("due").c_str (), NULL, 10)); + Date d (task->get ("due")); if (d < oldest) oldest = d; } } diff --git a/src/commands/CmdEdit.cpp b/src/commands/CmdEdit.cpp index 99177f110..2c58d77c5 100644 --- a/src/commands/CmdEdit.cpp +++ b/src/commands/CmdEdit.cpp @@ -296,7 +296,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) { Date edited (strtol (value.c_str (), NULL, 10)); - Date original (strtol (task.get ("entry").c_str (), NULL, 10)); + Date original (task.get_date ("entry")); if (!original.sameDay (edited)) { context.footnote ("Creation date modified."); @@ -314,7 +314,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) if (task.get ("start") != "") { - Date original (strtol (task.get ("start").c_str (), NULL, 10)); + Date original (task.get_date ("start")); if (!original.sameDay (edited)) { context.footnote ("Start date modified."); @@ -344,7 +344,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) if (task.get ("end") != "") { - Date original (strtol (task.get ("end").c_str (), NULL, 10)); + Date original (task.get_date ("end")); if (!original.sameDay (edited)) { context.footnote ("Done date modified."); @@ -372,7 +372,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) if (task.get ("due") != "") { - Date original (strtol (task.get ("due").c_str (), NULL, 10)); + Date original (task.get_date ("due")); if (!original.sameDay (edited)) { context.footnote ("Due date modified."); @@ -410,7 +410,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) if (task.get ("until") != "") { - Date original (strtol (task.get ("until").c_str (), NULL, 10)); + Date original (task.get_date ("until")); if (!original.sameDay (edited)) { context.footnote ("Until date modified."); @@ -472,7 +472,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after) if (task.get ("wait") != "") { - Date original (strtol (task.get ("wait").c_str (), NULL, 10)); + Date original (task.get_date ("wait")); if (!original.sameDay (edited)) { context.footnote ("Wait date modified."); diff --git a/src/commands/CmdHistory.cpp b/src/commands/CmdHistory.cpp index 2ee5bd47f..c3fafd89a 100644 --- a/src/commands/CmdHistory.cpp +++ b/src/commands/CmdHistory.cpp @@ -72,11 +72,11 @@ int CmdHistoryMonthly::execute (std::string& output) std::vector ::iterator task; for (task = filtered.begin (); task != filtered.end (); ++task) { - Date entry (task->get ("entry")); + Date entry (task->get_date ("entry")); Date end; if (task->has ("end")) - end = Date (task->get ("end")); + end = Date (task->get_date ("end")); time_t epoch = entry.startOfMonth ().toEpoch (); groups[epoch] = 0; @@ -232,11 +232,11 @@ int CmdHistoryAnnual::execute (std::string& output) std::vector ::iterator task; for (task = filtered.begin (); task != filtered.end (); ++task) { - Date entry (task->get ("entry")); + Date entry (task->get_date ("entry")); Date end; if (task->has ("end")) - end = Date (task->get ("end")); + end = Date (task->get_date ("end")); time_t epoch = entry.startOfYear ().toEpoch (); groups[epoch] = 0; @@ -389,11 +389,11 @@ int CmdGHistoryMonthly::execute (std::string& output) std::vector ::iterator task; for (task = filtered.begin (); task != filtered.end (); ++task) { - Date entry (task->get ("entry")); + Date entry (task->get_date ("entry")); Date end; if (task->has ("end")) - end = Date (task->get ("end")); + end = Date (task->get_date ("end")); time_t epoch = entry.startOfMonth ().toEpoch (); groups[epoch] = 0; @@ -588,11 +588,11 @@ int CmdGHistoryAnnual::execute (std::string& output) std::vector ::iterator task; for (task = filtered.begin (); task != filtered.end (); ++task) { - Date entry (task->get ("entry")); + Date entry (task->get_date ("entry")); Date end; if (task->has ("end")) - end = Date (task->get ("end")); + end = Date (task->get_date ("end")); time_t epoch = entry.startOfYear ().toEpoch (); groups[epoch] = 0; diff --git a/src/commands/CmdInfo.cpp b/src/commands/CmdInfo.cpp index 6811ff48f..bf3c1a5e5 100644 --- a/src/commands/CmdInfo.cpp +++ b/src/commands/CmdInfo.cpp @@ -179,7 +179,7 @@ int CmdInfo::execute (std::string& output) row = view.addRow (); view.set (row, 0, STRING_CMD_INFO_RECUR_UNTIL); - Date dt (strtol (task->get ("until").c_str (), NULL, 10)); + Date dt (task->get ("until")); std::string format = context.config.get ("reportdateformat"); if (format == "") format = context.config.get ("dateformat"); @@ -227,7 +227,7 @@ int CmdInfo::execute (std::string& output) { row = view.addRow (); view.set (row, 0, STRING_COLUMN_LABEL_WAITING); - Date dt (strtol (task->get ("wait").c_str (), NULL, 10)); + Date dt (task->get_date ("wait")); view.set (row, 1, dt.toString (context.config.get ("dateformat"))); } @@ -236,7 +236,7 @@ int CmdInfo::execute (std::string& output) { row = view.addRow (); view.set (row, 0, STRING_COLUMN_LABEL_START); - Date dt (strtol (task->get ("start").c_str (), NULL, 10)); + Date dt (task->get_date ("start")); view.set (row, 1, dt.toString (context.config.get ("dateformat"))); } @@ -245,7 +245,7 @@ int CmdInfo::execute (std::string& output) { row = view.addRow (); view.set (row, 0, STRING_COLUMN_LABEL_END); - Date dt (strtol (task->get ("end").c_str (), NULL, 10)); + Date dt (task->get_date ("end")); view.set (row, 1, dt.toString (context.config.get ("dateformat"))); } @@ -271,7 +271,7 @@ int CmdInfo::execute (std::string& output) // entry row = view.addRow (); view.set (row, 0, STRING_COLUMN_LABEL_ENTERED); - Date dt (strtol (task->get ("entry").c_str (), NULL, 10)); + Date dt (task->get_date ("entry")); std::string entry = dt.toString (context.config.get ("dateformat")); std::string age; diff --git a/src/commands/CmdTimesheet.cpp b/src/commands/CmdTimesheet.cpp index cf296e325..c057ee90f 100644 --- a/src/commands/CmdTimesheet.cpp +++ b/src/commands/CmdTimesheet.cpp @@ -117,7 +117,7 @@ int CmdTimesheet::execute (std::string& output) // If task completed within range. if (task->getStatus () == Task::completed) { - Date compDate (strtol (task->get ("end").c_str (), NULL, 10)); + Date compDate (task->get_date ("end")); if (compDate >= start && compDate < end) { Color c (task->get ("fg") + " " + task->get ("bg")); @@ -155,7 +155,7 @@ int CmdTimesheet::execute (std::string& output) if (task->getStatus () == Task::pending && task->has ("start")) { - Date startDate (strtol (task->get ("start").c_str (), NULL, 10)); + Date startDate (task->get_date ("start")); if (startDate >= start && startDate < end) { Color c (task->get ("fg") + " " + task->get ("bg")); diff --git a/src/recur.cpp b/src/recur.cpp index 446a9a9e0..4a248f006 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -107,8 +107,8 @@ void handleRecurrence () if (t->has ("wait")) { - Date old_wait (atoi (t->get ("wait").c_str ())); - Date old_due (atoi (t->get ("due").c_str ())); + Date old_wait (t->get_date ("wait")); + Date old_due (t->get_date ("due")); Date due (*d); sprintf (dueDate, "%u", (unsigned int) (due + (old_wait - old_due)).toEpoch ()); rec.set ("wait", dueDate); @@ -160,14 +160,14 @@ void handleRecurrence () bool generateDueDates (Task& parent, std::vector & allDue) { // Determine due date, recur period and until date. - Date due (atoi (parent.get ("due").c_str ())); + Date due (parent.get_date ("due")); std::string recur = parent.get ("recur"); bool specificEnd = false; Date until; if (parent.get ("until") != "") { - until = Date (atoi (parent.get ("until").c_str ())); + until = Date (parent.get ("until")); specificEnd = true; }