OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and 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
21#pragma once
22
25
26#include <stdexcept>
27
30
31
33{
35 static constexpr Int64 nanosecondsPerDay() noexcept
36 {
37 return 86400000000000ll;
38 }
39
41 static constexpr Int64 nanosecondsPerHour() noexcept
42 {
43 return 3600000000000ll;
44 }
45
47 static constexpr Int64 nanosecondsPerMinute() noexcept
48 {
49 return 60000000000ll;
50 }
51
53 static constexpr Int32 nanosecondsPerSecond() noexcept
54 {
55 return 1000000000;
56 }
57
59 static constexpr Int32 nanosecondsPerMillisecond() noexcept
60 {
61 return 1000000;
62 }
63
65 static constexpr Int32 nanosecondsPerMicrosecond() noexcept
66 {
67 return 1000;
68 }
69
71 static constexpr Int32 hoursPerDay() noexcept
72 {
73 return 24;
74 }
75
77 static constexpr Int32 minutesPerHour() noexcept
78 {
79 return 60;
80 }
81
83 static constexpr Int32 secondsPerMinute() noexcept
84 {
85 return 60;
86 }
87
89 static constexpr Int32 millisecondsPerSecond() noexcept
90 {
91 return 1000;
92 }
93
95 static constexpr Int32 microsecondsPerSecond() noexcept
96 {
97 return 1000000;
98 }
99};
100
131
136{
137public:
139 typedef Int64 Ticks;
140
142 typedef Int32 Days;
143
145 typedef Int32 Hours;
146
148 typedef Int32 Minutes;
149
151 typedef Int32 Seconds;
152
155
158
161
165
167 explicit TimeSpan(Ticks ticks = 0) noexcept
168 : ticks_(ticks)
169 {
170 }
171
180 : ticks_(
181 numericCast<Ticks>(days) * TimeTraits::nanosecondsPerDay()
182 + numericCast<Ticks>(hours) * TimeTraits::nanosecondsPerHour()
183 + numericCast<Ticks>(minutes) * TimeTraits::nanosecondsPerMinute()
184 + numericCast<Ticks>(seconds) * TimeTraits::nanosecondsPerSecond() + nanoseconds
185 )
186 {
187 }
188
197 : ticks_(
198 numericCast<Ticks>(hours) * TimeTraits::nanosecondsPerHour()
199 + numericCast<Ticks>(minutes) * TimeTraits::nanosecondsPerMinute()
200 + numericCast<Ticks>(seconds) * TimeTraits::nanosecondsPerSecond() + nanoseconds
201 )
202 {
203 }
204
206 Days days() const noexcept
207 {
208 return numericCast<Days>(ticks_ / TimeTraits::nanosecondsPerDay());
209 }
210
214 Hours hours() const noexcept
215 {
216 return numericCast<Hours>((ticks_ / TimeTraits::nanosecondsPerHour()) % TimeTraits::hoursPerDay());
217 }
218
222 Minutes minutes() const noexcept
223 {
224 return numericCast<Minutes>((ticks_ / TimeTraits::nanosecondsPerMinute()) % TimeTraits::minutesPerHour());
225 }
226
230 Seconds seconds() const noexcept
231 {
232 return numericCast<Seconds>((ticks_ / TimeTraits::nanosecondsPerSecond()) % TimeTraits::secondsPerMinute());
233 }
234
238 Milliseconds milliseconds() const noexcept
239 {
240 return numericCast<Milliseconds>(
242 );
243 }
244
248 Microseconds microseconds() const noexcept
249 {
250 return numericCast<Microseconds>(
252 );
253 }
254
258 Nanoseconds nanoseconds() const noexcept
259 {
260 return numericCast<Nanoseconds>(ticks_ % TimeTraits::nanosecondsPerSecond());
261 }
262
267 Ticks ticks() const noexcept
268 {
269 return ticks_;
270 }
271
273 TimeSpan& operator+=(const TimeSpan& other) noexcept
274 {
275 ticks_ += other.ticks_;
276
277 return *this;
278 }
279
281 TimeSpan& operator-=(const TimeSpan& other) noexcept
282 {
283 ticks_ -= other.ticks_;
284
285 return *this;
286 }
287
289 void swap(TimeSpan& other) noexcept
290 {
291 std::swap(ticks_, other.ticks_);
292 }
293
297 static TimeSpan fromStr(const std::string&);
298
299private:
300 Ticks ticks_;
301};
302
304class SecondSpan : public TimeSpan
305{
306public:
308 : TimeSpan(numericCast<TimeSpan::Ticks>(seconds) * TimeTraits::nanosecondsPerSecond())
309 {
310 }
311};
312
315{
316public:
318 : TimeSpan(numericCast<TimeSpan::Ticks>(milliseconds) * TimeTraits::nanosecondsPerMillisecond())
319 {
320 }
321};
322
325{
326public:
328 : TimeSpan(numericCast<TimeSpan::Ticks>(microseconds) * TimeTraits::nanosecondsPerMicrosecond())
329 {
330 }
331};
332
334inline bool operator==(const TimeSpan& left, const TimeSpan& right) noexcept
335{
336 return left.ticks() == right.ticks();
337}
338
340inline bool operator!=(const TimeSpan& left, const TimeSpan& right) noexcept
341{
342 return left.ticks() != right.ticks();
343}
344
346inline bool operator<(const TimeSpan& left, const TimeSpan& right) noexcept
347{
348 return left.ticks() < right.ticks();
349}
350
352inline bool operator>(const TimeSpan& left, const TimeSpan& right) noexcept
353{
354 return left.ticks() > right.ticks();
355}
356
358inline TimeSpan operator-(const TimeSpan& timeSpan) noexcept
359{
360 return TimeSpan(-timeSpan.ticks());
361}
362
365void toStrAsHHMMSS(std::string&, TimeSpan);
366
369void toStrAsHHMMSSmsec(std::string&, TimeSpan);
370
373void toStrAsHHMMSSusec(std::string&, TimeSpan);
374
377void toStrAsHHMMSSnsec(std::string&, TimeSpan);
378
381void toStrAsHHMMSSpsec(std::string&, TimeSpan);
382
385void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
386
390
393{
394 std::string str;
395
396 toStr(str, timeSpan, format);
397
398 return str;
399}
400
422
447
450{
451public:
453 typedef UInt64 Ticks;
454
456 typedef UInt32 Year;
457
459 using Month = ONIXS_CMEMDH_MESSAGING_NAMESPACE::Month::Enum;
460
462 typedef UInt32 Day;
463
465 typedef UInt32 Hour;
466
468 typedef UInt32 Minute;
469
471 typedef UInt32 Second;
472
475
478
481
485
487 explicit Timestamp(Ticks ticks = 0) noexcept
488 : sinceEpoch_(ticks)
489 {
490 }
491
497 Year year,
498 Month month,
499 Day day,
500 Hour hour = 0,
501 Minute minute = 0,
502 Second second = 0,
504 )
505 : sinceEpoch_(toTicks(year, month, day, hour, minute, second, nanosecond))
506 {
507 }
508
509 Timestamp(const Timestamp& other) noexcept
510 : sinceEpoch_(other.sinceEpoch_)
511 {
512 }
513
515 Year year() const
516 {
517 Year year;
518 Month month;
519 Day day;
520
521 toDate(sinceEpoch_, year, month, day);
522
523 return year;
524 }
525
527 Month month() const
528 {
529 Year year;
530 Month month;
531 Day day;
532
533 toDate(sinceEpoch_, year, month, day);
534
535 return month;
536 }
537
539 Day day() const
540 {
541 Year year;
542 Month month;
543 Day day;
544
545 toDate(sinceEpoch_, year, month, day);
546
547 return day;
548 }
549
551 Hour hour() const noexcept
552 {
553 return static_cast<Hour>(time().hours());
554 }
555
557 Minute minute() const noexcept
558 {
559 return static_cast<Minute>(time().minutes());
560 }
561
563 Second second() const noexcept
564 {
565 return static_cast<Second>(time().seconds());
566 }
567
569 Millisecond millisecond() const noexcept
570 {
571 return static_cast<Millisecond>(time().milliseconds());
572 }
573
575 Microsecond microsecond() const noexcept
576 {
577 return static_cast<Microsecond>(time().microseconds());
578 }
579
581 Nanosecond nanosecond() const noexcept
582 {
583 return static_cast<Nanosecond>(time().nanoseconds());
584 }
585
587 Timestamp date() const noexcept
588 {
589 return Timestamp(sinceEpoch_ - sinceEpoch_ % TimeTraits::nanosecondsPerDay());
590 }
591
593 TimeSpan time() const noexcept
594 {
595 return TimeSpan(sinceEpoch_ % TimeTraits::nanosecondsPerDay());
596 }
597
599 Ticks sinceEpoch() const noexcept
600 {
601 return sinceEpoch_;
602 }
603
604 Timestamp& operator=(const Timestamp& other) noexcept
605 {
606 sinceEpoch_ = other.sinceEpoch_;
607
608 return *this;
609 }
610
612 void swap(Timestamp& other) noexcept
613 {
614 std::swap(sinceEpoch_, other.sinceEpoch_);
615 }
616
621
624
627
630 struct MemberTraits
631 {
632 enum
633 {
634 Count = 1
635 };
636
637 typedef Ticks FirstArgType;
638 };
639
640 enum
641 {
643 };
644
645private:
647 Ticks sinceEpoch_;
648
651 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
652
653 // Extracts the ddate components from ticks.
655 static void toDate(Ticks, Year&, Month&, Day&);
656};
657
660{
661 return Timestamp(ticks);
662}
663
665inline bool operator==(const Timestamp& left, const Timestamp& right) noexcept
666{
667 return (left.sinceEpoch() == right.sinceEpoch());
668}
669
671inline bool operator!=(const Timestamp& left, const Timestamp& right) noexcept
672{
673 return (left.sinceEpoch() != right.sinceEpoch());
674}
675
677inline bool operator<(const Timestamp& left, const Timestamp& right) noexcept
678{
679 return (left.sinceEpoch() < right.sinceEpoch());
680}
681
683inline bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
684{
685 return (left.sinceEpoch() <= right.sinceEpoch());
686}
687
689inline bool operator>(const Timestamp& left, const Timestamp& right) noexcept
690{
691 return (left.sinceEpoch() > right.sinceEpoch());
692}
693
695inline bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
696{
697 return (left.sinceEpoch() >= right.sinceEpoch());
698}
699
701inline Timestamp operator+(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
702{
703 return Timestamp(timestamp.sinceEpoch() + timeSpan.ticks());
704}
705
707inline Timestamp operator-(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
708{
709 return Timestamp(timestamp.sinceEpoch() - timeSpan.ticks());
710}
711
713inline TimeSpan operator-(const Timestamp& left, const Timestamp& right) noexcept
714{
715 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
716}
717
720void toStrAsYYYYMMDD(std::string&, Timestamp);
721
725
729
733
737
741
745
747inline ONIXS_CMEMDH_NODISCARD std::string
749{
750 std::string str;
751
752 toStr(str, timestamp, format);
753
754 return str;
755}
756
759size_t toStr(Timestamp, Char*, size_t);
760
761inline std::ostream& operator<<(std::ostream& os, const Timestamp& value)
762{
763 return os << toStr(value);
764}
765
766inline std::ostream& operator<<(std::ostream& os, const TimeSpan& value)
767{
768 return os << toStr(value);
769}
770
774bool fromStr(TimeSpan&, const Char*, size_t);
775
777inline ONIXS_CMEMDH_NODISCARD bool fromStr(TimeSpan& ts, const std::string& str)
778{
779 return fromStr(ts, str.c_str(), str.size());
780}
781
786
788inline ONIXS_CMEMDH_NODISCARD bool
790{
791 return fromStr(ts, str.c_str(), str.size(), format);
792}
793
794inline Timestamp Timestamp::fromStr(const std::string& str, TimestampFormat::Enum format)
795{
796 Timestamp ts;
797
798 const bool result = Messaging::fromStr(ts, str, format);
799
800 if (!result)
801 throw std::runtime_error("Error parsing timestamp.");
802
803 return ts;
804}
805
806inline TimeSpan TimeSpan::fromStr(const std::string& str)
807{
808 TimeSpan ts;
809
810 const bool result = Messaging::fromStr(ts, str);
811
812 if (!result)
813 throw std::runtime_error("Error parsing timespan.");
814
815 return ts;
816}
817
818inline std::string TimeSpan::toString(TimeSpanFormat::Enum format) const
819{
820 return toStr(*this, format);
821}
822
823inline std::string Timestamp::toString(TimestampFormat::Enum format) const
824{
825 return toStr(*this, format);
826}
827
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:77
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:78
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition Bootstrap.h:62
#define ONIXS_CMEMDH_DATA_PACKING_END
Definition Compiler.h:197
#define ONIXS_CMEMDH_DATA_PACKING_BEGIN(alignment)
Definition Compiler.h:196
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:163
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
MicrosecondSpan(TimeSpan::Microseconds microseconds) noexcept
Definition Time.h:327
MillisecondSpan(TimeSpan::Milliseconds milliseconds) noexcept
Definition Time.h:317
SecondSpan(TimeSpan::Seconds seconds) noexcept
Definition Time.h:307
Nanoseconds nanoseconds() const noexcept
Definition Time.h:258
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:154
static TimeSpan fromStr(const std::string &)
Definition Time.h:806
void swap(TimeSpan &other) noexcept
Swaps.
Definition Time.h:289
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:148
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Definition Time.h:196
TimeSpan(Ticks ticks=0) noexcept
Initializes the timespan from the given number of ticks.
Definition Time.h:167
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Definition Time.h:179
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:160
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:151
Microseconds microseconds() const noexcept
Definition Time.h:248
Ticks ticks() const noexcept
Definition Time.h:267
Hours hours() const noexcept
Definition Time.h:214
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:157
TimeSpan & operator-=(const TimeSpan &other) noexcept
Subtracts the given time interval.
Definition Time.h:281
std::string toString(TimeSpanFormat::Enum=TimeSpanFormat::SDHHMMSSnsec) const
Definition Time.h:818
Int32 Hours
Integral type for number of hours.
Definition Time.h:145
TimeSpan & operator+=(const TimeSpan &other) noexcept
Adds the given time interval.
Definition Time.h:273
Int32 Days
Integral type for number of days.
Definition Time.h:142
Days days() const noexcept
Definition Time.h:206
Minutes minutes() const noexcept
Definition Time.h:222
Milliseconds milliseconds() const noexcept
Definition Time.h:238
Seconds seconds() const noexcept
Definition Time.h:230
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:139
The time point without the time-zone information.
Definition Time.h:450
static Timestamp fromStr(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec)
Definition Time.h:794
Microsecond microsecond() const noexcept
Definition Time.h:575
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:465
Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:487
Minute minute() const noexcept
Definition Time.h:557
static Timestamp now() noexcept
Current local time.
void swap(Timestamp &other) noexcept
Exchanges the value.
Definition Time.h:612
UInt32 Minute
Integral type presenting the minute component.
Definition Time.h:468
TimeSpan time() const noexcept
Definition Time.h:593
static Timestamp utcNow() noexcept
Current utc time.
UInt32 Millisecond
Integral type presenting the millisecond component.
Definition Time.h:474
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:453
Timestamp date() const noexcept
Definition Time.h:587
Timestamp & operator=(const Timestamp &other) noexcept
Definition Time.h:604
UInt32 Microsecond
Integral type presenting the microsecond component.
Definition Time.h:477
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:496
UInt32 Second
Integral type presenting the second component.
Definition Time.h:471
UInt32 Day
Integral type presenting the day component.
Definition Time.h:462
Second second() const noexcept
Definition Time.h:563
UInt32 Nanosecond
Integral type presenting the nanosecond component.
Definition Time.h:480
OnixS::CME::MDH::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:459
Hour hour() const noexcept
Definition Time.h:551
UInt32 Year
Integral type presenting the year component.
Definition Time.h:456
Ticks sinceEpoch() const noexcept
Definition Time.h:599
Millisecond millisecond() const noexcept
Definition Time.h:569
Nanosecond nanosecond() const noexcept
Definition Time.h:581
Timestamp(const Timestamp &other) noexcept
Definition Time.h:509
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec) const
Definition Time.h:823
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:695
void toStrAsHHMMSSpsec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.ssssssssssss pattern.
Timestamp makeTimestamp(Timestamp::Ticks ticks) noexcept
Make Timestamp helper.
Definition Time.h:659
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:683
TimeSpan operator-(const TimeSpan &timeSpan) noexcept
Changes the sign of the Timestamp.
Definition Time.h:358
void toStrAsHHMMSSnsec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.sssssssss pattern.
void toStrAsYYYYMMDDHHMMSSusec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSusec format.
bool fromStr(Int8 &, const Char *, size_t)
std::uint32_t UInt32
uInt32.
Definition Integral.h:35
void toStrAsHHMMSSusec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.ssssss pattern.
bool operator!=(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:340
void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSmsec format.
char Char
Character type alias.
Definition String.h:30
bool operator==(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:334
void toStrAsHHMMSSmsec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.sss pattern.
void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSS format.
void toStrAsHHMMSS(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS pattern.
std::int32_t Int32
int32.
Definition Integral.h:34
void toStrAsYYYYMMDDHHMMSSpsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSpsec format.
bool operator<(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:346
void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDD format.
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
Serializes the timespan according to the D.HH:MM:SS.sssssssss pattern.
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan) noexcept
Adds the time interval.
Definition Time.h:701
std::uint64_t UInt64
uInt64.
Definition Integral.h:37
bool operator>(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:352
void toStr(std::string &, Int8)
Serializes given integer into a string.
std::uint8_t UInt8
uInt8.
Definition Integral.h:31
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSnsec format.
std::ostream & operator<<(std::ostream &os, const Timestamp &value)
Definition Time.h:761
Messaging::Timestamp timestamp(const Messaging::FIX::MultiContainer &, Messaging::FIX::Tag)
Retrieves last update time field value.
The months in year.
Definition Time.h:403
Timespan formatting patterns.
Definition Time.h:103
Miscellaneous time characteristics.
Definition Time.h:33
static constexpr Int32 hoursPerDay() noexcept
Definition Time.h:71
static constexpr Int32 nanosecondsPerSecond() noexcept
Definition Time.h:53
static constexpr Int32 secondsPerMinute() noexcept
Definition Time.h:83
static constexpr Int32 minutesPerHour() noexcept
Definition Time.h:77
static constexpr Int64 nanosecondsPerDay() noexcept
Definition Time.h:35
static constexpr Int32 microsecondsPerSecond() noexcept
Definition Time.h:95
static constexpr Int32 millisecondsPerSecond() noexcept
Definition Time.h:89
static constexpr Int64 nanosecondsPerMinute() noexcept
Definition Time.h:47
static constexpr Int32 nanosecondsPerMicrosecond() noexcept
Definition Time.h:65
static constexpr Int32 nanosecondsPerMillisecond() noexcept
Definition Time.h:59
static constexpr Int64 nanosecondsPerHour() noexcept
Definition Time.h:41
Timestamp formatting patterns.
Definition Time.h:425
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:435
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:441
@ YYYYMMDDHHMMSSpsec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssssssssss format.
Definition Time.h:444
@ YYYYMMDDHHMMSSusec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssss format.
Definition Time.h:438