From 3ef6aa9f8e9795b5e2d50afc62fddd906dcd7646 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 31 May 2010 13:18:41 -0400 Subject: [PATCH] Enhancement - Date::toISO - Added ISO date format support (19980119T070000Z) to Date class, for use in export.ical. - Added unit test. --- src/Date.cpp | 20 ++++++++++++++++++++ src/Date.h | 1 + src/tests/date.t.cpp | 5 ++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Date.cpp b/src/Date.cpp index f9d1a4181..b3a187fa9 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -25,6 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -375,6 +376,25 @@ std::string Date::toEpochString () return epoch.str (); } +//////////////////////////////////////////////////////////////////////////////// +// 19980119T070000Z = YYYYMMDDThhmmssZ +std::string Date::toISO () +{ + struct tm* t = gmtime (&mT); + + std::stringstream iso; + iso << std::setw (4) << std::setfill ('0') << t->tm_year + 1900 + << std::setw (2) << std::setfill ('0') << t->tm_mon + 1 + << std::setw (2) << std::setfill ('0') << t->tm_mday + << "T" + << std::setw (2) << std::setfill ('0') << t->tm_hour + << std::setw (2) << std::setfill ('0') << t->tm_min + << std::setw (2) << std::setfill ('0') << t->tm_sec + << "Z"; + + return iso.str (); +} + //////////////////////////////////////////////////////////////////////////////// void Date::toEpoch (time_t& epoch) { diff --git a/src/Date.h b/src/Date.h index 4cebb0875..91635204c 100644 --- a/src/Date.h +++ b/src/Date.h @@ -47,6 +47,7 @@ public: void toEpoch (time_t&); time_t toEpoch (); std::string toEpochString (); + std::string toISO (); void toMDY (int&, int&, int&); const std::string toString (const std::string& format = "m/d/Y") const; const std::string toStringWithTime (const std::string& format = "m/d/Y") const; diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index 15e095ccc..44bbf44d9 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 (143); + UnitTest t (144); try { @@ -150,6 +150,9 @@ int main (int argc, char** argv) Date fromEpoch (epoch.toEpoch ()); t.is (fromEpoch.toString (), epoch.toString (), "ctor (time_t)"); + Date iso (1000000000); + t.is (iso.toISO (), "20010909T014640Z", "1,000,000,000 -> 20010909T014640Z"); + // Date parsing. Date fromString1 ("1/1/2008"); t.is (fromString1.month (), 1, "ctor (std::string) -> m");