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