From b7aa16da4bbb444f6361e4f14be7ef64b0ea3bb1 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Wed, 15 Nov 2023 15:29:35 -0500 Subject: [PATCH] encoding: add round_trip_encode()/decode() for chrono types the default encodings for chrono types were made to be backward-compatible with utime_t, so truncated seconds to 32 bits adds new functions that encode these chrono types using their underlying representation, which for ceph::real_time and ceph::timespan is 'nanoseconds as uint64_t' Signed-off-by: Casey Bodley --- src/include/encoding.h | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/include/encoding.h b/src/include/encoding.h index 40ba9d39c76f8..aac63a7e6cc93 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -322,7 +322,9 @@ inline void decode_nohead(int len, bufferlist& s, bufferlist::const_iterator& p) p.copy(len, s); } -// Time, since the templates are defined in std::chrono +// Time, since the templates are defined in std::chrono. The default encodings +// for time_point and duration are backward-compatible with utime_t, but +// truncate seconds to 32 bits so are not guaranteed to round-trip. template>* = nullptr> @@ -373,6 +375,40 @@ void decode(std::chrono::duration& d, d = std::chrono::seconds(s) + std::chrono::nanoseconds(ns); } +// Provide encodings for chrono::time_point and duration that use +// the underlying representation so are guaranteed to round-trip. + +template >* = nullptr> +void round_trip_encode(const std::chrono::duration& d, + ceph::bufferlist &bl) { + const Rep r = d.count(); + encode(r, bl); +} + +template >* = nullptr> +void round_trip_decode(std::chrono::duration& d, + bufferlist::const_iterator& p) { + Rep r; + decode(r, p); + d = std::chrono::duration(r); +} + +template +void round_trip_encode(const std::chrono::time_point& t, + ceph::bufferlist &bl) { + round_trip_encode(t.time_since_epoch(), bl); +} + +template +void round_trip_decode(std::chrono::time_point& t, + bufferlist::const_iterator& p) { + Duration dur; + round_trip_decode(dur, p); + t = std::chrono::time_point(dur); +} + // ----------------------------- // STL container types -- 2.39.5