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