From 6c02c03674e0d3157b2fc121a7f7b6eb979b2f65 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 26 Sep 2015 20:51:04 -0400 Subject: [PATCH] ISO8601d: Added various ::valid methods --- src/ISO8601.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ISO8601.h | 4 +++ 2 files changed, 71 insertions(+) diff --git a/src/ISO8601.cpp b/src/ISO8601.cpp index 38e5ea59b..7c99ed86b 100644 --- a/src/ISO8601.cpp +++ b/src/ISO8601.cpp @@ -972,6 +972,73 @@ ISO8601d ISO8601d::startOfYear () const return ISO8601d (1, 1, year ()); } +//////////////////////////////////////////////////////////////////////////////// +/* +bool ISO8601d::valid (const std::string& input, const std::string& format) +{ + try + { + ISO8601d test (input, format); + } + + catch (...) + { + return false; + } + + return true; +} +*/ + +//////////////////////////////////////////////////////////////////////////////// +bool ISO8601d::valid (const int m, const int d, const int y, const int hr, + const int mi, const int se) +{ + if (hr < 0 || hr > 23) + return false; + + if (mi < 0 || mi > 59) + return false; + + if (se < 0 || se > 59) + return false; + + return ISO8601d::valid (m, d, y); +} + +//////////////////////////////////////////////////////////////////////////////// +bool ISO8601d::valid (const int m, const int d, const int y) +{ + // Check that the year is valid. + if (y < 0) + return false; + + // Check that the month is valid. + if (m < 1 || m > 12) + return false; + + // Finally check that the days fall within the acceptable range for this + // month, and whether or not this is a leap year. + if (d < 1 || d > ISO8601d::daysInMonth (m, y)) + return false; + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// Julian +bool ISO8601d::valid (const int d, const int y) +{ + // Check that the year is valid. + if (y < 0) + return false; + + if (d < 1 || d > ISO8601d::daysInYear (y)) + return false; + + return true; +} + //////////////////////////////////////////////////////////////////////////////// // Static bool ISO8601d::leapYear (int year) diff --git a/src/ISO8601.h b/src/ISO8601.h index bb7c8e8af..94dc69427 100644 --- a/src/ISO8601.h +++ b/src/ISO8601.h @@ -58,6 +58,10 @@ public: ISO8601d startOfMonth () const; ISO8601d 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); + static bool valid (const int, const int); static bool leapYear (int); static int daysInMonth (int, int); static int daysInYear (int);