Skip to content

Commit

Permalink
Use own date formatting code to avoid std::locale overhead.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebubuth committed Feb 20, 2014
1 parent 717916c commit 159cd1c
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/xml_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,50 @@ std::string popen_command(const std::string &file_name, const boost::program_opt
return command.str();
}

// profiling revealed that conversion to a time was a hotspot in the
// code - not the conversion itself, but the allocation and setup of
// the locale objects used to do the formatting. since we want an
// ISO standard string, in zulu time, always then we don't need any
// of that overhead.
std::string fmt_iso_time(const pt::ptime &t) {
std::string s;
if (!t.is_special()) {
// 00000000001111111111
// 01234567890123456789
// format is YYYY-mm-ddTHH:MM:SSZ
s.resize(21);

const boost::gregorian::greg_year_month_day ymd = t.date().year_month_day();
const pt::time_duration tod = t.time_of_day();
const long hour = tod.hours();
const long minute = tod.minutes();
const long second = tod.seconds();

s[ 0] = '0' + ((ymd.year / 1000) % 10);
s[ 1] = '0' + ((ymd.year / 100) % 10);
s[ 2] = '0' + ((ymd.year / 10) % 10);
s[ 3] = '0' + ((ymd.year ) % 10);
s[ 4] = '-';
s[ 5] = (ymd.month >= 10) ? '1' : '0';
s[ 6] = '0' + (ymd.month % 10);
s[ 7] = '-';
s[ 8] = '0' + ((ymd.day / 10) % 10);
s[ 9] = '0' + (ymd.day % 10);
s[10] = 'T';
s[11] = '0' + ((hour / 10) % 10);
s[12] = '0' + (hour % 10);
s[13] = ':';
s[14] = '0' + ((minute / 10) % 10);
s[15] = '0' + (minute % 10);
s[16] = ':';
s[17] = '0' + ((second / 10) % 10);
s[18] = '0' + (second % 10);
s[19] = 'Z';
s[20] = '\0';
}
return s;
}

} // anonymous namespace

struct xml_writer::pimpl {
Expand Down Expand Up @@ -230,7 +274,7 @@ void xml_writer::pimpl::attribute(const char *name, double d) {
}

void xml_writer::pimpl::attribute(const char *name, const pt::ptime &t) {
std::string ts = pt::to_iso_extended_string(t) + "Z";
std::string ts = fmt_iso_time(t);
if (xmlTextWriterWriteAttribute(m_writer,
BAD_CAST name,
BAD_CAST ts.c_str()) < 0) {
Expand Down

0 comments on commit 159cd1c

Please sign in to comment.