JayBeams  0.1
Another project to have fun coding.
timestamp.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_timestamp_hpp
2 #define jb_itch5_timestamp_hpp
3 
6 #include <chrono>
7 #include <iosfwd>
8 
9 namespace jb {
10 namespace itch5 {
11 
12 /**
13  * Represent a ITCH-5.0 timestamp
14  *
15  * ITCH-5.0 uses nanoseconds since midnight for its timestamps.
16  */
17 struct timestamp {
18  std::chrono::nanoseconds ts;
19 };
20 
21 /**
22  * Validate the a timestamp value.
23  *
24  * In ITCH-5.0 messages the timestamp represents nanoseconds since
25  * midnight. The protocol is designed to start new sessions at the
26  * beginning of each day, so timestamps cannot ever be more than 24
27  * hours in nanoseconds.
28  *
29  * @tparam validate unless it is true the function is a no-op.
30  *
31  * @throws std::runtime_error if @a tparam is true and the timestamp
32  * is out of the expected range.
33  */
34 template <bool validate>
36 }
37 
38 /**
39  * Provide an active implementation of
40  * jb::itch5::check_timestamp_range<>
41  */
42 template <>
44 
45 /// Specialize jb::itch5::decoder<> for a timestamp
46 template <bool validate>
47 struct decoder<validate, timestamp> {
48  /// Please see the generic documentation for jb::itch5::decoder<>::r()
49  static timestamp r(std::size_t size, void const* buf, std::size_t offset) {
50  check_offset<validate>("timestamp", size, offset, 6);
51  std::uint64_t hi = decoder<false, std::uint16_t>::r(size, buf, offset);
52  std::uint64_t lo = decoder<false, std::uint32_t>::r(size, buf, offset + 2);
53  timestamp tmp{std::chrono::nanoseconds(hi << 32 | lo)};
54  check_timestamp_range<validate>(tmp);
55 
56  return tmp;
57  }
58 };
59 
60 /// Specialize jb::itch5::encoder<> for a timestamp
61 template <bool validate>
62 struct encoder<validate, timestamp> {
63  /// Please see the generic documentation for jb::itch5::decoder<>::r()
64  static void w(std::size_t size, void* buf, std::size_t offset, timestamp x) {
65  check_offset<validate>("encoder<timestamp>", size, offset, 6);
66  check_timestamp_range<validate>(x);
67  using std::chrono::duration_cast;
68  using std::chrono::nanoseconds;
69  std::uint64_t nanos = duration_cast<nanoseconds>(x.ts).count();
70  std::uint16_t hi = nanos >> 32;
71  std::uint32_t lo = nanos & 0xFFFFFFFF;
72  encoder<false, std::uint16_t>::w(size, buf, offset, hi);
73  encoder<false, std::uint32_t>::w(size, buf, offset + 2, lo);
74  }
75 };
76 
77 /// Streaming operator for jb::itch5::timestamp.
78 std::ostream& operator<<(std::ostream& os, timestamp const& x);
79 
80 } // namespace itch5
81 } // namespace jb
82 
83 #endif // jb_itch5_timestamp_hpp
static timestamp r(std::size_t size, void const *buf, std::size_t offset)
Please see the generic documentation for jb::itch5::decoder<>::r()
Definition: timestamp.hpp:49
static T r(std::size_t size, void const *msg, std::size_t offset)
Read a single message or field.
static void w(std::size_t size, void *msg, std::size_t offset, T const &x)
Write a single message or field to a buffer.
void check_timestamp_range< true >(timestamp const &t)
Provide an active implementation of jb::itch5::check_timestamp_range<>
Definition: timestamp.cpp:8
TODO(#19) all this code should be replaced with Boost.Endian.
Definition: encoder.hpp:13
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24
Represent a ITCH-5.0 timestamp.
Definition: timestamp.hpp:17
void check_timestamp_range(timestamp const &t)
Validate the a timestamp value.
Definition: timestamp.hpp:35
std::chrono::nanoseconds ts
Definition: timestamp.hpp:18
std::ostream & operator<<(std::ostream &os, add_order_message const &x)
Streaming operator for jb::itch5::add_order_message.
static void w(std::size_t size, void *buf, std::size_t offset, timestamp x)
Please see the generic documentation for jb::itch5::decoder<>::r()
Definition: timestamp.hpp:64
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7