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