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