OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
API documentation
TimeSpan.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 
22 
23 #include <sstream>
24 #include <iomanip>
25 #include <stdexcept>
26 
27 #include <OnixS/Core/Time/DateTime.h>
28 #include <OnixS/Core/Time/Operators.h>
29 #include <OnixS/Core/Time/Formatters.h>
30 
31 #include "NamespaceHelper.h"
32 
33 using namespace std;
34 namespace Core = OnixS::Time;
35 
36 ONIXS_HANDLER_NAMESPACE_BEGIN
37 
38 namespace
39 {
40  inline
41  Core::TimeSpan
42  convert(const TimeSpan& timeSpan)
43  {
44  return Core::TimeSpan(
45  timeSpan.totalSeconds(),
46  Core::NanosecondSubseconds(timeSpan.nanoseconds()));
47  }
48 
49  inline
50  TimeSpan
51  convert(const Core::TimeSpan& timeSpan)
52  {
53  return TimeSpan(
54  timeSpan.totalSeconds(),
55  timeSpan.nanoseconds());
56  }
57 
58  void
59  throwBadTimeSpanFormat(
60  const std::string& timeSpan)
61  {
62  throw std::logic_error(std::string("Time format is invalid: ") + timeSpan);
63  }
64 }
65 
66 const TimeSpan TimeSpan::Zero(0, 0);
67 
68 TimeSpan::TimeSpan()
69 : seconds_(0), nanoseconds_(0)
70 {
71 }
72 
74  int hours, int minutes,
75  int seconds, int nanoseconds)
76 {
77  *this = convert(
78  Core::TimeSpan(
79  0,
80  hours, minutes,
81  seconds,
82  Core::NanosecondSubseconds(nanoseconds)));
83 }
84 
86  int days,
87  int hours, int minutes,
88  int seconds, int nanoseconds)
89 {
90  *this = convert(
91  Core::TimeSpan(
92  days,
93  hours, minutes,
94  seconds,
95  Core::NanosecondSubseconds(nanoseconds)));
96 }
97 
99  long long totalSeconds,
100  int nanoseconds)
101 {
102  const
103  Core::TimeSpan
104  Normalized(totalSeconds, Core::NanosecondSubseconds(nanoseconds));
105 
106  seconds_ = Normalized.totalSeconds();
107  nanoseconds_ = Normalized.nanoseconds();
108 }
109 
111  const TimeSpan& other)
112  : seconds_(other.seconds_),
113  nanoseconds_(other.nanoseconds_)
114 {
115 }
116 
117 int
119 {
120  return convert(*this).days();
121 }
122 
123 int
125 {
126  return convert(*this).hours();
127 }
128 
129 int
131 {
132  return convert(*this).minutes();
133 }
134 
135 int
137 {
138  return convert(*this).seconds();
139 }
140 
141 int
143 {
144  return convert(*this).milliseconds();
145 }
146 
147 int
149 {
150  return convert(*this).microseconds();
151 }
152 
153 bool
155  const TimeSpan& other) const
156 {
157  return convert(*this) == convert(other);
158 }
159 
160 bool
162  const TimeSpan& other) const
163 {
164  return convert(*this) != convert(other);
165 }
166 
167 bool
169  const TimeSpan& other) const
170 {
171  return convert(*this) < convert(other);
172 }
173 
174 bool
176  const TimeSpan& other) const
177 {
178  return convert(*this) > convert(other);
179 }
180 
181 TimeSpan&
183  const TimeSpan& other)
184 {
185  Core::TimeSpan result(convert(*this));
186 
187  result += convert(other);
188 
189  return (*this = convert(result));
190 }
191 
192 
193 TimeSpan&
195  const TimeSpan& other)
196 {
197  Core::TimeSpan result(convert(*this));
198 
199  result -= convert(other);
200 
201  return (*this = convert(result));
202 }
203 
204 TimeSpan&
206  const TimeSpan& other)
207 {
208  seconds_ = other.seconds_;
209  nanoseconds_ = other.nanoseconds_;
210 
211  return *this;
212 }
213 
214 void
216  std::string& str,
217  TimeSpanFormat format) const
218 {
219  switch (format)
220  {
222  {
223  Core::HHMMSSFormatter()(convert(*this), str);
224  }
225  break;
226 
228  {
229  Core::HHMMSSmsecFormatter()(convert(*this), str);
230  }
231  break;
232 
234  {
235  Core::SDHHMMSSnsecFormatter()(convert(*this), str);
236  }
237  break;
238 
239  default:
240  {
241  throw std::logic_error("Unknown TimeSpan serialization format specified.");
242  }
243  break;
244  }
245 }
246 
247 TimeSpan
248 TimeSpan::deserialize(const string& str)
249 {
250  Core::TimeSpan parsedSpan;
251 
252  if (!Core::TimeSpan::parse(
253  str.data(), str.size(), parsedSpan))
254  {
255  throwBadTimeSpanFormat(str);
256  }
257 
258  return convert(parsedSpan);
259 }
260 
261 ONIXS_HANDLER_NAMESPACE_END
bool operator>(const TimeSpan &other) const
Checks whether time interval greater than other one.
Definition: TimeSpan.cpp:175
static TimeSpan deserialize(const std::string &str)
De-serializes time interval from its text presentation.
Definition: TimeSpan.cpp:248
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition: TimeSpan.cpp:182
TimeSpan & operator=(const TimeSpan &other)
Re-assigns time interval from other one.
Definition: TimeSpan.cpp:205
STL namespace.
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition: TimeSpan.cpp:194
bool operator<(const TimeSpan &other) const
Checks whether time interval less than other one.
Definition: TimeSpan.cpp:168
long long totalSeconds() const
Whole number of seconds in time interval.
Definition: TimeSpan.h:152
bool operator==(const TimeSpan &other) const
Compares with other instance for equality.
Definition: TimeSpan.cpp:154
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
Definition: TimeSpan.cpp:215
bool operator!=(const TimeSpan &other) const
Compares with other instance for in-equality.
Definition: TimeSpan.cpp:161