OnixS C++ CBOE CFE Binary Order Entry (BOE) Handler 1.12.0
API documentation
Loading...
Searching...
No Matches
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
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20#pragma once
21
22#include <string>
23#include <algorithm>
24#include <stdexcept>
25
27#include <OnixS/CboeCFE/Trading/BOE/Compiler.h>
29
30
31namespace OnixS
32{
33 namespace CboeCFE
34 {
35 namespace Trading
36 {
37 namespace BOE
38 {
39
42 {
44 ONIXS_BATS_BOE_CONSTEXPR
45 static Int64 nanosecondsPerDay()
46 {
47 return 86400000000000ll;
48 }
49
51 ONIXS_BATS_BOE_CONSTEXPR
52 static Int64 nanosecondsPerHour()
53 {
54 return 3600000000000ll;
55 }
56
58 ONIXS_BATS_BOE_CONSTEXPR
59 static Int64 nanosecondsPerMinute()
60 {
61 return 60000000000ll;
62 }
63
65 ONIXS_BATS_BOE_CONSTEXPR
66 static Int32 nanosecondsPerSecond()
67 {
68 return 1000000000;
69 }
70
72 ONIXS_BATS_BOE_CONSTEXPR
74 {
75 return 1000000;
76 }
77
79 ONIXS_BATS_BOE_CONSTEXPR
81 {
82 return 1000;
83 }
84
86 ONIXS_BATS_BOE_CONSTEXPR
87 static Int32 hoursPerDay()
88 {
89 return 24;
90 }
91
93 ONIXS_BATS_BOE_CONSTEXPR
94 static Int32 minutesPerHour()
95 {
96 return 60;
97 }
98
100 ONIXS_BATS_BOE_CONSTEXPR
101 static Int32 secondsPerMinute()
102 {
103 return 60;
104 }
105
107 ONIXS_BATS_BOE_CONSTEXPR
109 {
110 return 1000;
111 }
112
114 ONIXS_BATS_BOE_CONSTEXPR
116 {
117 return 1000000;
118 }
119 };
120
124 {
125 public:
127 typedef Int64 Ticks;
128
130 typedef Int32 Days;
131
133 typedef Int32 Hours;
134
136 typedef Int32 Minutes;
137
139 typedef Int32 Seconds;
140
142 typedef Int32 Milliseconds;
143
145 typedef Int32 Microseconds;
146
148 typedef Int32 Nanoseconds;
149
151 explicit TimeSpan(Ticks ticks = 0) ONIXS_BATS_BOE_NOTHROW
152 : ticks_(ticks)
153 {
154 }
155
164 Days days,
165 Hours hours,
169 : ticks_(
170 static_cast<Ticks>(days) *
171 TimeTraits::nanosecondsPerDay() +
172 static_cast<Ticks>(hours) *
173 TimeTraits::nanosecondsPerHour() +
174 static_cast<Ticks>(minutes) *
175 TimeTraits::nanosecondsPerMinute() +
176 static_cast<Ticks>(seconds) *
177 TimeTraits::nanosecondsPerSecond() +
179 {
180 }
181
190 Hours hours,
194 : ticks_(
195 static_cast<Ticks>(hours) *
196 TimeTraits::nanosecondsPerHour() +
197 static_cast<Ticks>(minutes) *
198 TimeTraits::nanosecondsPerMinute() +
199 static_cast<Ticks>(seconds) *
200 TimeTraits::nanosecondsPerSecond() +
202 {
203 }
204
207 Days days() const
208 {
209 return
210 static_cast<Days>
211 (ticks_ /
213 }
214
217 Hours hours() const
218 {
219 return
220 static_cast<Hours>(
221 (ticks_ /
224 );
225 }
226
229 Int32 minutes() const
230 {
231 return
232 static_cast<Minutes>(
233 (ticks_ /
236 );
237 }
238
241 Int32 seconds() const
242 {
243 return
244 static_cast<Seconds>(
245 (ticks_ /
248 );
249 }
250
254 {
255 return
256 static_cast<Milliseconds>
257 ((ticks_ /
260 );
261 }
262
266 {
267 return
268 static_cast<Microseconds>
269 ((ticks_ /
272 );
273 }
274
278 {
279 return
280 static_cast<Nanoseconds>
281 (ticks_ %
283 }
284
290 Ticks ticks() const
291 {
292 return ticks_;
293 }
294
296 TimeSpan&
298 const TimeSpan& other)
299 {
300 ticks_ += other.ticks_;
301 return *this;
302 }
303
305 TimeSpan&
307 const TimeSpan& other)
308 {
309 ticks_ -= other.ticks_;
310 return *this;
311 }
312
314 void
316 ONIXS_BATS_BOE_NOTHROW
317 {
318 std::swap(ticks_, other.ticks_);
319 }
320
321 private:
322 Ticks ticks_;
323 };
324
326 inline
327 bool
329 const TimeSpan& left,
330 const TimeSpan& right)
331 {
332 return left.ticks() == right.ticks();
333 }
334
336 inline
337 bool
339 const TimeSpan& left,
340 const TimeSpan& right)
341 {
342 return left.ticks() != right.ticks();
343 }
344
346 inline
347 bool
349 const TimeSpan& left,
350 const TimeSpan& right)
351 {
352 return left.ticks() < right.ticks();
353 }
354
356 inline
357 bool
359 const TimeSpan& left,
360 const TimeSpan& right)
361 {
362 return left.ticks() > right.ticks();
363 }
364
365 // TimeSpan serialization.
366
367 // Serializes timespan according to HH:MM:SS pattern.
368 ONIXS_CBOE_CFE_BOE_API
369 void
370 toStrAsHHMMSS(std::string&, TimeSpan);
371
372 // Serializes timespan according to HH:MM:SS.sss pattern.
373 ONIXS_CBOE_CFE_BOE_API
374 void
376
377 // Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
378 ONIXS_CBOE_CFE_BOE_API
379 void
381
384 {
385 enum Enum
386 {
389
392
395 };
396 };
397
398
400 inline
401 void toStr(
402 std::string& str,
403 TimeSpan timeSpan,
404 TimeSpanFormat::Enum format =
406 {
407 switch (format)
408 {
410 toStrAsHHMMSS(str, timeSpan);
411 break;
412
414 toStrAsHHMMSSmsec(str, timeSpan);
415 break;
416
418 toStrAsSDHHMMSSnsec(str, timeSpan);
419 break;
420
421 default:
422 {
423 throw std::invalid_argument(
424 "Unknown timespan format pattern specified. ");
425 }
426 }
427 }
428
430 inline
431 std::string toStr(
432 TimeSpan timeSpan,
433 TimeSpanFormat::Enum format =
435 {
436 std::string str;
437
438 toStr(str, timeSpan, format);
439
440 return str;
441 }
442
462
463
466 {
467 public:
469 typedef UInt64 Ticks;
470
472 typedef UInt32 Year;
473
475 typedef
478
480 typedef UInt32 Day;
481
483 typedef UInt32 Hour;
484
486 typedef UInt32 Minute;
487
489 typedef UInt32 Second;
490
492 typedef UInt32 Millisecond;
493
495 typedef UInt32 Microsecond;
496
498 typedef UInt32 Nanosecond;
499
501 ONIXS_CBOE_CFE_BOE_API
502 static
504
506 ONIXS_CBOE_CFE_BOE_API
507 static
509
511 explicit
513 Ticks ticks = 0) ONIXS_BATS_BOE_NOTHROW
514 : sinceEpoch_(ticks)
515 {
516 }
517
524 Year year,
525 Month month,
526 Day day,
527 Hour hour = 0,
528 Minute minute = 0,
529 Second second = 0,
531 : sinceEpoch_(
532 toTicks(
533 year, month, day,
535 )
536 {
537 }
538
541 const Timestamp& other)
542 : sinceEpoch_(other.sinceEpoch_)
543 {
544 }
545
547 Year year() const
548 {
550
551 toDate(sinceEpoch_, year, month, day);
552
553 return year;
554 }
555
557 Month month() const
558 {
560
561 toDate(sinceEpoch_, year, month, day);
562
563 return month;
564 }
565
567 Day day() const
568 {
570
571 toDate(sinceEpoch_, year, month, day);
572
573 return day;
574 }
575
577 Hour hour() const
578 {
579 return static_cast<Hour>(time().hours());
580 }
581
584 {
585 return static_cast<Minute>(time().minutes());
586 }
587
590 {
591 return static_cast<Second>(time().seconds());
592 }
593
596 {
597 return static_cast<Millisecond>(time().milliseconds());
598 }
599
602 {
603 return static_cast<Microsecond>(time().microseconds());
604 }
605
608 {
609 return static_cast<Nanosecond>(time().nanoseconds());
610 }
611
614 {
615 return
616 Timestamp(
617 sinceEpoch_ -
618 sinceEpoch_ %
620 }
621
624 {
625 return
626 TimeSpan(
627 sinceEpoch_ %
629 }
630
633 ONIXS_BATS_BOE_NOTHROW
634 {
635 return sinceEpoch_;
636 }
637
639 Timestamp&
641 const Timestamp& other)
642 {
643 sinceEpoch_ = other.sinceEpoch_;
644
645 return *this;
646 }
647
649 void
651 Timestamp& other)
652 {
653 std::swap(
654 sinceEpoch_,
655 other.sinceEpoch_);
656 }
657
658 private:
659 // Time interval in nanoseconds since the 01-01-1970.
660 Ticks sinceEpoch_;
661
662 // Converts structured date-time into ticks.
663 ONIXS_CBOE_CFE_BOE_API
664 static
665 Ticks toTicks(
666 Year, Month, Day,
668 Nanosecond);
669
670 // Extracts date components from ticks.
671 ONIXS_CBOE_CFE_BOE_API
672 static
673 void toDate(Ticks, Year&, Month&, Day&);
674 };
675
676
678 inline
679 bool
681 const Timestamp& left,
682 const Timestamp& right)
683 {
684 return (
685 left.sinceEpoch() ==
686 right.sinceEpoch()
687 );
688 }
689
691 inline
692 bool
694 const Timestamp& left,
695 const Timestamp& right)
696 {
697 return (
698 left.sinceEpoch() !=
699 right.sinceEpoch()
700 );
701 }
702
704 inline
705 bool
707 const Timestamp& left,
708 const Timestamp& right)
709 {
710 return (
711 left.sinceEpoch() <
712 right.sinceEpoch()
713 );
714 }
715
717 inline
718 bool
720 const Timestamp& left,
721 const Timestamp& right)
722 {
723 return (
724 left.sinceEpoch() <=
725 right.sinceEpoch()
726 );
727 }
728
730 inline
731 bool
733 const Timestamp& left,
734 const Timestamp& right)
735 {
736 return (
737 left.sinceEpoch() >
738 right.sinceEpoch()
739 );
740 }
741
743 inline
744 bool
746 const Timestamp& left,
747 const Timestamp& right)
748 {
749 return (
750 left.sinceEpoch() >=
751 right.sinceEpoch()
752 );
753 }
754
756 inline
757 Timestamp
759 const Timestamp& timestamp,
760 const TimeSpan& timeSpan)
761 {
762 return
763 Timestamp(
764 timestamp.sinceEpoch() +
765 timeSpan.ticks()
766 );
767 }
768
770 inline
771 Timestamp
773 const Timestamp& timestamp,
774 const TimeSpan& timeSpan)
775 {
776 return
777 Timestamp(
778 timestamp.sinceEpoch() -
779 timeSpan.ticks()
780 );
781 }
782
784 inline
785 TimeSpan
787 const Timestamp& left,
788 const Timestamp& right)
789 {
790 return
791 TimeSpan(
792 left.sinceEpoch() -
793 right.sinceEpoch()
794 );
795 }
796
797 // Serialization.
798
800 ONIXS_CBOE_CFE_BOE_API
801 void
803 (
804 std::string&,
806 );
807
809 ONIXS_CBOE_CFE_BOE_API
810 void
812 (
813 std::string&,
815 );
816
818 ONIXS_CBOE_CFE_BOE_API
819 void
821 (
822 std::string&,
824 );
825
827 ONIXS_CBOE_CFE_BOE_API
828 void
830 (
831 std::string&,
833 );
834
853
855 ONIXS_CBOE_CFE_BOE_API
856 void
858 std::string& str,
859 Timestamp timestamp,
860 TimestampFormat::Enum format =
862
864 inline
865 std::string
867 Timestamp timestamp,
868 TimestampFormat::Enum format =
870 {
871 std::string str;
872
873 toStr(str, timestamp, format);
874
875 return str;
876 }
877
879 ONIXS_CBOE_CFE_BOE_API
880 bool
882 Timestamp&,
883 const char*,
884 size_t);
885
886 inline
887 bool
889 Timestamp& ts,
890 const std::string& str)
891 {
892 return
893 fromStr(
894 ts, str.c_str(), str.size());
895 }
896
897
898
899
900 }
901 }
902 }
903}
Milliseconds milliseconds() const
Definition Time.h:253
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:163
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:142
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:136
Nanoseconds nanoseconds() const
Definition Time.h:277
void swap(TimeSpan &other) ONIXS_BATS_BOE_NOTHROW
Exchanges with given instance.
Definition Time.h:315
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:148
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:139
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition Time.h:297
TimeSpan(Ticks ticks=0) ONIXS_BATS_BOE_NOTHROW
Initializes timespan from given number of ticks.
Definition Time.h:151
Microseconds microseconds() const
Definition Time.h:265
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:145
Int32 Hours
Integral type for number of hours.
Definition Time.h:133
Int32 Days
Integral type for number of days.
Definition Time.h:130
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition Time.h:306
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:127
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:189
Represents time point without time-zone information.
Definition Time.h:466
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:601
Month month() const
Month component of given time point.
Definition Time.h:557
Year year() const
Year component of given time point.
Definition Time.h:547
Timestamp(const Timestamp &other)
Initializes as copy of other instance.
Definition Time.h:540
Day day() const
Day component of given time point.
Definition Time.h:567
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:483
static ONIXS_CBOE_CFE_BOE_API Timestamp utcNow()
Returns current UTC time.
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:486
Minute minute() const
Minute component of given time point.
Definition Time.h:583
Hour hour() const
Hour component of given time point.
Definition Time.h:577
OnixS::CboeCFE::Trading::BOE::Month::Enum Month
Type presenting month component.
Definition Time.h:477
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:492
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:607
Timestamp date() const
Timestamp without a time part.
Definition Time.h:613
static ONIXS_CBOE_CFE_BOE_API Timestamp now()
Returns current local time.
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:469
TimeSpan time() const
Time part of timestamp.
Definition Time.h:623
Second second() const
Second component of given time point.
Definition Time.h:589
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:495
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:523
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:650
UInt32 Second
Integral type presenting second component.
Definition Time.h:489
UInt32 Day
Integral type presenting day component.
Definition Time.h:480
Timestamp & operator=(const Timestamp &other)
Reinitializes as copy of given instance.
Definition Time.h:640
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:498
UInt32 Year
Integral type presenting year component.
Definition Time.h:472
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:595
Ticks sinceEpoch() const ONIXS_BATS_BOE_NOTHROW
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:632
Timestamp(Ticks ticks=0) ONIXS_BATS_BOE_NOTHROW
Initializes from number of ticks since epoch.
Definition Time.h:512
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:131
ONIXS_CBOE_CFE_BOE_API void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:117
bool operator<=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:138
void toStr(std::string &str, const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition Decimal.h:156
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:110
ONIXS_CBOE_CFE_BOE_API bool fromStr(Timestamp &, const char *, size_t)
De-serializes a timestamp from the given string.
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition Time.h:758
ONIXS_CBOE_CFE_BOE_API void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:145
ONIXS_CBOE_CFE_BOE_API void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
ONIXS_CBOE_CFE_BOE_API void toStrAsHHMMSSmsec(std::string &, TimeSpan)
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:124
ONIXS_CBOE_CFE_BOE_API void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
ONIXS_CBOE_CFE_BOE_API void toStrAsHHMMSS(std::string &, TimeSpan)
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition Time.h:772
ONIXS_CBOE_CFE_BOE_API void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
Identifies months in year.
Definition Time.h:445
Collection of timespan formatting patterns.
Definition Time.h:384
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Time.h:394
Miscellaneous time characteristics.
Definition Time.h:42
static ONIXS_BATS_BOE_CONSTEXPR Int64 nanosecondsPerHour()
Returns number of nanoseconds in single hour.
Definition Time.h:52
static ONIXS_BATS_BOE_CONSTEXPR Int32 minutesPerHour()
Returns number of minutes in single hour.
Definition Time.h:94
static ONIXS_BATS_BOE_CONSTEXPR Int64 nanosecondsPerMinute()
Returns number of nanoseconds in single minute.
Definition Time.h:59
static ONIXS_BATS_BOE_CONSTEXPR Int32 nanosecondsPerMicrosecond()
Returns number of nanoseconds in single microsecond.
Definition Time.h:80
static ONIXS_BATS_BOE_CONSTEXPR Int32 nanosecondsPerMillisecond()
Returns number of nanoseconds in single millisecond.
Definition Time.h:73
static ONIXS_BATS_BOE_CONSTEXPR Int32 nanosecondsPerSecond()
Returns number of nanoseconds in single second.
Definition Time.h:66
static ONIXS_BATS_BOE_CONSTEXPR Int32 microsecondsPerSecond()
Returns number of microseconds in single second.
Definition Time.h:115
static ONIXS_BATS_BOE_CONSTEXPR Int32 secondsPerMinute()
Returns number of seconds in single minute.
Definition Time.h:101
static ONIXS_BATS_BOE_CONSTEXPR Int64 nanosecondsPerDay()
Returns number of nanoseconds in single day.
Definition Time.h:45
static ONIXS_BATS_BOE_CONSTEXPR Int32 hoursPerDay()
Returns number of hours in single day.
Definition Time.h:87
static ONIXS_BATS_BOE_CONSTEXPR Int32 millisecondsPerSecond()
Returns number of milliseconds in single second.
Definition Time.h:108
Collection of timestamp formatting patterns.
Definition Time.h:837
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:847
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:850