OnixS C++ eSpeed ITCH Market Data Handler  1.7.3
API documentation
Timestamp.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 #pragma once
20 
21 #include <string>
22 
25 
26 namespace OnixS { namespace HandlerCore { namespace Common { struct TimeHelper; }}}
27 
29 
30 /// Defines all the months in the year.
31 struct ONIXS_ESPEED_ITCH_API Month
32 {
33  /// Defines all the months in the year.
34  enum Enum
35  {
41  May,
48  December
49  };
50 };
51 
52 /// Year, month, day fields.
53 struct ONIXS_ESPEED_ITCH_API YearMonthDay
54 {
55  /// Year
56  unsigned year;
57 
58  /// Month
60 
61  ///Day
62  unsigned day;
63 };
64 
65 /// Collection of timestamp formats supported.
66 struct ONIXS_ESPEED_ITCH_API TimestampFormat
67 {
68  enum Enum
69  {
70  /// Indicates timestamp in "YYYYMMDD" format.
72 
73  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
75 
76  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
78 
79  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
80  YYYYMMDDHHMMSSNsec
81  };
82 };
83 
84 /// 4 most significant bytes – seconds since Epoch
85 /// 4 least significant bytes – nanoseconds within second
86 typedef UInt64 RawTimestamp;
87 
88 /// Represents timestamp without time-zone information.
89 class ONIXS_ESPEED_ITCH_API Timestamp
90 {
91  public:
92  /// Initializes as Jan 1, 0001, 00:00:00.
93  Timestamp();
94 
95  /// Initializes date-time from all details.
96  Timestamp (
97  unsigned int year, Month::Enum month, unsigned int day,
98  unsigned int hour = 0, unsigned int minute = 0, unsigned int second = 0,
99  unsigned int nanosecond = 0);
100 
101  /// Initializes date-time from raw presentation provided by the exchange
102  /// 4 most significant bytes – seconds since Epoch
103  /// 4 least significant bytes – nanoseconds within second
104  explicit
106 
107  /// Year component of timestamp
108  unsigned year() const;
109 
110  /// Month component of timestamp
111  Month::Enum month() const;
112 
113  /// day of month component of timestamp
114  /// Valid values are 1 through 31.
115  unsigned int day() const;
116 
117  /// Returns date part of timestamp.
118  void date (YearMonthDay&) const;
119 
120  /// Hour component of timestamp.
121  /// Valid values are 0 through 23.
122  unsigned int hour() const;
123 
124  /// Minute component of timestamp
125  /// Valid values are 0 through 59.
126  unsigned int minute() const;
127 
128  /// Second component of timestamp
129  /// Valid values are 0 through 59.
130  unsigned int second() const;
131 
132  /// Millisecond component of timestamp
133  /// Valid values are 0 through 999.
134  unsigned int millisecond() const;
135 
136  /// Microsecond component of timestamp
137  /// Valid values are 0 through 999999.
138  unsigned int microsecond() const;
139 
140  /// Nanosecond component of timestamp
141  /// Valid values are 0 through 999999999.
142  unsigned int nanosecond() const;
143 
144  /// Time span since Jan 1, 0001, 00:00:00.
145  const TimeSpan& sinceEpoch() const;
146 
147  /// Epoch
148  static Timestamp epoch();
149 
150  /// Compares instance with another one.
151  bool operator == (const Timestamp&) const;
152 
153  /// Compares instance with another one.
154  bool operator != (const Timestamp&) const;
155 
156  /// Tests whether instance is less than another one.
157  bool operator < (const Timestamp&) const;
158 
159  /// Returns timestamp text presentation in requested
160  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
161  std::string
162  toString (
165 
166  /// Appends timestamp text presentation in requested
167  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
168  void
169  toString (
170  std::string& str,
173 
174  /// Returns the current UTC time.
175  ///
176  /// @note Timestamp resolution depends on
177  /// capabilities of an operating system.
178  static Timestamp utcNow();
179 
180  /// Returns the current local time.
181  ///
182  /// @note Timestamp resolution depends on
183  /// capabilities of an operating system.
184  static Timestamp now();
185 
186  /// Parses timestamp from its text presentation assuming it's in
187  /// specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
188  static
189  Timestamp
190  parse (
191  const std::string&,
194 
195  /// Parses timestamp from its numeric presentation
196  static
197  Timestamp
198  parse (
199  unsigned long long presentation,
200  TimestampFormat::Enum format);
201 
202  private:
203  Timestamp (long long totalSeconds, int subseconds);
204 
205  friend struct HandlerCore::Common::TimeHelper;
206  TimeSpan sinceEpoch_;
207 };
208 
209 inline
211  long long totalSeconds, int subseconds)
212  : sinceEpoch_ (totalSeconds, subseconds)
213 
214 {
215 }
216 
217 inline
218 unsigned int
220 {
221  return sinceEpoch_.milliseconds();
222 }
223 
224 inline
225 unsigned int
227 {
228  return sinceEpoch_.microseconds();
229 }
230 
231 inline
232 unsigned int
234 {
235  return sinceEpoch_.nanoseconds();
236 }
237 
238 inline
239 const TimeSpan&
241 {
242  return sinceEpoch_;
243 }
244 
245 inline
246 std::string
248  TimestampFormat::Enum format) const
249 {
250  std::string str;
251 
252  toString (str, format);
253 
254  return str;
255 }
256 
257 inline
258 TimeSpan
260  const Timestamp& left,
261  const Timestamp& right)
262 {
263  TimeSpan res (left.sinceEpoch() );
264  return res -= right.sinceEpoch();
265 }
266 
unsigned int millisecond() const
Definition: Timestamp.h:219
Defines all the months in the year.
Definition: Timestamp.h:31
#define ONIXS_ESPEED_ITCH_NAMESPACE_END
Definition: Bootstrap.h:31
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:414
TimeSpan operator-(const Timestamp &left, const Timestamp &right)
Definition: Timestamp.h:259
#define ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
Collection of timestamp formats supported.
Definition: Timestamp.h:66
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:77
UInt64 RawTimestamp
Definition: Timestamp.h:86
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Definition: Timestamp.h:247
int microseconds() const
int nanoseconds() const
Definition: TimeSpan.h:157
bool operator!=(const StrRef &left, const StrRef &right)
Compares with another instance.
Definition: String.h:324
unsigned int nanosecond() const
Definition: Timestamp.h:233
Year, month, day fields.
Definition: Timestamp.h:53
int milliseconds() const
Month::Enum month
Month.
Definition: Timestamp.h:59
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
Definition: Timestamp.h:80
Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:74
Timestamp()
Initializes as Jan 1, 0001, 00:00:00.
unsigned year
Year.
Definition: Timestamp.h:56
Indicates timestamp in "YYYYMMDD" format.
Definition: Timestamp.h:71
const TimeSpan & sinceEpoch() const
Time span since Jan 1, 0001, 00:00:00.
Definition: Timestamp.h:240
unsigned int microsecond() const
Definition: Timestamp.h:226
bool operator==(const StrRef &left, const StrRef &right)
Compares StrRef instance with another one.
Definition: String.h:310
unsigned day
Day.
Definition: Timestamp.h:62
Enum
Defines all the months in the year.
Definition: Timestamp.h:34
Represents timestamp without time-zone information.
Definition: Timestamp.h:89