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