From: Casey Bodley Date: Tue, 31 Mar 2020 13:22:46 +0000 (-0400) Subject: common: add [coarse_]real_time support to ceph_json.h X-Git-Tag: v16.1.0~2586^2~25 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=01c601fb72aadf770893a1f7be9966efdc7e19f8;p=ceph.git common: add [coarse_]real_time support to ceph_json.h Signed-off-by: Casey Bodley --- diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc index 7250b147c4dc..b5f8756718b0 100644 --- a/src/common/ceph_json.cc +++ b/src/common/ceph_json.cc @@ -478,6 +478,34 @@ void decode_json_obj(utime_t& val, JSONObj *obj) } } +void decode_json_obj(ceph::real_time& val, JSONObj *obj) +{ + const std::string& s = obj->get_data(); + uint64_t epoch; + uint64_t nsec; + int r = utime_t::parse_date(s, &epoch, &nsec); + if (r == 0) { + using namespace std::chrono; + val = real_time{seconds(epoch) + nanoseconds(nsec)}; + } else { + throw JSONDecoder::err("failed to decode real_time"); + } +} + +void decode_json_obj(ceph::coarse_real_time& val, JSONObj *obj) +{ + const std::string& s = obj->get_data(); + uint64_t epoch; + uint64_t nsec; + int r = utime_t::parse_date(s, &epoch, &nsec); + if (r == 0) { + using namespace std::chrono; + val = coarse_real_time{seconds(epoch) + nanoseconds(nsec)}; + } else { + throw JSONDecoder::err("failed to decode coarse_real_time"); + } +} + void decode_json_obj(ceph_dir_layout& i, JSONObj *obj){ unsigned tmp; @@ -547,6 +575,16 @@ void encode_json(const char *name, const utime_t& val, Formatter *f) val.gmtime(f->dump_stream(name)); } +void encode_json(const char *name, const ceph::real_time& val, Formatter *f) +{ + encode_json(name, utime_t{val}, f); +} + +void encode_json(const char *name, const ceph::coarse_real_time& val, Formatter *f) +{ + encode_json(name, utime_t{val}, f); +} + void encode_json(const char *name, const bufferlist& bl, Formatter *f) { /* need to copy data from bl, as it is const bufferlist */ diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index b22882b173cf..041f52455b8b 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -6,6 +6,7 @@ #include #include #include +#include "common/ceph_time.h" #include "json_spirit/json_spirit.h" @@ -170,6 +171,9 @@ class utime_t; void decode_json_obj(utime_t& val, JSONObj *obj); void decode_json_obj(ceph_dir_layout& i, JSONObj *obj); +void decode_json_obj(ceph::real_time& val, JSONObj *obj); +void decode_json_obj(ceph::coarse_real_time& val, JSONObj *obj); + template void decode_json_obj(std::list& l, JSONObj *obj) { @@ -487,6 +491,9 @@ void encode_json(const char *name, const utime_t& val, ceph::Formatter *f); void encode_json(const char *name, const ceph::buffer::list& bl, ceph::Formatter *f); void encode_json(const char *name, long long unsigned val, ceph::Formatter *f); +void encode_json(const char *name, const ceph::real_time& val, ceph::Formatter *f); +void encode_json(const char *name, const ceph::coarse_real_time& val, ceph::Formatter *f); + template static void encode_json(const char *name, const std::list& l, ceph::Formatter *f) {