+void DateTime::init(const TimeStamp &ts)
+{
+ year = 1970;
+ month = 1;
+ mday = 1;
+ hour = 0;
+ minute = 0;
+ second = 0;
+ usec = 0;
+ add_raw(ts.raw());
+}
+
+void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
+{
+ year = y;
+ month = m;
+ mday = d;
+ hour = h;
+ minute = n;
+ second = s;
+ usec = u;
+
+ if(usec>=1000000)
+ throw out_of_range("DateTime::DateTime usec");
+ if(second>=60)
+ throw out_of_range("DateTime::DateTime second");
+ if(minute>=60)
+ throw out_of_range("DateTime::DateTime minute");
+ if(hour>=24)
+ throw out_of_range("DateTime::DateTime hour");
+ if(month<1 || month>12)
+ throw out_of_range("DateTime::DateTime month");
+ if(mday<1 || mday>month_days(year, month))
+ 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<unsigned>(m[1].str);
+ unsigned month = lexical_cast<unsigned>(m[2].str);
+ unsigned mday = lexical_cast<unsigned>(m[3].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, us);
+
+ int tzoff = 0;
+ if(m[9].str!="Z")
+ {
+ tzoff = lexical_cast<unsigned>(m[10].str)*60+lexical_cast<unsigned>(m[11].str);
+ if(m[9].str[0]=='-')
+ tzoff = -tzoff;
+ }
+
+ result.set_timezone(tzoff);
+
+ return result;
+}
+