diff options
| author | Inada Naoki <songofacandy@gmail.com> | 2019-12-05 18:29:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-05 18:29:15 +0900 |
| commit | 641406902efaa8f22f4a7973d04c921a2a35a6be (patch) | |
| tree | 386d7f59b87ce5b798b4a21ac55a3aabb77ddd68 /msgpack/pack_template.h | |
| parent | 2c6668941f72e3bcb797d096437683eca4e3caf5 (diff) | |
| download | msgpack-python-641406902efaa8f22f4a7973d04c921a2a35a6be.tar.gz | |
Add Timestamp support (#382)
Diffstat (limited to 'msgpack/pack_template.h')
| -rw-r--r-- | msgpack/pack_template.h | 33 |
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 |
