ISO8601: Began merge of Duration and ISO8601p

This commit is contained in:
Paul Beckingham 2015-08-09 16:33:21 -04:00
parent 9cd10a5c1e
commit 6dc7244fa6
2 changed files with 98 additions and 2 deletions

View file

@ -25,6 +25,8 @@
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <sstream>
#include <stdlib.h>
#include <Lexer.h>
#include <ISO8601.h>
#include <Date.h>
@ -513,6 +515,60 @@ ISO8601p::~ISO8601p ()
{
}
////////////////////////////////////////////////////////////////////////////////
ISO8601p::ISO8601p (const std::string& input)
{
if (Lexer::isAllDigits (input))
{
time_t value = (time_t) strtol (input.c_str (), NULL, 10);
if (value == 0 || value > 60)
{
_value = value;
return;
}
}
std::string::size_type idx = 0;
parse (input, idx);
}
////////////////////////////////////////////////////////////////////////////////
ISO8601p& ISO8601p::operator= (const ISO8601p& other)
{
if (this != &other)
{
_year = other._year;
_month = other._month;
_day = other._day;
_hours = other._hours;
_minutes = other._minutes;
_seconds = other._seconds;
_value = other._value;
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
bool ISO8601p::operator< (const ISO8601p& other)
{
return _value < other._value;
}
////////////////////////////////////////////////////////////////////////////////
bool ISO8601p::operator> (const ISO8601p& other)
{
return _value > other._value;
}
////////////////////////////////////////////////////////////////////////////////
ISO8601p::operator std::string () const
{
std::stringstream s;
s << _value;
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
ISO8601p::operator time_t () const
{
@ -576,6 +632,41 @@ void ISO8601p::clear ()
_value = 0;
}
////////////////////////////////////////////////////////////////////////////////
const std::string ISO8601p::format () const
{
if (_value)
{
time_t t = _value;
int seconds = t % 60; t /= 60;
int minutes = t % 60; t /= 60;
int hours = t % 24; t /= 24;
int days = t % 30; t /= 30;
int months = t % 12; t /= 12;
int years = t;
std::stringstream s;
s << 'P';
if (years) s << years << 'Y';
if (months) s << months << 'M';
if (days) s << days << 'D';
if (hours || minutes || seconds)
{
s << 'T';
if (hours) s << hours << 'H';
if (minutes) s << minutes << 'M';
if (seconds) s << seconds << 'S';
}
return s.str ();
}
else
{
return "PT0S";
}
}
////////////////////////////////////////////////////////////////////////////////
// 'P' [nn 'Y'] [nn 'M'] [nn 'D'] ['T' [nn 'H'] [nn 'M'] [nn 'S']]
bool ISO8601p::parse_designated (Nibbler& n)

View file

@ -73,10 +73,15 @@ public:
ISO8601p ();
~ISO8601p ();
ISO8601p (const ISO8601p&); // Unimplemented
ISO8601p& operator= (const ISO8601p&); // Unimplemented
ISO8601p (const std::string&);
ISO8601p& operator= (const ISO8601p&);
bool operator< (const ISO8601p&);
bool operator> (const ISO8601p&);
operator std::string () const;
operator time_t () const;
bool parse (const std::string&, std::string::size_type&);
void clear ();
const std::string format () const;
private:
bool parse_designated (Nibbler&);