ISO8601: Added formatted date parsing

This commit is contained in:
Paul Beckingham 2015-09-27 00:18:38 -04:00
parent b8977c4dd0
commit dbb6a209a8
3 changed files with 257 additions and 4 deletions

View file

@ -119,6 +119,17 @@ ISO8601d::ISO8601d ()
_date = time (NULL);
}
////////////////////////////////////////////////////////////////////////////////
ISO8601d::ISO8601d (const std::string& input, const std::string& format /*= ""*/)
{
clear ();
std::string::size_type start = 0;
if (parse (input, start, format))
return;
throw ::format (STRING_DATE_INVALID_FORMAT, input, format);
}
////////////////////////////////////////////////////////////////////////////////
ISO8601d::ISO8601d (const time_t t)
{
@ -222,8 +233,20 @@ bool ISO8601d::parse (
const std::string& format /* = "" */)
{
auto i = start;
Nibbler n (input.substr (i));
// Look for a formatted date, if a format is present.
if (format != "")
{
Nibbler n (input.substr (i));
if (n.getDate (format, _date))
{
start = n.cursor ();
return true;
}
}
// Look for an ISO date.
Nibbler n (input.substr (i));
if (parse_date_time (n) || // Strictest first.
parse_date_time_ext (n) ||
parse_date_ext (n) ||

View file

@ -38,11 +38,11 @@ public:
static int minimumMatchLength;
ISO8601d ();
ISO8601d (const std::string&, const std::string& format = "");
ISO8601d (time_t);
ISO8601d (const int, const int, const int);
ISO8601d (const int, const int, const int, const int, const int, const int);
~ISO8601d ();
ISO8601d (const ISO8601d&); // Unimplemented
operator time_t () const;
bool parse (const std::string&, std::string::size_type&, const std::string& format = "");