diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/local_time.cc | 18 | ||||
| -rw-r--r-- | src/local_time.h | 6 |
2 files changed, 18 insertions, 6 deletions
diff --git a/src/local_time.cc b/src/local_time.cc index 0f87bb49..cadb7137 100644 --- a/src/local_time.cc +++ b/src/local_time.cc @@ -182,14 +182,24 @@ LocalTime::LocalTime (string s) string -LocalTime::as_string (bool with_millisecond) const +LocalTime::as_string(bool with_millisecond, bool with_timezone) const { char buffer[32]; - snprintf ( + + auto const written = snprintf( buffer, sizeof (buffer), - "%sT%s%s%02d:%02d", - date().c_str(), time_of_day(true, with_millisecond).c_str(), (_offset.hour() >= 0 ? "+" : "-"), abs(_offset.hour()), abs(_offset.minute()) + "%sT%s", + date().c_str(), time_of_day(true, with_millisecond).c_str() ); + + DCP_ASSERT(written < 32); + + if (with_timezone) { + snprintf( + buffer + written, sizeof(buffer) - written, + "%s%02d:%02d", (_offset.hour() >= 0 ? "+" : "-"), abs(_offset.hour()), abs(_offset.minute()) + ); + } return buffer; } diff --git a/src/local_time.h b/src/local_time.h index 7c91db3e..c76b876c 100644 --- a/src/local_time.h +++ b/src/local_time.h @@ -92,8 +92,10 @@ public: , _offset(offset) {} - /** @return A string of the form 2013-01-05T18:06:59+04:00 or 2013-01-05T18:06:59.123+04:00 */ - std::string as_string (bool with_millisecond = false) const; + /** @return A string of the form 2013-01-05T18:06:59+04:00, 2013-01-05T18:06:59.123+04:00 + * 2013-01-05T18:06:59 or 2013-01-05T18:06:59.123 + */ + std::string as_string(bool with_millisecond = false, bool with_timezone = true) const; /** @return The date in the form YYYY-MM-DD */ std::string date () const; |
