timestamp.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <msgpack/object.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. typedef struct msgpack_timestamp {
  17. int64_t tv_sec;
  18. uint32_t tv_nsec;
  19. } msgpack_timestamp;
  20. static inline bool msgpack_object_to_timestamp(const msgpack_object* obj, msgpack_timestamp* ts) {
  21. if (obj->type != MSGPACK_OBJECT_EXT) return false;
  22. if (obj->via.ext.type != -1) return false;
  23. switch (obj->via.ext.size) {
  24. case 4:
  25. ts->tv_nsec = 0;
  26. {
  27. uint32_t v;
  28. _msgpack_load32(uint32_t, obj->via.ext.ptr, &v);
  29. ts->tv_sec = v;
  30. }
  31. return true;
  32. case 8: {
  33. uint64_t value;
  34. _msgpack_load64(uint64_t, obj->via.ext.ptr, &value);
  35. ts->tv_nsec = (uint32_t)(value >> 34);
  36. ts->tv_sec = value & 0x00000003ffffffffLL;
  37. return true;
  38. }
  39. case 12:
  40. _msgpack_load32(uint32_t, obj->via.ext.ptr, &ts->tv_nsec);
  41. _msgpack_load64(int64_t, obj->via.ext.ptr + 4, &ts->tv_sec);
  42. return true;
  43. default:
  44. return false;
  45. }
  46. }
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #endif /* msgpack/timestamp.h */