- Added Date::startof{Day,Week,Month,Year} methods to facilitate the
  history, ghistory and burndown charts.
This commit is contained in:
Paul Beckingham 2010-11-15 11:43:21 -05:00
parent 891136788f
commit 125058093f
3 changed files with 40 additions and 1 deletions

View file

@ -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)
{

View file

@ -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);

View file

@ -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");