summaryrefslogtreecommitdiff
path: root/msgpack/pack_template.h
diff options
context:
space:
mode:
Diffstat (limited to 'msgpack/pack_template.h')
-rw-r--r--msgpack/pack_template.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h
index 69982f4..0e940b8 100644
--- a/msgpack/pack_template.h
+++ b/msgpack/pack_template.h
@@ -759,6 +759,39 @@ static inline int msgpack_pack_ext(msgpack_packer* x, char typecode, size_t l)
}
+/*
+ * Pack Timestamp extension type. Follows msgpack-c pack_template.h.
+ */
+static inline int msgpack_pack_timestamp(msgpack_packer* x, int64_t seconds, uint32_t nanoseconds)
+{
+ if ((seconds >> 34) == 0) {
+ /* seconds is unsigned and fits in 34 bits */
+ uint64_t data64 = ((uint64_t)nanoseconds << 34) | (uint64_t)seconds;
+ if ((data64 & 0xffffffff00000000L) == 0) {
+ /* no nanoseconds and seconds is 32bits or smaller. timestamp32. */
+ unsigned char buf[4];
+ uint32_t data32 = (uint32_t)data64;
+ msgpack_pack_ext(x, -1, 4);
+ _msgpack_store32(buf, data32);
+ msgpack_pack_raw_body(x, buf, 4);
+ } else {
+ /* timestamp64 */
+ unsigned char buf[8];
+ msgpack_pack_ext(x, -1, 8);
+ _msgpack_store64(buf, data64);
+ msgpack_pack_raw_body(x, buf, 8);
+
+ }
+ } else {
+ /* seconds is signed or >34bits */
+ unsigned char buf[12];
+ _msgpack_store32(&buf[0], nanoseconds);
+ _msgpack_store64(&buf[4], seconds);
+ msgpack_pack_ext(x, -1, 12);
+ msgpack_pack_raw_body(x, buf, 12);
+ }
+ return 0;
+}
#undef msgpack_pack_append_buffer