]> git.tdb.fi Git - libs/core.git/commitdiff
Add microsecond precision to RFC 3339 dates
authorMikko Rasa <tdb@tdb.fi>
Fri, 28 Oct 2016 21:41:01 +0000 (00:41 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 28 Oct 2016 21:41:01 +0000 (00:41 +0300)
source/time/datetime.cpp

index 26bd7cdb7d59925a0caf076b22dab059e8498a60..e8170cc0246d26393b34d2d102d7005aa6d91e2e 100644 (file)
@@ -107,7 +107,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 +119,24 @@ DateTime DateTime::parse_rfc3339(const string &str)
        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;
        }
 
@@ -316,6 +326,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<int>(offs/Time::min));