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