OnixS Eurex ETI Handler C++ library  9.21.0
API documentation
Time.h
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
13  * part of this source code or associated reference material to any other location for further
14  * reproduction or redistribution, and any amendments to this copyright notice, are expressly
15  * prohibited.
16  *
17  * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
18  * the terms of the Agreement is a violation of copyright law.
19  */
20 
21 #pragma once
22 
23 #if defined(__linux__)
24 # include <sys/time.h>
25 # include <unistd.h>
26 #endif
27 
29 #include "OnixS/Eurex/Trading/Export.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 namespace OnixS { namespace Eurex { namespace Trading {
35 
36 #if defined(_WIN32)
37 
38 typedef unsigned __int64 AbsoluteHighResolutionTimeImpl;
39 
40 typedef __int64 RelativeHighResolutionTimeImpl;
41 
42 #elif defined(__linux__)
43 
44 typedef timeval AbsoluteHighResolutionTimeImpl;
45 
46 typedef struct Nothing
47 {
48 } RelativeHighResolutionTimeImpl;
49 #else
50 
51 # error High resolution time services are not defined for a given platform.
52 
53 #endif
54 /// Alias for raw timestamps.
55 typedef AbsoluteHighResolutionTimeImpl Timestamp;
56 
57 /// Fields of HighResolutionTime.
58 struct ONIXS_EUREX_ETI_EXPORT HighResolutionTimeFields
59 {
60  unsigned short year;
61  unsigned short month;
62  unsigned short day;
63 
64  unsigned short hour;
65  unsigned short minute;
66  unsigned short seconds;
67 
68  unsigned int microseconds;
69 
70  /// Default constructor.
72 
73  /// From absolute.
74  void fromAbsolute(const AbsoluteHighResolutionTimeImpl&);
75 
76  /// To absolute.
77  void toAbsolute(AbsoluteHighResolutionTimeImpl*) const;
78 
79  /// Parse from string.
80  void fromString(const std::string&);
81 
82  /// Returns string representation.
83  std::string toString() const;
84 };
85 
86 typedef long long HighResolutionTimeSpan;
87 
88 /// Miscellaneous time traits.
89 class ONIXS_EUREX_ETI_EXPORT HighResolutionTime
90 {
91 public:
92  /// Initializes as not valid.
94 
95  /// Initializes from given number of milliseconds since Jan 1st, 1970, 00:00:00 GMT.
97 
98  /// Initializes from the given set of time attributes.
100 
101  /// Initializes from the other instance.
103 
104  /// Indicates whether the instance is valid time.
105  bool isValid() const;
106 
107  /// Extracts details like year, month, seconds, etc.
108  void getFields(HighResolutionTimeFields* fields) const;
109 
110  /// Returns canonical presentation of time.
111  std::string toString() const;
112 
113  /// Updates to current time.
114  void setToNow();
115 
116  /// Compares two timestamps for equality.
117  bool operator==(const HighResolutionTime&) const;
118 
119  /// Compares two timestamps for inequality.
120  bool operator!=(const HighResolutionTime&) const;
121 
122  /// Returns difference between two times in microseconds.
123  HighResolutionTimeSpan operator-(const HighResolutionTime&) const;
124 
125  /// Reinitializes from the other instance.
126  HighResolutionTime& operator=(const HighResolutionTime&);
127 
128  /// Returns current time.
129  static HighResolutionTime now();
130 
131  /// Returns time from its string presentation.
132  static HighResolutionTime parse(const std::string& time);
133 
134 protected:
135  friend struct TimestampHelpers;
136 #if defined(_WIN32)
137  /// Returns time created from relative value.
138  static HighResolutionTime createFromRelative(RelativeHighResolutionTimeImpl value);
139 #elif defined(__linux__)
140  /// Returns time created from absolute value.
141  static HighResolutionTime createFromAbsolute(AbsoluteHighResolutionTimeImpl value);
142 #endif
143 
144 private:
145  enum ValueKind
146  {
147  Invalid,
148  Absolute,
149  Relative
150  };
151 
152  union Value
153  {
154  AbsoluteHighResolutionTimeImpl asAbsolute;
155  RelativeHighResolutionTimeImpl asRelative;
156  };
157 
158  Value value_;
159  ValueKind kind_;
160 
161  HighResolutionTime(ValueKind kind);
162 };
163 
164 inline bool HighResolutionTime::isValid() const
165 {
166  return !(Invalid == kind_);
167 }
168 
169 ONIXS_EUREX_ETI_EXPORT std::ostream& operator<<(std::ostream&, const HighResolutionTime&);
170 
171 }}} // namespace OnixS::Eurex::Trading
unsigned long long UInt64
Definition: Defines.h:46
Fields of HighResolutionTime.
Definition: Time.h:58
long long HighResolutionTimeSpan
Definition: Time.h:86
bool isValid() const
Indicates whether the instance is valid time.
Definition: Time.h:164
std::ostream & operator<<(std::ostream &, ConnectionState::Enum)
Make it printable using C++ I/O streams.
Miscellaneous time traits.
Definition: Time.h:89
AbsoluteHighResolutionTimeImpl Timestamp
Alias for raw timestamps.
Definition: Time.h:55