From 125058093ff8178a0ad2a6c944b43c8c369563dc Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 15 Nov 2010 11:43:21 -0500 Subject: [PATCH] Dates - Added Date::startof{Day,Week,Month,Year} methods to facilitate the history, ghistory and burndown charts. --- src/Date.cpp | 26 ++++++++++++++++++++++++++ src/Date.h | 6 ++++++ src/tests/date.t.cpp | 9 ++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Date.cpp b/src/Date.cpp index 93b3a6c3d..c83cc06b5 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -460,6 +460,32 @@ const std::string Date::toString (const std::string& format /*= "m/d/Y" */) cons return formatted; } +//////////////////////////////////////////////////////////////////////////////// +Date Date::startOfDay () const +{ + return Date (month (), day (), year ()); +} + +//////////////////////////////////////////////////////////////////////////////// +Date Date::startOfWeek () const +{ + Date sow (mT); + sow -= (dayOfWeek () * 86400); + return Date (sow.month (), sow.day (), sow.year ()); +} + +//////////////////////////////////////////////////////////////////////////////// +Date Date::startOfMonth () const +{ + return Date (month (), 1, year ()); +} + +//////////////////////////////////////////////////////////////////////////////// +Date Date::startOfYear () const +{ + return Date (1, 1, year ()); +} + //////////////////////////////////////////////////////////////////////////////// bool Date::valid (const std::string& input, const std::string& format) { diff --git a/src/Date.h b/src/Date.h index c6e7fadea..1cb93b497 100644 --- a/src/Date.h +++ b/src/Date.h @@ -50,6 +50,12 @@ public: std::string toISO (); void toMDY (int&, int&, int&); const std::string toString (const std::string& format = "m/d/Y") const; + + Date startOfDay () const; + Date startOfWeek () const; + Date startOfMonth () const; + Date startOfYear () const; + static bool valid (const std::string&, const std::string& format = "m/d/Y"); static bool valid (const int, const int, const int, const int, const int, const int); static bool valid (const int, const int, const int); diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index 4df1e2569..ca7641a53 100644 --- a/src/tests/date.t.cpp +++ b/src/tests/date.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (154); + UnitTest t (158); try { @@ -160,6 +160,13 @@ int main (int argc, char** argv) Date iso (1000000000); t.is (iso.toISO (), "20010909T014640Z", "1,000,000,000 -> 20010909T014640Z"); + // Quantization. + Date quant (1234567890); + t.is (quant.startOfDay ().toString ("YMDHNS"), "20090213000000", "1234567890 -> 2/13/2009 18:31:30 -> 2/13/2009 0:00:00"); + t.is (quant.startOfWeek ().toString ("YMDHNS"), "20090208000000", "1234567890 -> 2/13/2009 18:31:30 -> 2/8/2009 0:00:00"); + t.is (quant.startOfMonth ().toString ("YMDHNS"), "20090201000000", "1234567890 -> 2/13/2009 18:31:30 -> 2/1/2009 0:00:00"); + t.is (quant.startOfYear ().toString ("YMDHNS"), "20090101000000", "1234567890 -> 2/13/2009 18:31:30 -> 1/1/2009 0:00:00"); + // Date parsing. Date fromString1 ("1/1/2008"); t.is (fromString1.month (), 1, "ctor (std::string) -> m");