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