diff options
| author | Carl Hetherington <cth@carlh.net> | 2023-02-02 01:02:50 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2023-02-02 01:02:50 +0100 |
| commit | b1464249c7b403a66ca63f403c80c39f1af63e65 (patch) | |
| tree | 966daea7402bf5c2454c1b5ff0967768d2acd481 | |
| parent | c89ed445b9fcd4d6c76cbf3278dd2d7d7e2e0a42 (diff) | |
Add with_timezone parameter to ::to_string().
| -rw-r--r-- | src/local_time.cc | 18 | ||||
| -rw-r--r-- | src/local_time.h | 6 | ||||
| -rw-r--r-- | test/local_time_test.cc | 26 |
3 files changed, 44 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; diff --git a/test/local_time_test.cc b/test/local_time_test.cc index 63083947..8aedd47b 100644 --- a/test/local_time_test.cc +++ b/test/local_time_test.cc @@ -88,6 +88,32 @@ BOOST_AUTO_TEST_CASE (local_time_basic_test) } { + dcp::LocalTime t ("2011-11-20T01:06:59.456-09:30"); + BOOST_CHECK_EQUAL (t._year, 2011); + BOOST_CHECK_EQUAL (t._month, 11); + BOOST_CHECK_EQUAL (t._day, 20); + BOOST_CHECK_EQUAL (t._hour, 1); + BOOST_CHECK_EQUAL (t._minute, 6); + BOOST_CHECK_EQUAL (t._second, 59); + BOOST_CHECK_EQUAL (t._millisecond, 456); + BOOST_CHECK(t._offset == dcp::UTCOffset(-9, -30)); + BOOST_CHECK_EQUAL (t.as_string(true, false), "2011-11-20T01:06:59.456"); + } + + { + dcp::LocalTime t ("2011-11-20T01:06:59.456-09:30"); + BOOST_CHECK_EQUAL (t._year, 2011); + BOOST_CHECK_EQUAL (t._month, 11); + BOOST_CHECK_EQUAL (t._day, 20); + BOOST_CHECK_EQUAL (t._hour, 1); + BOOST_CHECK_EQUAL (t._minute, 6); + BOOST_CHECK_EQUAL (t._second, 59); + BOOST_CHECK_EQUAL (t._millisecond, 456); + BOOST_CHECK(t._offset == dcp::UTCOffset(-9, -30)); + BOOST_CHECK_EQUAL (t.as_string(false, false), "2011-11-20T01:06:59"); + } + + { /* Construction from boost::posix_time::ptime */ dcp::LocalTime b (boost::posix_time::time_from_string ("2002-01-20 19:03:56")); BOOST_CHECK_EQUAL (b._year, 2002); |
