OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
API documentation
Timestamp.cpp
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 #include <util/Converter.h>
20 
21 #include <OnixS/Core/Time/DateTime.h>
22 #include <OnixS/Core/Time/Operators.h>
23 #include <OnixS/Core/Time/Service.h>
24 
25 #include <OnixS/HandlerCore/TimeHelper.h>
26 
28 
29 #include "NamespaceHelper.h"
30 
31 using namespace std;
32 using namespace OnixS::Util;
33 using OnixS::HandlerCore::Common::TimeHelper;
34 
35 ONIXS_HANDLER_NAMESPACE_BEGIN
36 
37 namespace
38 {
39  inline
40  OnixS::Time::DateTime
41  convert(const Timestamp& time)
42  {
43  return
44  OnixS::Time::DateTime(
45  OnixS::Time::TimeSpan(time.sinceEpoch().totalSeconds(),
46  OnixS::Time::NanosecondSubseconds(time.sinceEpoch().nanoseconds())));
47  }
48 }
49 
50 
51 Timestamp::Timestamp()
52 {
53  *this = TimeHelper::convert<Timestamp>(
54  OnixS::Time::DateTime::epoch());
55 }
56 
57 Timestamp::Timestamp(
58  unsigned int year, Month::Enum month, unsigned int day,
59  unsigned int hour, unsigned int minute, unsigned int second,
60  unsigned int nanosecond) :
61  sinceEpoch_()
62 {
63  *this = TimeHelper::convert<Timestamp>(
64  OnixS::Time::DateTime(
65  year,
66  static_cast<OnixS::Time::Month>(month),
67  day,
68  hour,
69  minute,
70  second,
71  OnixS::Time::NanosecondSubsecond(nanosecond)));
72 }
73 
74 unsigned
76 {
77  return convert(*this).year();
78 }
79 
82 {
83  return static_cast<Month::Enum>(
84  convert(*this).month());
85 }
86 
87 unsigned int
89 {
90  return convert(*this).day();
91 }
92 
93 void
95 {
96  OnixS::Time::YearMonthDay impl;
97  convert(*this).getDate(impl);
98 
99  ymd.year = impl.year;
100  ymd.month = static_cast<Month::Enum>(impl.month);
101  ymd.day = impl.day;
102 }
103 
104 unsigned int
106 {
107  return convert(*this).hour();
108 }
109 
110 unsigned int
112 {
113  return convert(*this).minute();
114 }
115 
116 unsigned int
118 {
119  return convert(*this).second();
120 }
121 
122 bool
124 const Timestamp& other) const
125 {
126  return convert(*this) ==
127  convert(other);
128 }
129 
130 bool
132 const Timestamp& other) const
133 {
134  return convert(*this) !=
135  convert(other);
136 }
137 
138 bool
140 const Timestamp& other) const
141 {
142  return convert(*this) <
143  convert(other);
144 }
145 
146 void
148 std::string& str,
149 TimestampFormat::Enum format) const
150 {
151  const
152  OnixS::Time::DateTime
153  dateTime = convert(*this);
154 
155  switch (format)
156  {
158  OnixS::Time::YYYYMMDDFormatter()(dateTime, str);
159  break;
160 
162  OnixS::Time::YYYYMMDDHHMMSSFormatter()(dateTime, str);
163  break;
164 
166  OnixS::Time::YYYYMMDDHHMMSSmsecFormatter()(dateTime, str);
167  break;
168 
170  OnixS::Time::YYYYMMDDHHMMSSnsecFormatter()(dateTime, str);
171  break;
172 }
173 }
174 
175 Timestamp
177 {
178  return TimeHelper::convert<Timestamp>(
179  OnixS::Time::Accurate::utcNow());
180 }
181 
182 Timestamp
184 {
185  return TimeHelper::convert<Timestamp>(
186  OnixS::Time::Accurate::now());
187 }
188 
189 Timestamp
191  const std::string &str,
192  TimestampFormat::Enum format)
193  {
194  bool succeeded = false;
195  OnixS::Time::DateTime dateTime;
196 
197  switch (format)
198  {
200  {
201  succeeded =
202  OnixS::Time::YYYYMMDDFormatter()(
203  str.data(), str.size(), dateTime);
204  }
205  break;
206 
208  {
209  succeeded =
210  OnixS::Time::YYYYMMDDHHMMSSFormatter()(
211  str.data(), str.size(), dateTime);
212  }
213  break;
214 
216  {
217  succeeded =
218  OnixS::Time::YYYYMMDDHHMMSSmsecFormatter()(
219  str.data(), str.size(), dateTime);
220  }
221  break;
222 
224  {
225  succeeded =
226  OnixS::Time::YYYYMMDDHHMMSSnsecFormatter()(
227  str.data(), str.size(), dateTime);
228  }
229  break;
230  }
231 
232  if (succeeded)
233  return TimeHelper::convert<Timestamp>(dateTime);
234 
235  std::string exceptionReason;
236 
237  exceptionReason += "Cannot parse timestamp [str=";
238  exceptionReason += str;
239  exceptionReason += "]. ";
240 
241  throw OnixS::DomainException(exceptionReason);
242 }
243 
244 Timestamp
246  unsigned long long numericPresentation,
247  TimestampFormat::Enum format)
248  {
249  switch (format)
250  {
252  {
253  const unsigned int day = static_cast<unsigned int>((numericPresentation) % 100);
254 
255  const Month::Enum month = static_cast<Month::Enum>((numericPresentation /= 100) % 100);
256 
257  const unsigned int year = static_cast<unsigned int>(numericPresentation / 100);
258 
259  return Timestamp(year, month, day);
260  }
261 
265  default:
266  break;
267  }
268 
269  std::string exceptionReason;
270 
271  exceptionReason += "Cannot parse timestamp [str=";
272  exceptionReason += i2str(numericPresentation);
273  exceptionReason += "]. ";
274 
275  throw OnixS::DomainException(exceptionReason);
276 }
277 
278 Timestamp
280 {
281  return TimeHelper::convert<Timestamp>(OnixS::Time::DateTime::epoch());
282 }
283 
284 
285 
286 ONIXS_HANDLER_NAMESPACE_END
287 
Represents timestamp without time-zone information.
Definition: Timestamp.h:84
STL namespace.
void date(YearMonthDay &) const
Returns date part of timestamp.
Definition: Timestamp.cpp:94
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Definition: Timestamp.h:236
Enum
Defines all the months in the year.
Definition: Timestamp.h:33
bool operator<(const Timestamp &) const
Tests whether instance is less than another one.
Definition: Timestamp.cpp:139
Month::Enum month() const
Month component of timestamp.
Definition: Timestamp.cpp:81
bool operator!=(const Timestamp &) const
Compares instance with another one.
Definition: Timestamp.cpp:131
static Timestamp parse(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec)
Definition: Timestamp.cpp:190
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
Definition: Timestamp.h:79
bool operator==(const Timestamp &) const
Compares instance with another one.
Definition: Timestamp.cpp:123
Indicates timestamp in "YYYYMMDD" format.
Definition: Timestamp.h:70
Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:73
Timestamp()
Initializes as Jan 1, 0001, 00:00:00.
Definition: Timestamp.cpp:51
unsigned year() const
Year component of timestamp.
Definition: Timestamp.cpp:75
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:76