OnixS C++ FIX Engine  4.10.1
API Documentation
Timestamp.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 #include <string>
23 
24 #include <OnixS/FIXEngine/ABI.h>
26 
27 namespace OnixS {
28 namespace FIX {
29 typedef Amount Year;
30 typedef Amount Day;
31 
32 typedef Amount Hour;
33 typedef Amount Minute;
34 typedef Amount Second;
35 
40 
41 /// Defines all the months in the year.
43  enum Enum {
49  May,
56  December
57  };
58 };
59 
60 /// Year, month, day fields.
62  Year year;
64  Day day;
65 };
66 
67 /// The collection of timestamp formats supported.
69  enum Enum {
70  /// Indicates the timestamp in the "YYYYMMDD" format.
72 
73  /// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS" format.
75 
76  /// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sss" format.
78 
79  /// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.ssssss" format.
81 
82  /// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sssssssss" format.
84 
85  /// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.ssssssssssss" format.
86  YYYYMMDDHHMMSSPsec
87  };
88 };
89 
90 /// The timestamps related functionality.
92 {
93 public:
94  /// Constructs an uninitialized instance.
95  Timestamp();
96 
97  /// Initializes from seconds and nanoseconds since Jan 1, 0001, 00:00:00.
98  Timestamp(
99  TotalSeconds seconds,
100  Nanosecond nanosecond);
101 
102  /// Initializes from seconds and picoseconds since Jan 1, 0001, 00:00:00.
103  Timestamp(
104  TotalSeconds seconds,
105  Picosecond picosecond);
106 
107  /// Initializes date-time from all details.
108  Timestamp(
109  Year year, Month::Enum month, Day day,
110  Hour hour = 0, Minute minute = 0, Second second = 0,
111  Nanosecond nanosecond = 0);
112 
113  /// Initializes date-time from all details.
114  Timestamp(
115  Year year, Month::Enum month, Day day,
116  Hour hour, Minute minute, Second second,
117  Picosecond picosecond);
118 
119  /// Initializes from nanoseconds since Jan 1, 1970, 00:00:00 (Unix epoch).
120  explicit Timestamp(TotalNanoseconds unixTimestampNanoseconds);
121 
122  /// The current year.
123  Year year() const;
124 
125  /// The current month.
126  Month::Enum month() const;
127 
128  /// The current day of month.
129  /// Valid values are 1 through 31.
130  Day day() const;
131 
132  /// Returns the date part of the timestamp.
133  void date(YearMonthDay &) const;
134 
135  /// The current hour.
136  /// Valid values are 0 through 23.
137  Hour hour() const;
138 
139  /// The current minute.
140  /// Valid values are 0 through 59.
141  Minute minute() const;
142 
143  /// The current second.
144  /// Valid values are 0 through 59.
145  Second second() const;
146 
147  /// The current millisecond.
148  /// Valid values are 0 through 999.
149  Millisecond millisecond() const;
150 
151  /// The current microsecond.
152  /// Valid values are 0 through 999999.
153  Microsecond microsecond() const;
154 
155  /// The current nanosecond.
156  /// Valid values are 0 through 999999999.
157  Nanosecond nanosecond() const;
158 
159  /// The current picosecond.
160  /// Valid values are 0 through 999999999999.
161  Picosecond picosecond() const;
162 
163  /// The total number of seconds since Jan 1, 0001, 00:00:00.
164  TotalSeconds totalSeconds() const;
165 
166  /// Compares the instance with another one.
167  bool operator == (const Timestamp &) const;
168 
169  /// Compares the instance with another one.
170  bool operator != (const Timestamp &) const;
171 
172  /// Tests whether the instance is less than another one.
173  bool operator < (const Timestamp &) const;
174 
175  /// Increases the instance by time span
176  Timestamp & operator += (const TimeSpan &);
177 
178  /// Decreases the instance by time span
179  Timestamp & operator -= (const TimeSpan &);
180 
181  /// Returns the timestamp text presentation in the requested
182  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
183  std::string
184  toString(
187 
188  /// Appends the timestamp text presentation in the requested
189  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
190  void
191  toString(
192  std::string & str,
195 
196  /// Returns the nanosecond timestamp since the Unix epoch.
197  TotalNanoseconds toUnixNanosecondTimestamp() const;
198 
199  /// Returns the current UTC time.
200  ///
201  /// @note The timestamp resolution depends on
202  /// capabilities of an operating system.
203  static Timestamp utc();
204 
205  /// Returns the current local time.
206  ///
207  /// @note The timestamp resolution depends on
208  /// capabilities of an operating system.
209  static Timestamp local();
210 
211  /// Parses the timestamp from its text presentation assuming it's in
212  /// the specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
213  static
214  Timestamp
215  parse(
216  const std::string &,
219 
220 private:
221  friend class TimeManager;
222 
223  TotalSeconds totalSeconds_;
224  Picosecond picoseconds_;
225 };
226 
227 inline
229  TotalSeconds totalSeconds, Nanosecond subseconds)
230  : totalSeconds_(totalSeconds),
231  picoseconds_(subseconds * static_cast<Picosecond>(TimeDetails::PicosecondsPerNanosecond))
232 {
233 }
234 
235 inline
237  TotalSeconds totalSeconds, Picosecond subseconds)
238  : totalSeconds_(totalSeconds),
239  picoseconds_(subseconds)
240 {
241 }
242 
243 inline
246 {
247  return totalSeconds_;
248 }
249 
250 inline
251 Millisecond
253 {
254  return static_cast<Millisecond>(picoseconds_ / TimeDetails::PicosecondsPerMillisecond);
255 }
256 
257 inline
258 Microsecond
260 {
261  return static_cast<Microsecond>(picoseconds_ / TimeDetails::PicosecondsPerMicrosecond);
262 }
263 
264 inline
265 Nanosecond
267 {
268  return static_cast<Nanosecond>(picoseconds_ / TimeDetails::PicosecondsPerNanosecond);
269 }
270 
271 inline
272 Picosecond
274 {
275  return picoseconds_;
276 }
277 
278 inline
279 std::string
281  TimestampFormat::Enum format) const
282 {
283  std::string str;
284 
285  toString(str, format);
286 
287  return str;
288 }
289 }
290 }
static const Picoseconds PicosecondsPerMicrosecond
Definition: TimeSpan.h:67
static const Picoseconds PicosecondsPerMillisecond
Definition: TimeSpan.h:68
Amount Year
Definition: Timestamp.h:29
Amount Minute
Definition: Timestamp.h:33
Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sssssssss" format.
Definition: Timestamp.h:83
The collection of timestamp formats supported.
Definition: Timestamp.h:68
HugeInterval TotalNanoseconds
Definition: TimeSpan.h:50
Amount Millisecond
Definition: Timestamp.h:36
Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:77
Millisecond millisecond() const
The current millisecond.
Definition: Timestamp.h:252
UInt64 HugeAmount
Definition: TimeSpan.h:32
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Picosecond picosecond() const
The current picosecond.
Definition: Timestamp.h:273
Amount Second
Definition: Timestamp.h:34
Microsecond microsecond() const
The current microsecond.
Definition: Timestamp.h:259
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Returns the timestamp text presentation in the requested format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
Definition: Timestamp.h:280
Time related constants.
Definition: TimeSpan.h:54
Amount Microsecond
Definition: Timestamp.h:37
UInt32 Amount
Definition: TimeSpan.h:31
Amount Hour
Definition: Timestamp.h:32
Amount Nanosecond
Definition: Timestamp.h:38
Indicates the timestamp in the "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:74
HugeAmount Picosecond
Definition: Timestamp.h:39
Timestamp()
Constructs an uninitialized instance.
Nanosecond nanosecond() const
The current nanosecond.
Definition: Timestamp.h:266
The time span related functionality.
Definition: TimeSpan.h:93
HugeInterval TotalSeconds
Definition: TimeSpan.h:47
bool operator==(const FieldValueRef &ref, const std::string &str)
Defines all the months in the year.
Definition: Timestamp.h:42
Indicates the timestamp in the "YYYYMMDD" format.
Definition: Timestamp.h:71
TotalSeconds totalSeconds() const
The total number of seconds since Jan 1, 0001, 00:00:00.
Definition: Timestamp.h:245
Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.ssssss" format.
Definition: Timestamp.h:80
The timestamps related functionality.
Definition: Timestamp.h:91
Year, month, day fields.
Definition: Timestamp.h:61
static const Picoseconds PicosecondsPerNanosecond
Definition: TimeSpan.h:66
bool operator!=(const FieldValueRef &ref, const std::string &str)
Amount Day
Definition: Timestamp.h:30