diff --git a/src/Date.cpp b/src/Date.cpp index f82a80da3..07465fb68 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -100,7 +100,7 @@ Date::Date (const std::string& input, const std::string& format /* = "m/d/Y" */) unsigned int i = 0; // Index into input. - // Format may include: mMdDyYVaAbBhHNS + // Format may include: mMdDyYVaAbBhHNSjJ // // Note that the format should never include T or Z, as that interferes with // the potential parsing for ISO dates constructed from the above format. @@ -666,6 +666,13 @@ int Date::dayOfWeek (const std::string& input) return -1; } +//////////////////////////////////////////////////////////////////////////////// +int Date::dayOfYear () const +{ + struct tm* t = localtime (&mT); + return t->tm_yday + 1; +} + //////////////////////////////////////////////////////////////////////////////// int Date::monthOfYear (const std::string& input) { diff --git a/src/Date.h b/src/Date.h index 6fcfe806b..24572b260 100644 --- a/src/Date.h +++ b/src/Date.h @@ -75,6 +75,7 @@ public: int year () const; int weekOfYear (int) const; int dayOfWeek () const; + int dayOfYear () const; int hour () const; int minute () const; int second () const; diff --git a/test/date.t.cpp b/test/date.t.cpp index b04326e41..26c5d0f81 100644 --- a/test/date.t.cpp +++ b/test/date.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (165); + UnitTest t (168); try { @@ -242,6 +242,11 @@ int main (int argc, char** argv) t.is (fromString13.minute (), 34, "ctor (std::string) -> N"); t.is (fromString13.second (), 56, "ctor (std::string) -> S"); + // Day of year + t.is (Date ("1/1/2011", "m/d/Y").dayOfYear (), 1, "dayOfYear (1/1/2011) -> 1"); + t.is (Date ("5/1/2011", "m/d/Y").dayOfYear (), 121, "dayOfYear (5/1/2011) -> 121"); + t.is (Date ("12/31/2011", "m/d/Y").dayOfYear (), 365, "dayOfYear (12/31/2011) -> 365"); + // Easter Date e1 (Date::easter(1980)); t.is (e1.toString (), "4/6/1980", "Easter 4/6/1980");