]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add [coarse_]real_time support to ceph_json.h
authorCasey Bodley <cbodley@redhat.com>
Tue, 31 Mar 2020 13:22:46 +0000 (09:22 -0400)
committerCasey Bodley <cbodley@redhat.com>
Mon, 13 Apr 2020 15:06:46 +0000 (11:06 -0400)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/common/ceph_json.cc
src/common/ceph_json.h

index 7250b147c4dc436fdc220508e732558fd09f955d..b5f8756718b0fabc52bcd42efaada32cf619f047 100644 (file)
@@ -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 */
index b22882b173cf3b784c6df52c3ec42ec639d7a6c5..041f52455b8bc35f1985110cf61b3977445ce29d 100644 (file)
@@ -6,6 +6,7 @@
 #include <include/types.h>
 #include <boost/container/flat_map.hpp>
 #include <include/ceph_fs.h>
+#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<class T>
 void decode_json_obj(std::list<T>& 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<class T>
 static void encode_json(const char *name, const std::list<T>& l, ceph::Formatter *f)
 {