fix8  version 1.4.0
Open Source C++ FIX Framework
tickval.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-------------------------------------------------------------------------------------------------
37 #ifndef FIX8_TICKVAL_HPP_
38 #define FIX8_TICKVAL_HPP_
39 
40 //-------------------------------------------------------------------------------------------------
41 #include <chrono>
42 #ifdef _MSC_VER
43 #include <limits.h>
44 #undef min
45 #undef max
46 #pragma warning(push)
47 #pragma warning(disable: 4307) // chrono(132): warning C4307: '*': integral constant overflow in vc140
48 #endif
49 
50 //-------------------------------------------------------------------------------------------------
51 namespace FIX8 {
52 
53 //-------------------------------------------------------------------------------------------------
57 class Tickval
58 {
59 public:
60 #if defined(__APPLE__) || defined(_MSC_VER)
61  using f8_clock = std::chrono::system_clock;
62 #else
63  using f8_clock = std::chrono::high_resolution_clock;
64 #endif
65  using f8_time_point = std::chrono::time_point<f8_clock>;
66  using ticks = decltype(f8_time_point::min().time_since_epoch().count());
67  using f8_duration = f8_time_point::duration;
68  static const ticks noticks = 0; // this should be a signed value
69  static const ticks& errorticks() // 2262-04-12 09:47:16.854775807
70  {
71  static const auto g_ticks(std::chrono::duration_cast<std::chrono::nanoseconds>
72  (f8_time_point::max().time_since_epoch()).count());
73  return g_ticks;
74  }
75  static const ticks thousand = 1000;
76  static const ticks million = thousand * thousand;
77  static const ticks billion = thousand * million;
78  static const ticks second = billion;
79  static const ticks minute = 60 * second;
80  static const ticks hour = 60 * minute;
81  static const ticks day = 24 * hour;
82  static const ticks week = 7 * day;
83 
84 private:
86 
87 public:
90  explicit Tickval(bool settonow=false)
91  : _value(settonow ? f8_clock::now() : f8_time_point(f8_duration::zero())) {}
92 
94  Tickval(const Tickval& from) : _value(from._value) {}
95 
98  explicit Tickval(ticks from) : _value(std::chrono::duration_cast<f8_duration>(std::chrono::nanoseconds(from))) {}
99 
102  explicit Tickval(f8_time_point from) : _value(from) {}
103 
107  Tickval(time_t secs, long nsecs)
108  : _value(f8_clock::from_time_t(secs) + std::chrono::duration_cast<f8_duration>(std::chrono::nanoseconds(nsecs))) {}
109 
111  Tickval& operator=(const Tickval& that)
112  {
113  if (this != &that)
114  _value = that._value;
115  return *this;
116  }
117 
122  {
123  _value = f8_time_point(std::chrono::duration_cast<f8_duration>(std::chrono::nanoseconds(that)));
124  return *this;
125  }
126 
129  ticks get_ticks() const { return std::chrono::duration_cast<std::chrono::nanoseconds>(_value.time_since_epoch()).count(); }
130 
134  {
135  _value = f8_clock::now();
136  return *this;
137  }
138 
141  time_t secs() const { return std::chrono::duration_cast<std::chrono::seconds>(_value.time_since_epoch()).count(); }
142 
145  unsigned msecs() const
146  {
147  return std::chrono::duration_cast<std::chrono::milliseconds>(_value.time_since_epoch()).count() % std::milli::den;
148  }
149 
152  unsigned usecs() const
153  {
154  return std::chrono::duration_cast<std::chrono::microseconds>(_value.time_since_epoch()).count() % std::micro::den;
155  }
156 
159  unsigned nsecs() const
160  {
161  return std::chrono::duration_cast<std::chrono::nanoseconds>(_value.time_since_epoch()).count() % std::nano::den;
162  }
163 
166  double todouble() const
167  {
168  return static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(_value.time_since_epoch()).count()) / std::nano::den;
169  }
170 
173  bool is_errorval() const { return get_ticks() == errorticks(); }
174 
179  bool in_range(const Tickval& a, const Tickval& b) const
180  {
181  return !b.is_errorval() ? a <= *this && *this <= b : a <= *this;
182  }
183 
187  Tickval& adjust(ticks by) { return *this += by; }
188 
191  static Tickval get_tickval() { return Tickval(true); }
192 
195  static Tickval& get_tickval(Tickval& to) { return to = Tickval(true); }
196 
200  struct tm *as_tm(struct tm& result) const
201  {
202  const time_t insecs(std::chrono::system_clock::to_time_t(_value));
203 #ifdef _MSC_VER
204  gmtime_s(&result, &insecs);
205 #else
206  gmtime_r(&insecs, &result);
207 #endif
208  return &result;
209  }
210 
213  struct tm get_tm() const
214  {
215  struct tm result;
216  return *as_tm(result);
217  }
218 
222  void set(time_t secs, long nsecs) { _value = Tickval(secs, nsecs)._value; }
223 
226  bool operator!() const { return _value.time_since_epoch() == std::chrono::nanoseconds::zero(); }
227 
230  operator void*() { return _value.time_since_epoch() == std::chrono::nanoseconds::zero() ? nullptr : this; }
231 
234  operator Tickval::ticks () { return _value.time_since_epoch().count(); }
235 
238  operator double() { return todouble(); }
239 
244  friend Tickval operator-(const Tickval& newtime, const Tickval& oldtime)
245  { return Tickval(newtime._value - oldtime._value.time_since_epoch()); }
246 
251  friend Tickval& operator-=(Tickval& oldtime, const Tickval& newtime)
252  {
253  oldtime._value -= newtime._value.time_since_epoch();
254  return oldtime;
255  }
256 
261  friend Tickval operator+(const Tickval& newtime, const Tickval& oldtime)
262  { return Tickval(newtime._value + oldtime._value.time_since_epoch()); }
263 
268  friend Tickval& operator+=(Tickval& oldtime, const Tickval& newtime)
269  {
270  oldtime._value += newtime._value.time_since_epoch();
271  return oldtime;
272  }
273 
278  friend bool operator==(const Tickval& a, const Tickval& b) { return a._value == b._value; }
279 
284  friend bool operator!=(const Tickval& a, const Tickval& b) { return a._value != b._value; }
285 
290  friend bool operator>(const Tickval& a, const Tickval& b) { return a._value > b._value; }
291 
296  friend bool operator<(const Tickval& a, const Tickval& b) { return a._value < b._value; }
297 
302  friend bool operator>=(const Tickval& a, const Tickval& b) { return a._value >= b._value; }
303 
308  friend bool operator<=(const Tickval& a, const Tickval& b) { return a._value <= b._value; }
309 
314  friend Tickval& operator+=(Tickval& oldtime, ticks ns)
315  {
316  oldtime._value += std::chrono::duration_cast<f8_duration>(std::chrono::nanoseconds(ns));
317  return oldtime;
318  }
319 
324  friend Tickval& operator-=(Tickval& oldtime, ticks ns)
325  {
326  oldtime._value -= std::chrono::duration_cast<f8_duration>(std::chrono::nanoseconds(ns));
327  return oldtime;
328  }
329 
334  friend std::ostream& operator<<(std::ostream& os, const Tickval& what)
335  {
336  std::string result;
337  return os << GetTimeAsStringMS(result, &what, 9, false);
338  }
339 
344  friend std::ostream& operator>>(std::ostream& os, const Tickval& what)
345  {
346  std::string result;
347  return os << GetTimeAsStringMS(result, &what, 9, true);
348  }
349 };
350 
351 } // FIX8
352 
353 #ifdef _MSC_VER
354 #pragma warning(pop)
355 #endif
356 
357 #endif // FIX8PRO_TICKVAL_HPP_
Tickval(time_t secs, long nsecs)
Definition: tickval.hpp:107
unsigned nsecs() const
Definition: tickval.hpp:159
Tickval(const Tickval &from)
Definition: tickval.hpp:94
static const ticks million
Definition: tickval.hpp:76
static Tickval get_tickval()
Definition: tickval.hpp:191
friend bool operator==(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:278
static const ticks & errorticks()
Definition: tickval.hpp:69
Tickval & operator=(ticks that)
Definition: tickval.hpp:121
friend Tickval operator-(const Tickval &newtime, const Tickval &oldtime)
Definition: tickval.hpp:244
void set(time_t secs, long nsecs)
Definition: tickval.hpp:222
static const ticks week
Definition: tickval.hpp:82
f8_time_point::duration f8_duration
Definition: tickval.hpp:67
friend Tickval operator+(const Tickval &newtime, const Tickval &oldtime)
Definition: tickval.hpp:261
std::chrono::time_point< f8_clock > f8_time_point
Definition: tickval.hpp:65
f8_time_point _value
Definition: tickval.hpp:85
static const ticks day
Definition: tickval.hpp:81
static const ticks second
Definition: tickval.hpp:78
friend bool operator>(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:290
friend std::ostream & operator>>(std::ostream &os, const Tickval &what)
Definition: tickval.hpp:344
unsigned usecs() const
Definition: tickval.hpp:152
std::chrono::high_resolution_clock f8_clock
Definition: tickval.hpp:63
Tickval(ticks from)
Definition: tickval.hpp:98
struct tm * as_tm(struct tm &result) const
Definition: tickval.hpp:200
struct tm get_tm() const
Definition: tickval.hpp:213
static const ticks billion
Definition: tickval.hpp:77
friend Tickval & operator-=(Tickval &oldtime, const Tickval &newtime)
Definition: tickval.hpp:251
static const ticks noticks
Definition: tickval.hpp:68
Tickval & now()
Definition: tickval.hpp:133
bool is_errorval() const
Definition: tickval.hpp:173
unsigned msecs() const
Definition: tickval.hpp:145
F8API const std::string & GetTimeAsStringMS(std::string &result, const class Tickval *tv=0, const unsigned dplaces=6, bool use_gm=false)
time_t secs() const
Definition: tickval.hpp:141
decltype(f8_time_point::min().time_since_epoch().count()) ticks
Definition: tickval.hpp:66
friend bool operator<(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:296
static Tickval & get_tickval(Tickval &to)
Definition: tickval.hpp:195
friend bool operator!=(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:284
friend Tickval & operator+=(Tickval &oldtime, const Tickval &newtime)
Definition: tickval.hpp:268
static const ticks minute
Definition: tickval.hpp:79
Tickval(f8_time_point from)
Definition: tickval.hpp:102
Tickval & operator=(const Tickval &that)
Definition: tickval.hpp:111
friend bool operator>=(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:302
bool in_range(const Tickval &a, const Tickval &b) const
Definition: tickval.hpp:179
friend std::ostream & operator<<(std::ostream &os, const Tickval &what)
Definition: tickval.hpp:334
friend Tickval & operator+=(Tickval &oldtime, ticks ns)
Definition: tickval.hpp:314
Tickval & adjust(ticks by)
Definition: tickval.hpp:187
static const ticks thousand
Definition: tickval.hpp:75
static const ticks hour
Definition: tickval.hpp:80
bool operator!() const
Definition: tickval.hpp:226
double todouble() const
Definition: tickval.hpp:166
ticks get_ticks() const
Definition: tickval.hpp:129
Tickval(bool settonow=false)
Definition: tickval.hpp:90
friend Tickval & operator-=(Tickval &oldtime, ticks ns)
Definition: tickval.hpp:324
friend bool operator<=(const Tickval &a, const Tickval &b)
Definition: tickval.hpp:308