OnixS C++ FIX Engine  4.13.0
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  /// Calculates the time interval between two time points.
182  TimeSpan operator - (const Timestamp &);
183 
184  /// Returns the timestamp text presentation in the requested
185  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
186  std::string
187  toString(
190 
191  /// Appends the timestamp text presentation in the requested
192  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
193  void
194  toString(
195  std::string & str,
198 
199  /// Returns the nanosecond timestamp since the Unix epoch.
200  TotalNanoseconds toUnixNanosecondTimestamp() const;
201 
202  /// Returns the current UTC time.
203  ///
204  /// @note The timestamp resolution depends on
205  /// capabilities of an operating system.
206  static Timestamp utc();
207 
208  /// Returns the current local time.
209  ///
210  /// @note The timestamp resolution depends on
211  /// capabilities of an operating system.
212  static Timestamp local();
213 
214  /// Parses the timestamp from its text presentation assuming it's in
215  /// the specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
216  static
217  Timestamp
218  parse(
219  const std::string &,
222 
223 private:
224  friend class TimeManager;
225 
226  TotalSeconds totalSeconds_;
227  Picosecond picoseconds_;
228 };
229 
230 inline
232  TotalSeconds totalSeconds, Nanosecond subseconds)
233  : totalSeconds_(totalSeconds),
234  picoseconds_(subseconds * static_cast<Picosecond>(TimeDetails::PicosecondsPerNanosecond))
235 {
236 }
237 
238 inline
240  TotalSeconds totalSeconds, Picosecond subseconds)
241  : totalSeconds_(totalSeconds),
242  picoseconds_(subseconds)
243 {
244 }
245 
246 inline
249 {
250  return totalSeconds_;
251 }
252 
253 inline
254 Millisecond
256 {
257  return static_cast<Millisecond>(picoseconds_ / TimeDetails::PicosecondsPerMillisecond);
258 }
259 
260 inline
261 Microsecond
263 {
264  return static_cast<Microsecond>(picoseconds_ / TimeDetails::PicosecondsPerMicrosecond);
265 }
266 
267 inline
268 Nanosecond
270 {
271  return static_cast<Nanosecond>(picoseconds_ / TimeDetails::PicosecondsPerNanosecond);
272 }
273 
274 inline
275 Picosecond
277 {
278  return picoseconds_;
279 }
280 
281 inline
282 std::string
284  TimestampFormat::Enum format) const
285 {
286  std::string str;
287 
288  toString(str, format);
289 
290  return str;
291 }
292 }
293 }
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:255
UInt64 HugeAmount
Definition: TimeSpan.h:32
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Picosecond picosecond() const
The current picosecond.
Definition: Timestamp.h:276
Amount Second
Definition: Timestamp.h:34
Microsecond microsecond() const
The current microsecond.
Definition: Timestamp.h:262
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:283
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:269
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:248
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