OnixS C++ FIX Engine  4.2.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 /// Collection of timestamp formats supported.
69  enum Enum {
70  /// Indicates timestamp in "YYYYMMDD" format.
72 
73  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
75 
76  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
78 
79  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssss" format.
81 
82  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
84 
85  /// Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssssssssss" format.
86  YYYYMMDDHHMMSSPsec
87  };
88 };
89 
90 /// Timestamps related functionality.
92 {
93 public:
94  /// Constructs 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  /// Current year.
120  Year year() const;
121 
122  /// Current month.
123  Month::Enum month() const;
124 
125  /// Current day of month.
126  /// Valid values are 1 through 31.
127  Day day() const;
128 
129  /// Returns date part of timestamp.
130  void date(YearMonthDay &) const;
131 
132  /// Current hour.
133  /// Valid values are 0 through 23.
134  Hour hour() const;
135 
136  /// Current minute.
137  /// Valid values are 0 through 59.
138  Minute minute() const;
139 
140  /// Current second.
141  /// Valid values are 0 through 59.
142  Second second() const;
143 
144  /// Current millisecond.
145  /// Valid values are 0 through 999.
146  Millisecond millisecond() const;
147 
148  /// Current microsecond.
149  /// Valid values are 0 through 999999.
150  Microsecond microsecond() const;
151 
152  /// Current nanosecond.
153  /// Valid values are 0 through 999999999.
154  Nanosecond nanosecond() const;
155 
156  /// Current picosecond.
157  /// Valid values are 0 through 999999999999.
158  Picosecond picosecond() const;
159 
160  /// Total number of seconds since Jan 1, 0001, 00:00:00.
161  TotalSeconds totalSeconds() const;
162 
163  /// Compares instance with another one.
164  bool operator == (const Timestamp &) const;
165 
166  /// Compares instance with another one.
167  bool operator != (const Timestamp &) const;
168 
169  /// Tests whether instance is less than another one.
170  bool operator < (const Timestamp &) const;
171 
172  /// Increases instance by time span
173  Timestamp & operator += (const TimeSpan &);
174 
175  /// Decreases instance by time span
176  Timestamp & operator -= (const TimeSpan &);
177 
178  /// Returns timestamp text presentation in requested
179  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
180  std::string
181  toString(
184 
185  /// Appends timestamp text presentation in requested
186  /// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
187  void
188  toString(
189  std::string & str,
192 
193  /// Returns the current UTC time.
194  ///
195  /// @note Timestamp resolution depends on
196  /// capabilities of an operating system.
197  static Timestamp utc();
198 
199  /// Returns the current local time.
200  ///
201  /// @note Timestamp resolution depends on
202  /// capabilities of an operating system.
203  static Timestamp local();
204 
205  /// Parses timestamp from its text presentation assuming it's in
206  /// specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
207  static
208  Timestamp
209  parse(
210  const std::string &,
213 
214 private:
215  friend class TimeManager;
216 
217  TotalSeconds totalSeconds_;
218  Picosecond picoseconds_;
219 };
220 
221 inline
223  TotalSeconds totalSeconds, Nanosecond subseconds)
224  : totalSeconds_(totalSeconds),
225  picoseconds_(subseconds * static_cast<Picosecond>(TimeDetails::PicosecondsPerNanosecond))
226 {
227 }
228 
229 inline
231  TotalSeconds totalSeconds, Picosecond subseconds)
232  : totalSeconds_(totalSeconds),
233  picoseconds_(subseconds)
234 {
235 }
236 
237 inline
240 {
241  return totalSeconds_;
242 }
243 
244 inline
245 Millisecond
247 {
248  return static_cast<Millisecond>(picoseconds_ / TimeDetails::PicosecondsPerMillisecond);
249 }
250 
251 inline
252 Microsecond
254 {
255  return static_cast<Microsecond>(picoseconds_ / TimeDetails::PicosecondsPerMicrosecond);
256 }
257 
258 inline
259 Nanosecond
261 {
262  return static_cast<Nanosecond>(picoseconds_ / TimeDetails::PicosecondsPerNanosecond);
263 }
264 
265 inline
266 Picosecond
268 {
269  return picoseconds_;
270 }
271 
272 inline
273 std::string
275  TimestampFormat::Enum format) const
276 {
277  std::string str;
278 
279  toString(str, format);
280 
281  return str;
282 }
283 }
284 }
Defines all the months in the year.
Definition: Timestamp.h:42
HugeAmount Picosecond
Definition: Timestamp.h:39
Millisecond millisecond() const
Current millisecond.
Definition: Timestamp.h:246
Microsecond microsecond() const
Current microsecond.
Definition: Timestamp.h:253
static const Picoseconds PicosecondsPerNanosecond
Definition: TimeSpan.h:66
Time related constants.
Definition: TimeSpan.h:54
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSNsec) const
Returns timestamp text presentation in requested format ("YYYYMMDD-HH:MM:SS.sssssssss" by default)...
Definition: Timestamp.h:274
static const Picoseconds PicosecondsPerMicrosecond
Definition: TimeSpan.h:67
UInt32 Amount
Definition: TimeSpan.h:31
bool operator!=(const FieldValueRef &ref, const std::string &str)
Amount Day
Definition: Timestamp.h:30
HugeInterval TotalSeconds
Definition: TimeSpan.h:47
Amount Microsecond
Definition: Timestamp.h:37
Indicates timestamp in "YYYYMMDD" format.
Definition: Timestamp.h:71
Timestamps related functionality.
Definition: Timestamp.h:91
Amount Millisecond
Definition: Timestamp.h:36
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
Nanosecond nanosecond() const
Current nanosecond.
Definition: Timestamp.h:260
Indicates timestamp in "YYYYMMDD-HH:MM:SS" format.
Definition: Timestamp.h:74
TotalSeconds totalSeconds() const
Total number of seconds since Jan 1, 0001, 00:00:00.
Definition: Timestamp.h:239
Picosecond picosecond() const
Current picosecond.
Definition: Timestamp.h:267
Amount Nanosecond
Definition: Timestamp.h:38
Amount Hour
Definition: Timestamp.h:32
Timestamp()
Constructs uninitialized instance.
Amount Year
Definition: Timestamp.h:29
UInt64 HugeAmount
Definition: TimeSpan.h:32
Year, month, day fields.
Definition: Timestamp.h:61
static const Picoseconds PicosecondsPerMillisecond
Definition: TimeSpan.h:68
Collection of timestamp formats supported.
Definition: Timestamp.h:68
bool operator==(const FieldValueRef &ref, const std::string &str)
Amount Minute
Definition: Timestamp.h:33
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sssssssss" format.
Definition: Timestamp.h:83
Indicates timestamp in "YYYYMMDD-HH:MM:SS.sss" format.
Definition: Timestamp.h:77
Time span related functionality.
Definition: TimeSpan.h:93
Amount Second
Definition: Timestamp.h:34
Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssss" format.
Definition: Timestamp.h:80