X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Fdatetime.cpp;h=e011f3b2ac0b8a3681048bd1c59e17194dbda03c;hp=bf824112ac9e202a97d3f0158c42d8bc86d79286;hb=HEAD;hpb=c7afef88380ebebc8c2b04e48664d73281ec8848 diff --git a/source/time/datetime.cpp b/source/time/datetime.cpp index bf82411..e011f3b 100644 --- a/source/time/datetime.cpp +++ b/source/time/datetime.cpp @@ -1,9 +1,9 @@ #include #include #include +#include #include "datetime.h" #include "timestamp.h" -#include "units.h" using namespace std; @@ -29,7 +29,7 @@ inline unsigned char month_days(int y, unsigned char m) } template -inline int cmp_(T a, T b) +inline int _cmp(T a, T b) { if(a=1000000) throw out_of_range("DateTime::DateTime usec"); if(second>=60) @@ -105,6 +85,46 @@ void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, un throw out_of_range("DateTime::DateTime mday"); } +DateTime DateTime::parse_rfc3339(const string &str) +{ + static Regex re("^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(.([0-9]+))?(Z|[-+]([0-9]{2}):([0-9]{2}))$"); + + RegMatch m = re.match(str); + if(!m) + throw invalid_argument("DateTime::parse_rfc3339"); + + unsigned year = lexical_cast(m[1].str); + unsigned month = lexical_cast(m[2].str); + unsigned mday = lexical_cast(m[3].str); + unsigned hr = lexical_cast(m[4].str); + unsigned minute = lexical_cast(m[5].str); + unsigned second = lexical_cast(m[6].str); + unsigned us = 0; + const string &sec_frac = m[8].str; + if(!sec_frac.empty()) + { + us = lexical_cast(sec_frac); + for(unsigned i=sec_frac.size(); i<6; ++i) + us *= 10; + for(unsigned i=sec_frac.size(); i>6; --i) + us /= 10; + } + + DateTime result = DateTime(year, month, mday, hr, minute, second, us); + + int tzoff = 0; + if(m[9].str!="Z") + { + tzoff = lexical_cast(m[10].str)*60+lexical_cast(m[11].str); + if(m[9].str[0]=='-') + tzoff = -tzoff; + } + + result.set_timezone(tzoff); + + return result; +} + void DateTime::add_days(int days) { int new_year = year; @@ -211,19 +231,19 @@ DateTime &DateTime::operator-=(const TimeDelta &td) int DateTime::cmp(const DateTime &dt) const { - if(int c = cmp_(year, dt.year)) + if(int c = _cmp(year, dt.year)) return c; - if(int c = cmp_(month, dt.month)) + if(int c = _cmp(month, dt.month)) return c; - if(int c = cmp_(mday, dt.mday)) + if(int c = _cmp(mday, dt.mday)) return c; - if(int c = cmp_(hour, dt.hour)) + if(int c = _cmp(hour, dt.hour)) return c; - if(int c = cmp_(minute, dt.minute)) + if(int c = _cmp(minute, dt.minute)) return c; - if(int c = cmp_(second, dt.second)) + if(int c = _cmp(second, dt.second)) return c; - if(int c = cmp_(usec, dt.usec)) + if(int c = _cmp(usec, dt.usec)) return c; return 0; } @@ -248,7 +268,7 @@ TimeStamp DateTime::get_timestamp() const string DateTime::format(const string &fmt) const { string result; - for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i) + for(auto i=fmt.begin(); i!=fmt.end(); ++i) { if(*i=='%') { @@ -286,6 +306,8 @@ string DateTime::format(const string &fmt) const string DateTime::format_rfc3339() const { string result = format("%Y-%m-%dT%H:%M:%S"); + if(usec) + result += Msp::format(".%06d", usec); if(const TimeDelta &offs = zone.get_offset()) { int m = abs(static_cast(offs/Time::min));