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)
unsigned hr = lexical_cast<unsigned>(m[4].str);
unsigned minute = lexical_cast<unsigned>(m[5].str);
unsigned second = lexical_cast<unsigned>(m[6].str);
+ unsigned us = 0;
+ const string &sec_frac = m[8].str;
+ if(!sec_frac.empty())
+ {
+ us = lexical_cast<unsigned>(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<unsigned>(m[8].str)*60+lexical_cast<unsigned>(m[9].str);
- if(m[7].str[0]=='-')
+ tzoff = lexical_cast<unsigned>(m[10].str)*60+lexical_cast<unsigned>(m[11].str);
+ if(m[9].str[0]=='-')
tzoff = -tzoff;
}
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<int>(offs/Time::min));