OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  7.4.3
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 
30 namespace OnixS {
31 namespace Eurex {
32 namespace MarketData {
33 
34 using namespace std;
35 using namespace OnixS::Util;
36 
37 
39 {
40  *this = TimeManager::convert(
41  OnixS::Time::DateTime::epoch());
42 }
43 
45  unsigned int year, Month::Enum month, unsigned int day,
46  unsigned int hour, unsigned int minute, unsigned int second,
47  unsigned int nanosecond) :
48  sinceEpoch_()
49 {
50  *this = TimeManager::convert(
51  OnixS::Time::DateTime(
52  year,
53  static_cast<OnixS::Time::Month>(month),
54  day,
55  hour,
56  minute,
57  second,
58  OnixS::Time::NanosecondSubsecond(nanosecond)
59  )
60  );
61 }
62 
63 unsigned
65 {
66  return TimeManager::convert(*this).year();
67 }
68 
71 {
72  return static_cast<Month::Enum>(
73  TimeManager::convert(*this).month());
74 }
75 
76 unsigned int
78 {
79  return TimeManager::convert(*this).day();
80 }
81 
82 void
84 {
85  OnixS::Time::YearMonthDay impl;
86  TimeManager::convert(*this).getDate(impl);
87 
88  ymd.year = impl.year;
89  ymd.month = static_cast<Month::Enum>(impl.month);
90  ymd.day = impl.day;
91 }
92 
93 unsigned int
95 {
96  return TimeManager::convert(*this).hour();
97 }
98 
99 unsigned int
101 {
102  return TimeManager::convert(*this).minute();
103 }
104 
105 unsigned int
107 {
108  return TimeManager::convert(*this).second();
109 }
110 
111 bool
113  const Timestamp& other) const
114 {
115  return TimeManager::convert(*this) ==
116  TimeManager::convert(other);
117 }
118 
119 bool
121  const Timestamp& other) const
122 {
123  return TimeManager::convert(*this) !=
124  TimeManager::convert(other);
125 }
126 
127 bool
129  const Timestamp& other) const
130 {
131  return TimeManager::convert(*this) <
132  TimeManager::convert(other);
133 }
134 
135 void
137  std::string& str,
138  TimestampFormat::Enum format) const
139 {
140  const
141  OnixS::Time::DateTime
142  dateTime = TimeManager::convert(*this);
143 
144  switch (format)
145  {
147  OnixS::Time::YYYYMMDDFormatter()(dateTime, str);
148  break;
149 
151  OnixS::Time::YYYYMMDDHHMMSSFormatter()(dateTime, str);
152  break;
153 
155  OnixS::Time::YYYYMMDDHHMMSSmsecFormatter()(dateTime, str);
156  break;
157 
159  OnixS::Time::YYYYMMDDHHMMSSnsecFormatter()(dateTime, str);
160  break;
161  }
162 }
163 
164 Timestamp
166 {
167  return TimeManager::convert(
168  OnixS::Time::Accurate::utcNow());
169 }
170 
171 Timestamp
173 {
174  return TimeManager::convert(
175  OnixS::Time::Accurate::now());
176 }
177 
178 Timestamp
180  const std::string &str,
181  TimestampFormat::Enum format)
182 {
183  bool succeeded = false;
184  OnixS::Time::DateTime dateTime;
185 
186  switch (format)
187  {
189  {
190  succeeded =
191  OnixS::Time::YYYYMMDDFormatter()(
192  str.data(), str.size(), dateTime);
193  }
194  break;
195 
197  {
198  succeeded =
199  OnixS::Time::YYYYMMDDHHMMSSFormatter()(
200  str.data(), str.size(), dateTime);
201  }
202  break;
203 
205  {
206  succeeded =
207  OnixS::Time::YYYYMMDDHHMMSSmsecFormatter()(
208  str.data(), str.size(), dateTime);
209  }
210  break;
211 
213  {
214  succeeded =
215  OnixS::Time::YYYYMMDDHHMMSSnsecFormatter()(
216  str.data(), str.size(), dateTime);
217  }
218  break;
219  }
220 
221  if (succeeded)
222  return TimeManager::convert(dateTime);
223 
224  std::string exceptionReason;
225 
226  exceptionReason += "Cannot parse timestamp [str=";
227  exceptionReason += str;
228  exceptionReason += "]. ";
229 
230  throw OnixS::DomainException(exceptionReason);
231 }
232 
233 Timestamp
235  unsigned long long numericPresentation,
236  TimestampFormat::Enum format)
237 {
238  switch (format)
239  {
241  {
242  const unsigned int day = static_cast<unsigned int>((numericPresentation) % 100);
243 
244  const Month::Enum month = static_cast<Month::Enum>((numericPresentation /= 100) % 100);
245 
246  const unsigned int year = static_cast<unsigned int>(numericPresentation / 100);
247 
248  return Timestamp(year, month, day);
249  }
250 
254  default:
255  break;
256  }
257 
258  std::string exceptionReason;
259 
260  exceptionReason += "Cannot parse timestamp [str=";
261  exceptionReason += i2str(numericPresentation);
262  exceptionReason += "]. ";
263 
264  throw OnixS::DomainException(exceptionReason);
265 }
266 
267 Timestamp
269 {
270  return TimeManager::convert(OnixS::Time::DateTime::epoch());
271 }
272 
273 
274 }}}
275 
static Timestamp parse(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec)
Definition: Timestamp.cpp:179
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:72
static Timestamp epoch()
Epoch.
Definition: Timestamp.cpp:268
unsigned int minute() const
Definition: Timestamp.cpp:100
unsigned int second() const
Definition: Timestamp.cpp:106
bool operator!=(const Timestamp &) const
Compares instance with another one.
Definition: Timestamp.cpp:120
void date(YearMonthDay &) const
Returns date part of timestamp.
Definition: Timestamp.cpp:83
STL namespace.
Enum
Defines all the months in the year.
Definition: Timestamp.h:34
Year, month, day fields.
Definition: Timestamp.h:53
Definition: Defines.h:30
unsigned year() const
Year component of timestamp.
Definition: Timestamp.cpp:64
Indicates timestamp in "YYYYMMDD" format.
Definition: Timestamp.h:66
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Definition: Timestamp.h:232
Represents timestamp without time-zone information.
Definition: Timestamp.h:80
Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:69
Month::Enum month() const
Month component of timestamp.
Definition: Timestamp.cpp:70
bool operator==(const Timestamp &) const
Compares instance with another one.
Definition: Timestamp.cpp:112
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
Definition: Timestamp.h:75
unsigned int hour() const
Definition: Timestamp.cpp:94
static Timestamp convert(const OnixS::Time::DateTime &time)
Definition: Utils.cpp:169
Timestamp()
Initializes as Jan 1, 0001, 00:00:00.
Definition: Timestamp.cpp:38
bool operator<(const Timestamp &) const
Tests whether instance is less than another one.
Definition: Timestamp.cpp:128