OnixS CME Drop Copy Handler C++ library  5.7.1
API documentation
Time.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
4 // and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable OnixS Software
7 // Services Agreement (the Agreement) and Customer end user license agreements granting
8 // a non-assignable, non-transferable and non-exclusive license to use the software
9 // for it's own data processing purposes under the terms defined in the Agreement.
10 //
11 // Except as otherwise granted within the terms of the Agreement, copying or reproduction of any
12 // part of this source code or associated reference material to any other location for further
13 // reproduction or redistribution, and any amendments to this copyright notice, are expressly
14 // 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 "OnixS/CME/DropCopy/Export.h"
24 
25 #include <string>
26 
27 namespace OnixS { namespace CME { namespace DropCopy {
28 
29 /// Time span formats supported.
30 struct ONIXS_CME_DROP_COPY_EXPORT TimeSpanFormats
31 {
32  enum Enum
33  {
34  /// HH:MM:SS.
36 
37  /// HH:MM:SS.sss.
39 
40  /// HH:MM:SS.sssssssss.
41  SDHHMMSSnsec
42  };
43 };
44 
45 /// Time span format.
47 
48 /// Subseconds formats supported.
49 struct ONIXS_CME_DROP_COPY_EXPORT Subseconds
50 {
51  enum Enum
52  {
53  /// Milliseconds
55 
56  /// Microseconds
58 
59  /// Nanoseconds
60  Nano
61  };
62 };
63 
64 /// Time span format.
66 
67 /// Represents time interval. Used primarily to
68 /// present time-only stamps and to measure time
69 /// intervals between two timestamps.
70 class ONIXS_CME_DROP_COPY_EXPORT TimeSpan
71 {
72 public:
73  /// Initializes zero span.
74  TimeSpan();
75 
76  /// Initializes with given set of values.
77  /// Input parameters are treated as quantities,
78  /// but not as a time stamp. Therefore, there's
79  /// no requirement to fit in a certain range like
80  /// hours must fit into [0, 24) range. After
81  /// initialization time span will be normalized.
82  TimeSpan(Int32 hours, Int32 minutes, Int32 seconds, Int32 nanoseconds = 0);
83 
84  /// Initializes with given set of values.
85  /// Input parameters are treated as quantities,
86  /// but not as a time stamp. Therefore, there's
87  /// no requirement to fit in a certain range like
88  /// hours must fit into [0, 24) range. After
89  /// initialization time span will be normalized.
90  TimeSpan(Int32 days, Int32 hours, Int32 minutes, Int32 seconds, Int32 nanoseconds);
91 
92  /// Initializes time interval from total number
93  /// seconds and its fractional (nanosecond) part.
94  TimeSpan(Int64 totalSeconds, Int32 nanoseconds);
95 
96  /// Initializes as clone of other instance.
97  TimeSpan(const TimeSpan& other);
98 
99  /// Whole number of seconds in time interval.
100  Int64 totalSeconds() const;
101 
102  /// Whole number of subseconds in time interval.
103  Int64 totalSubseconds(Subsecond subsecondType) const;
104 
105  /// Days component of time interval.
106  /// Whole number of days in time interval.
107  Int32 days() const;
108 
109  /// Hours component of time interval.
110  /// Values are in range from -23 through 23.
111  Int32 hours() const;
112 
113  /// Minutes component of time interval.
114  /// Values are in range from -59 through 59.
115  Int32 minutes() const;
116 
117  /// Seconds component of time interval.
118  /// Values are in range from -59 through 59.
119  Int32 seconds() const;
120 
121  /// Milliseconds component of time interval.
122  /// Values are in range from -999 through 999.
123  Int32 milliseconds() const;
124 
125  /// Microseconds component of time interval.
126  /// Values are in range from -999999 through 999999.
127  Int32 microseconds() const;
128 
129  /// Nanoseconds component of time interval.
130  /// Values are in range from -999999999 through 999999999.
131  Int32 nanoseconds() const;
132 
133  /// Compares with other instance for equality.
134  bool operator==(const TimeSpan& other) const;
135 
136  /// Compares with other instance for in-equality.
137  bool operator!=(const TimeSpan& other) const;
138 
139  /// Checks whether time interval less than other one.
140  bool operator<(const TimeSpan& other) const;
141 
142  /// Checks whether time interval greater than other one.
143  bool operator>(const TimeSpan& other) const;
144 
145  /// Adds time interval to current one.
146  TimeSpan& operator+=(const TimeSpan& other);
147 
148  /// Subtracts time interval from current one.
149  TimeSpan& operator-=(const TimeSpan& other);
150 
151  /// Re-assigns time interval from other one.
152  TimeSpan& operator=(const TimeSpan& other);
153 
154  /// Serializes time stamp into text presentation
155  /// using specified time-span presentation format.
156  void toString(std::string& str, TimeSpanFormat format = TimeSpanFormats::SDHHMMSSnsec) const;
157 
158  /// Serializes time stamp into text presentation
159  /// using specified time-span presentation format.
160  std::string toString(TimeSpanFormat format = TimeSpanFormats::SDHHMMSSnsec) const;
161 
162  /// De-serializes time interval from its text presentation.
163  static TimeSpan deserialize(const std::string& str);
164 
165  /// Time interval of zero length.
166  static const TimeSpan Zero;
167 
168 private:
169  Int64 seconds_;
170  Int32 nanoseconds_;
171 };
172 
174 {
175  return seconds_;
176 }
177 
179 {
180  return nanoseconds_;
181 }
182 
183 inline std::string TimeSpan::toString(TimeSpanFormat format) const
184 {
185  std::string str;
186  toString(str, format);
187  return str;
188 }
189 
190 /// Identifies months in year.
191 struct ONIXS_CME_DROP_COPY_EXPORT Months
192 {
193  enum Enum
194  {
196 
209 
210  Count = December
211  };
212 
213  /// Deserializes value from text presentation.
214  static Enum deserialize(const char*);
215 
216  /// Returns text presentation for given value.
217  static const char* toString(Enum);
218 };
219 
220 /// Identifies months in year.
222 
223 /// Identifies day within week.
224 struct ONIXS_CME_DROP_COPY_EXPORT DaysOfWeek
225 {
226  enum Enum
227  {
235 
236  Total
237  };
238 
239  /// Deserializes value from text presentation.
240  static Enum deserialize(const char*);
241 
242  /// Returns text presentation for given value.
243  static const char* toString(Enum);
244 };
245 
246 /// Identifies days in week.
248 
249 /// Identifies week number within month.
250 struct ONIXS_CME_DROP_COPY_EXPORT WeekCodes
251 {
252  enum Enum
253  {
260 
261  LastWeek = Week5
262  };
263 
264  /// Deserializes value from text presentation.
265  static Enum deserialize(const char*);
266 
267  /// Returns text presentation for given value.
268  static const char* toString(Enum);
269 };
270 
271 /// Identifies week number within month.
273 
274 /// Represents month-year pair.
275 /// Year must fit into [0001, 9999] range.
276 /// Month must fit into [01, 12] range.
277 class ONIXS_CME_DROP_COPY_EXPORT YearMonth
278 {
279 public:
280  /// Initializes instance as Jan, 0001.
281  YearMonth();
282 
283  /// Initializes instance with given values according
284  /// YYYYMM (year/month) format.
285  /// Input parameters are checked for validness.
286  /// \throw exception if input parameters do
287  /// not fit into a valid range.
288  YearMonth(UInt32 year, Month month);
289 
290  /// Initializes instance with given values according
291  /// YYYYMMDD (year/month/day) format.
292  /// Input parameters are checked for validness.
293  /// \throw exception if input parameters do
294  /// not fit into a valid range.
295  YearMonth(UInt32 year, Month month, UInt32 day);
296 
297  /// Initializes instance with given values according
298  /// YYYYMMWW (year/month/week) format.
299  /// Input parameters are checked for validness.
300  /// \throw exception if input parameters do
301  /// not fit into a valid range.
302  YearMonth(UInt32 year, Month month, WeekCode week);
303 
304  /// Initializes as copy of other instance.
305  YearMonth(const YearMonth& other);
306 
307  /// Year component.
308  /// Valid range of values is [0001, 9999].
309  UInt32 year() const
310  {
311  return year_;
312  }
313 
314  /// Month component.
315  /// Valid range of values is [01, 12].
316  Month month() const
317  {
318  return month_;
319  }
320 
321  /// Day component.
322  /// Valid range of values is [01, 31].
323  UInt32 day() const
324  {
325  return day_;
326  }
327 
328  /// Week component.
329  /// Valid range of values is [1, 5].
330  WeekCode week() const
331  {
332  return week_;
333  }
334 
335  /// Checks that instance has valid day component.
336  /// Note: it's not valid to have both valid day and week, only one of them allowed.
337  bool hasDay() const
338  {
339  return day_ != 0;
340  }
341 
342  /// Checks that instance has valid week component.
343  /// Note: it's not valid to have both valid day and week, only one of them allowed.
344  bool hasWeek() const
345  {
346  return week_ != WeekCodes::Invalid;
347  }
348 
349  /// Compares with other instance for equality.
350  bool operator==(const YearMonth&) const;
351 
352  /// Compares with other instance for inequality.
353  bool operator!=(const YearMonth&) const;
354 
355  /// Checks whether this instance is less than other one.
356  /// Note: when comparing objects with different date components,
357  /// e.g. days vs weeks (but same year and month) following order is applied:
358  /// YYYYMM < YYYYMMWW < YYYYMMDD
359  bool operator<(const YearMonth& other) const;
360 
361  /// Checks whether this instance is greater than other one.
362  /// Note: when comparing objects with different date components,
363  /// e.g. days vs weeks (but same year and month) following order is applied:
364  /// YYYYMMDD > YYYYMMWW > YYYYMM
365  bool operator>(const YearMonth& other) const;
366 
367  // Re-initializes instance as copy of other one.
368  YearMonth& operator=(const YearMonth& other);
369 
370  /// Serializes into text (YYYYMM/YYYYMMDD/YYYYMMWW) presentation.
371  std::string toString() const;
372 
373  /// Serializes into text (YYYYMM/YYYYMMDD/YYYYMMWW) presentation.
374  void toString(std::string&) const;
375 
376  /// De-serializes instance from its string
377  /// presentation (YYYYMM/YYYYMMDD/YYYYMMWW) as it's used by CME Globex.
378  static YearMonth deserialize(const StringRef&);
379 
380 private:
381  // Year component.
382  UInt32 year_;
383 
384  // Month component.
385  Month month_;
386 
387  // Day component.
388  UInt32 day_;
389 
390  // Week component.
391  WeekCode week_;
392 };
393 
394 inline std::string YearMonth::toString() const
395 {
396  std::string str;
397  toString(str);
398  return str;
399 }
400 
401 /// Collection of timestamp formats supported.
402 struct ONIXS_CME_DROP_COPY_EXPORT TimestampFormats
403 {
404  enum Enum
405  {
406  /// YYYYMMDD.
408 
409  /// YYYYMMDD-HH:MM:SS.
411 
412  /// YYYYMMDD-HH:MM:SS.sss.
414 
415  /// YYYYMMDD-HH:MM:SS.sssssssss.
416  YYYYMMDDHHMMSSnsec
417  };
418 };
419 
420 /// Timestamp format.
422 
423 /// Since epoch time units supported.
424 struct ONIXS_CME_DROP_COPY_EXPORT SinceEpochUnits
425 {
426  enum Enum
427  {
429  Days
430  };
431 };
432 
433 /// Since epoch units format.
435 
436 /// Represents timestamp without time-zone information.
437 class ONIXS_CME_DROP_COPY_EXPORT Timestamp
438 {
439 public:
440  /// Initializes as Jan 1, 1970, 00:00:00.
441  Timestamp();
442 
443  /// Initializes as date with zero time component.
444  ///
445  /// Input parameters are validated, therefore
446  /// constructor throws exception if input values
447  /// do not fit into their valid ranges.
448  Timestamp(UInt32 year, Month month, UInt32 day);
449 
450  /// Explicit timestamp initialization.
451  ///
452  /// Input parameters are validated, therefore
453  /// constructor throws exception if input values
454  /// do not fit into their valid ranges.
455  Timestamp(
456  UInt32 year,
457  Month month,
458  UInt32 day,
459  UInt32 hour,
460  UInt32 minute,
461  UInt32 second,
462  UInt32 nanosecond
463  );
464 
465  /// Initializes as copy of other instance.
466  Timestamp(const Timestamp& other);
467 
468  /// Year component of timestamp.
469  UInt32 year() const;
470 
471  /// Month component of timestamp.
472  Month month() const;
473 
474  /// Day component of timestamp.
475  UInt32 day() const;
476 
477  /// Hour component of timestamp.
478  UInt32 hour() const;
479 
480  /// Minute component of timestamp.
481  UInt32 minute() const;
482 
483  /// Second component of timestamp.
484  UInt32 second() const;
485 
486  /// Millisecond component of timestamp.
487  UInt32 millisecond() const;
488 
489  /// Microsecond component of timestamp.
490  UInt32 microsecond() const;
491 
492  /// Nanosecond component of timestamp.
493  UInt32 nanosecond() const;
494 
495  /// Returns timestamp without time part.
496  Timestamp date() const;
497 
498  /// Return time part of timestamp.
499  TimeSpan time() const;
500 
501  /// Returns day of the week.
502  DayOfWeek dayOfWeek() const;
503 
504  /// Compares with other instance for equality.
505  bool operator==(const Timestamp& other) const;
506 
507  /// Compares with other instance for inequality.
508  bool operator!=(const Timestamp& other) const;
509 
510  /// Checks whether timestamp is less than other one.
511  bool operator<(const Timestamp& other) const;
512 
513  /// Checks whether timestamp is greater than other one.
514  bool operator>(const Timestamp& other) const;
515 
516  /// Adds time interval to given timestamp.
517  Timestamp& operator+=(const TimeSpan& span);
518 
519  /// Subtracts time interval from given timestamp.
520  Timestamp& operator-=(const TimeSpan& span);
521 
522  /// Re-initializes as copy of other timestamp.
523  Timestamp& operator=(const Timestamp& other);
524 
525  /// Return timestamp that is current date
526  /// and time expressed as local time.
527  static Timestamp now();
528 
529  /// Return timestamp that is current date
530  /// and time expressed as local time.
531  static void now(Timestamp&);
532 
533  /// Return timestamp that is current date
534  /// and time expressed as UTC time.
535  static Timestamp utcNow();
536 
537  /// Return timestamp that is current date
538  /// and time expressed as UTC time.
539  static void utcNow(Timestamp&);
540 
541  /// Returns text presentation of timestamp
542  /// using specified presentation format.
543  std::string toString(TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
544 
545  /// Returns text presentation of timestamp
546  /// using specified presentation format.
547  void
548  toString(std::string& str, TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
549 
550  /// De-serializes timestamp from text presentation.
551  static Timestamp deserialize(const StringRef&);
552 
553  /// Converts timestamp from its numeric presentation
554  /// (Unix timestamp in nanoseconds or days since 01-01-1970 UTC)
555  /// as it's used by the CME Globex.
556  static Timestamp fromUnixTimestamp(UInt64 sinceEpoch, SinceEpochUnit unit);
557 
558  /// Converts timestamp into its numeric presentation
559  /// (Unix timestamp in nanoseconds or days since 01-01-1970 UTC)
560  /// as it's used by the CME Globex.
561  UInt64 toUnixTimestamp(SinceEpochUnit unit) const;
562 
563  /// Returns the maximal possible timestamp (year 2554)
564  static Timestamp maxTimestamp();
565 
566 private:
567  friend ONIXS_CME_DROP_COPY_EXPORT TimeSpan
568  operator-(const Timestamp& left, const Timestamp& right);
569 
570  /// Initializes from time interval since the Unix Epoch
571  /// (in nanoseconds since 01-01-1970 UTC).
572  Timestamp(UInt64);
573 
574  /// Time interval since the Unix Epoch
575  /// (nanoseconds since 01-01-1970 UTC).
576  UInt64 sinceEpoch_;
577 };
578 
579 inline std::string Timestamp::toString(TimestampFormat format) const
580 {
581  std::string str;
582  toString(str, format);
583  return str;
584 }
585 
586 /// Calculates time interval between two timestamps.
587 ONIXS_CME_DROP_COPY_EXPORT TimeSpan operator-(const Timestamp& left, const Timestamp& right);
588 
589 }}}
std::string toString() const
Serializes into text (YYYYMM/YYYYMMDD/YYYYMMWW) presentation.
Definition: Time.h:394
Month month() const
Definition: Time.h:316
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:323
Int64 totalSeconds() const
Whole number of seconds in time interval.
Definition: Time.h:173
SinceEpochUnits::Enum SinceEpochUnit
Since epoch units format.
Definition: Time.h:434
Identifies day within week.
Definition: Time.h:224
Int32 nanoseconds() const
Definition: Time.h:178
static const TimeSpan Zero
Time interval of zero length.
Definition: Time.h:166
long long Int64
Definition: Numeric.h:35
bool operator==(const FieldValueRef &ref, const std::string &str)
Definition: Messaging.h:188
bool operator!=(const FieldValueRef &ref, const std::string &str)
Definition: Messaging.h:193
Since epoch time units supported.
Definition: Time.h:424
WeekCodes::Enum WeekCode
Identifies week number within month.
Definition: Time.h:272
unsigned long long UInt64
Definition: Numeric.h:36
Collection of timestamp formats supported.
Definition: Time.h:402
DaysOfWeek::Enum DayOfWeek
Identifies days in week.
Definition: Time.h:247
TimeSpanFormats::Enum TimeSpanFormat
Time span format.
Definition: Time.h:46
TimeSpan operator-(const Timestamp &left, const Timestamp &right)
Calculates time interval between two timestamps.
UInt32 day() const
Definition: Time.h:323
Subseconds::Enum Subsecond
Time span format.
Definition: Time.h:65
std::string toString(TimestampFormat format=TimestampFormats::YYYYMMDDHHMMSSnsec) const
Definition: Time.h:579
unsigned int UInt32
Definition: Numeric.h:33
Identifies months in year.
Definition: Time.h:191
UInt32 year() const
Definition: Time.h:309
Identifies week number within month.
Definition: Time.h:250
WeekCode week() const
Definition: Time.h:330
Time span formats supported.
Definition: Time.h:30
Represents timestamp without time-zone information.
Definition: Time.h:437
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
TimestampFormats::Enum TimestampFormat
Timestamp format.
Definition: Time.h:421
Subseconds formats supported.
Definition: Time.h:49
Months::Enum Month
Identifies months in year.
Definition: Time.h:221