| 1 | /* |
| 2 | * MessagePack for C TimeStamp |
| 3 | * |
| 4 | * Copyright (C) 2018 KONDO Takatoshi |
| 5 | * |
| 6 | * Distributed under the Boost Software License, Version 1.0. |
| 7 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | * http://www.boost.org/LICENSE_1_0.txt) |
| 9 | */ |
| 10 | #ifndef MSGPACK_TIMESTAMP_H |
| 11 | #define MSGPACK_TIMESTAMP_H |
| 12 | |
| 13 | #include <vendor/msgpack/object.h> |
| 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | typedef struct msgpack_timestamp { |
| 21 | int64_t tv_sec; |
| 22 | uint32_t tv_nsec; |
| 23 | } msgpack_timestamp; |
| 24 | |
| 25 | static inline bool msgpack_object_to_timestamp(const msgpack_object* obj, msgpack_timestamp* ts) { |
| 26 | if (obj->type != MSGPACK_OBJECT_EXT) return false; |
| 27 | if (obj->via.ext.type != -1) return false; |
| 28 | switch (obj->via.ext.size) { |
| 29 | case 4: |
| 30 | ts->tv_nsec = 0; |
| 31 | { |
| 32 | uint32_t v; |
| 33 | _msgpack_load32(uint32_t, obj->via.ext.ptr, &v); |
| 34 | ts->tv_sec = v; |
| 35 | } |
| 36 | return true; |
| 37 | case 8: { |
| 38 | uint64_t value; |
| 39 | _msgpack_load64(uint64_t, obj->via.ext.ptr, &value); |
| 40 | ts->tv_nsec = (uint32_t)(value >> 34); |
| 41 | ts->tv_sec = value & 0x00000003ffffffffLL; |
| 42 | return true; |
| 43 | } |
| 44 | case 12: |
| 45 | _msgpack_load32(uint32_t, obj->via.ext.ptr, &ts->tv_nsec); |
| 46 | _msgpack_load64(int64_t, obj->via.ext.ptr + 4, &ts->tv_sec); |
| 47 | return true; |
| 48 | default: |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | #endif /* msgpack/timestamp.h */ |