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