OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
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 
24 
25 namespace OnixS { namespace HandlerCore { namespace Common { struct TimeHelper; }}}
26 
27 ONIXS_SGXTITAN_ITCH_NAMESPACE_BEGIN
28 
29 /// Defines all the months in the year.
30 struct ONIXS_SGXTITAN_ITCH_API Month
31 {
32  /// Defines all the months in the year.
33  enum Enum
34  {
40  May,
47  December
48  };
49 };
50 
51 /// Year, month, day fields.
52 struct ONIXS_SGXTITAN_ITCH_API YearMonthDay
53 {
54  /// Year
55  unsigned year;
56 
57  /// Month
59 
60  ///Day
61  unsigned day;
62 };
63 
64 /// Collection of timestamp formats supported.
65 struct ONIXS_SGXTITAN_ITCH_API TimestampFormat
66 {
67  enum Enum
68  {
69  /// Indicates timestamp in "YYYYMMDD" format.
71 
72  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
74 
75  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
77 
78  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
79  YYYYMMDDHHMMSSNsec
80  };
81 };
82 
83 /// Represents timestamp without time-zone information.
84 class ONIXS_SGXTITAN_ITCH_API Timestamp
85 {
86  public:
87  /// Initializes as Jan 1, 0001, 00:00:00.
88  Timestamp();
89 
90  /// Initializes date-time from all details.
91  Timestamp (
92  unsigned int year, Month::Enum month, unsigned int day,
93  unsigned int hour = 0, unsigned int minute = 0, unsigned int second = 0,
94  unsigned int nanosecond = 0);
95 
96  /// Year component of timestamp
97  unsigned year() const;
98 
99  /// Month component of timestamp
100  Month::Enum month() const;
101 
102  /// day of month component of timestamp
103  /// Valid values are 1 through 31.
104  unsigned int day() const;
105 
106  /// Returns date part of timestamp.
107  void date (YearMonthDay&) const;
108 
109  /// Hour component of timestamp.
110  /// Valid values are 0 through 23.
111  unsigned int hour() const;
112 
113  /// Minute component of timestamp
114  /// Valid values are 0 through 59.
115  unsigned int minute() const;
116 
117  /// Second component of timestamp
118  /// Valid values are 0 through 59.
119  unsigned int second() const;
120 
121  /// Millisecond component of timestamp
122  /// Valid values are 0 through 999.
123  unsigned int millisecond() const;
124 
125  /// Microsecond component of timestamp
126  /// Valid values are 0 through 999999.
127  unsigned int microsecond() const;
128 
129  /// Nanosecond component of timestamp
130  /// Valid values are 0 through 999999999.
131  unsigned int nanosecond() const;
132 
133  /// Time span since Jan 1, 0001, 00:00:00.
134  const TimeSpan& sinceEpoch() const;
135 
136  /// Epoch
137  static Timestamp epoch();
138 
139  /// Compares instance with another one.
140  bool operator == (const Timestamp&) const;
141 
142  /// Compares instance with another one.
143  bool operator != (const Timestamp&) const;
144 
145  /// Tests whether instance is less than another one.
146  bool operator < (const Timestamp&) const;
147 
148  /// Returns timestamp text presentation in requested
149  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
150  std::string
151  toString (
153  TimestampFormat::YYYYMMDDHHMMSSNsec) const;
154 
155  /// Appends timestamp text presentation in requested
156  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
157  void
158  toString (
159  std::string& str,
161  TimestampFormat::YYYYMMDDHHMMSSNsec) const;
162 
163  /// Returns the current UTC time.
164  ///
165  /// @note Timestamp resolution depends on
166  /// capabilities of an operating system.
167  static Timestamp utcNow();
168 
169  /// Returns the current local time.
170  ///
171  /// @note Timestamp resolution depends on
172  /// capabilities of an operating system.
173  static Timestamp now();
174 
175  /// Parses timestamp from its text presentation assuming it's in
176  /// specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
177  static
178  Timestamp
179  parse (
180  const std::string&,
182  TimestampFormat::YYYYMMDDHHMMSSNsec);
183 
184  /// Parses timestamp from its numeric presentation
185  static
186  Timestamp
187  parse (
188  unsigned long long presentation,
189  TimestampFormat::Enum format);
190 
191  private:
192  Timestamp (long long totalSeconds, int subseconds);
193 
194  friend struct HandlerCore::Common::TimeHelper;
195  TimeSpan sinceEpoch_;
196 };
197 
198 inline
199 Timestamp::Timestamp (
200  long long totalSeconds, int subseconds)
201  : sinceEpoch_ (totalSeconds, subseconds)
202 
203 {
204 }
205 
206 inline
207 unsigned int
209 {
210  return sinceEpoch_.milliseconds();
211 }
212 
213 inline
214 unsigned int
216 {
217  return sinceEpoch_.microseconds();
218 }
219 
220 inline
221 unsigned int
223 {
224  return sinceEpoch_.nanoseconds();
225 }
226 
227 inline
228 const TimeSpan&
230 {
231  return sinceEpoch_;
232 }
233 
234 inline
235 std::string
237  TimestampFormat::Enum format) const
238 {
239  std::string str;
240 
241  toString (str, format);
242 
243  return str;
244 }
245 
246 inline
247 TimeSpan
249  const Timestamp& left,
250  const Timestamp& right)
251 {
252  TimeSpan res (left.sinceEpoch() );
253  return res -= right.sinceEpoch();
254 }
255 
256 ONIXS_SGXTITAN_ITCH_NAMESPACE_END
Represents timestamp without time-zone information.
Definition: Timestamp.h:84
Collection of timestamp formats supported.
Definition: Timestamp.h:65
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Definition: Timestamp.h:236
TimeSpan operator-(const Timestamp &left, const Timestamp &right)
Definition: Timestamp.h:248
Enum
Defines all the months in the year.
Definition: Timestamp.h:33
const TimeSpan & sinceEpoch() const
Time span since Jan 1, 0001, 00:00:00.
Definition: Timestamp.h:229
Indicates timestamp in "YYYYMMDD" format.
Definition: Timestamp.h:70
Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:73
bool operator!=(const Price &left, const Price &right)
Definition: Price.h:93
bool operator<(const Price &left, const Price &right)
Definition: Price.h:102
Defines all the months in the year.
Definition: Timestamp.h:30
bool operator==(const Price &left, const Price &right)
Definition: Price.h:84
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:76