X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Fdatetime.cpp;h=e011f3b2ac0b8a3681048bd1c59e17194dbda03c;hp=26bd7cdb7d59925a0caf076b22dab059e8498a60;hb=HEAD;hpb=d716073b5f2e2fcc518d53ec6ef09c5971b6d0e4 diff --git a/source/time/datetime.cpp b/source/time/datetime.cpp index 26bd7cd..e011f3b 100644 --- a/source/time/datetime.cpp +++ b/source/time/datetime.cpp @@ -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) @@ -107,7 +87,7 @@ void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, un 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})(Z|[-+]([0-9]{2}):([0-9]{2}))$"); + 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) @@ -119,14 +99,24 @@ DateTime DateTime::parse_rfc3339(const string &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); + DateTime result = DateTime(year, month, mday, hr, minute, second, us); int tzoff = 0; - if(m[7].str!="Z") + if(m[9].str!="Z") { - tzoff = lexical_cast(m[8].str)*60+lexical_cast(m[9].str); - if(m[7].str[0]=='-') + tzoff = lexical_cast(m[10].str)*60+lexical_cast(m[11].str); + if(m[9].str[0]=='-') tzoff = -tzoff; } @@ -241,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; } @@ -278,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=='%') { @@ -316,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));