OnixS C++ CME MDP Premium Market Data Handler 5.10.3
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
335inline bool operator==(const TimeSpan& left, const TimeSpan& right) noexcept
336{
337 return left.ticks() == right.ticks();
338}
339
342inline bool operator!=(const TimeSpan& left, const TimeSpan& right) noexcept
343{
344 return left.ticks() != right.ticks();
345}
346
349inline bool operator<(const TimeSpan& left, const TimeSpan& right) noexcept
350{
351 return left.ticks() < right.ticks();
352}
353
356inline bool operator>(const TimeSpan& left, const TimeSpan& right) noexcept
357{
358 return left.ticks() > right.ticks();
359}
360
363inline TimeSpan operator-(const TimeSpan& timeSpan) noexcept
364{
365 return TimeSpan(-timeSpan.ticks());
366}
367
370void toStrAsHHMMSS(std::string&, TimeSpan);
371
374void toStrAsHHMMSSmsec(std::string&, TimeSpan);
375
378void toStrAsHHMMSSusec(std::string&, TimeSpan);
379
382void toStrAsHHMMSSnsec(std::string&, TimeSpan);
383
386void toStrAsHHMMSSpsec(std::string&, TimeSpan);
387
390void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
391
395
398{
399 std::string str;
400
401 toStr(str, timeSpan, format);
402
403 return str;
404}
405
427
452
455{
456public:
458 typedef UInt64 Ticks;
459
461 typedef UInt32 Year;
462
464 using Month = ONIXS_CMEMDH_MESSAGING_NAMESPACE::Month::Enum;
465
467 typedef UInt32 Day;
468
470 typedef UInt32 Hour;
471
473 typedef UInt32 Minute;
474
476 typedef UInt32 Second;
477
480
483
486
490
492 explicit Timestamp(Ticks ticks = 0) noexcept
493 : sinceEpoch_(ticks)
494 {
495 }
496
502 Year year,
503 Month month,
504 Day day,
505 Hour hour = 0,
506 Minute minute = 0,
507 Second second = 0,
509 )
510 : sinceEpoch_(toTicks(year, month, day, hour, minute, second, nanosecond))
511 {
512 }
513
514 Timestamp(const Timestamp& other) noexcept
515 : sinceEpoch_(other.sinceEpoch_)
516 {
517 }
518
520 Year year() const
521 {
522 Year year;
523 Month month;
524 Day day;
525
526 toDate(sinceEpoch_, year, month, day);
527
528 return year;
529 }
530
532 Month month() const
533 {
534 Year year;
535 Month month;
536 Day day;
537
538 toDate(sinceEpoch_, year, month, day);
539
540 return month;
541 }
542
544 Day day() const
545 {
546 Year year;
547 Month month;
548 Day day;
549
550 toDate(sinceEpoch_, year, month, day);
551
552 return day;
553 }
554
556 Hour hour() const noexcept
557 {
558 return static_cast<Hour>(time().hours());
559 }
560
562 Minute minute() const noexcept
563 {
564 return static_cast<Minute>(time().minutes());
565 }
566
568 Second second() const noexcept
569 {
570 return static_cast<Second>(time().seconds());
571 }
572
574 Millisecond millisecond() const noexcept
575 {
576 return static_cast<Millisecond>(time().milliseconds());
577 }
578
580 Microsecond microsecond() const noexcept
581 {
582 return static_cast<Microsecond>(time().microseconds());
583 }
584
586 Nanosecond nanosecond() const noexcept
587 {
588 return static_cast<Nanosecond>(time().nanoseconds());
589 }
590
592 Timestamp date() const noexcept
593 {
594 return Timestamp(sinceEpoch_ - sinceEpoch_ % TimeTraits::nanosecondsPerDay());
595 }
596
598 TimeSpan time() const noexcept
599 {
600 return TimeSpan(sinceEpoch_ % TimeTraits::nanosecondsPerDay());
601 }
602
604 Ticks sinceEpoch() const noexcept
605 {
606 return sinceEpoch_;
607 }
608
609 Timestamp& operator=(const Timestamp& other) noexcept
610 {
611 sinceEpoch_ = other.sinceEpoch_;
612
613 return *this;
614 }
615
617 void swap(Timestamp& other) noexcept
618 {
619 std::swap(sinceEpoch_, other.sinceEpoch_);
620 }
621
626
629
632
635 struct MemberTraits
636 {
637 enum
638 {
639 Count = 1
640 };
641
642 typedef Ticks FirstArgType;
643 };
644
645 enum
646 {
648 };
649
650private:
652 Ticks sinceEpoch_;
653
656 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
657
658 // Extracts the ddate components from ticks.
660 static void toDate(Ticks, Year&, Month&, Day&);
661};
662
666{
667 return Timestamp(ticks);
668}
669
672inline bool operator==(const Timestamp& left, const Timestamp& right) noexcept
673{
674 return (left.sinceEpoch() == right.sinceEpoch());
675}
676
679inline bool operator!=(const Timestamp& left, const Timestamp& right) noexcept
680{
681 return (left.sinceEpoch() != right.sinceEpoch());
682}
683
686inline bool operator<(const Timestamp& left, const Timestamp& right) noexcept
687{
688 return (left.sinceEpoch() < right.sinceEpoch());
689}
690
693inline bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
694{
695 return (left.sinceEpoch() <= right.sinceEpoch());
696}
697
700inline bool operator>(const Timestamp& left, const Timestamp& right) noexcept
701{
702 return (left.sinceEpoch() > right.sinceEpoch());
703}
704
707inline bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
708{
709 return (left.sinceEpoch() >= right.sinceEpoch());
710}
711
714inline Timestamp operator+(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
715{
716 return Timestamp(timestamp.sinceEpoch() + timeSpan.ticks());
717}
718
721inline Timestamp operator-(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
722{
723 return Timestamp(timestamp.sinceEpoch() - timeSpan.ticks());
724}
725
728inline TimeSpan operator-(const Timestamp& left, const Timestamp& right) noexcept
729{
730 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
731}
732
735void toStrAsYYYYMMDD(std::string&, Timestamp);
736
740
744
748
752
756
760
762inline ONIXS_CMEMDH_NODISCARD std::string
764{
765 std::string str;
766
767 toStr(str, timestamp, format);
768
769 return str;
770}
771
774size_t toStr(Timestamp, Char*, size_t);
775
776inline std::ostream& operator<<(std::ostream& os, const Timestamp& value)
777{
778 return os << toStr(value);
779}
780
781inline std::ostream& operator<<(std::ostream& os, const TimeSpan& value)
782{
783 return os << toStr(value);
784}
785
789bool fromStr(TimeSpan&, const Char*, size_t);
790
792inline ONIXS_CMEMDH_NODISCARD bool fromStr(TimeSpan& ts, const std::string& str)
793{
794 return fromStr(ts, str.c_str(), str.size());
795}
796
801
803inline ONIXS_CMEMDH_NODISCARD bool
805{
806 return fromStr(ts, str.c_str(), str.size(), format);
807}
808
809inline Timestamp Timestamp::fromStr(const std::string& str, TimestampFormat::Enum format)
810{
811 Timestamp ts;
812
813 const bool result = Messaging::fromStr(ts, str, format);
814
815 if (!result)
816 throw std::runtime_error("Error parsing timestamp.");
817
818 return ts;
819}
820
821inline TimeSpan TimeSpan::fromStr(const std::string& str)
822{
823 TimeSpan ts;
824
825 const bool result = Messaging::fromStr(ts, str);
826
827 if (!result)
828 throw std::runtime_error("Error parsing timespan.");
829
830 return ts;
831}
832
833inline std::string TimeSpan::toString(TimeSpanFormat::Enum format) const
834{
835 return toStr(*this, format);
836}
837
838inline std::string Timestamp::toString(TimestampFormat::Enum format) const
839{
840 return toStr(*this, format);
841}
842
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:57
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:58
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition Bootstrap.h:47
#define ONIXS_CMEMDH_DATA_PACKING_END
Definition Compiler.h:176
#define ONIXS_CMEMDH_DATA_PACKING_BEGIN(alignment)
Definition Compiler.h:175
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:148
#define ONIXS_CMEMDH_PURE
Definition Compiler.h:154
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:150
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:821
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:833
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:455
static Timestamp fromStr(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec)
Definition Time.h:809
Microsecond microsecond() const noexcept
Definition Time.h:580
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:470
Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:492
Minute minute() const noexcept
Definition Time.h:562
static Timestamp now() noexcept
Current local time.
void swap(Timestamp &other) noexcept
Exchanges the value.
Definition Time.h:617
UInt32 Minute
Integral type presenting the minute component.
Definition Time.h:473
TimeSpan time() const noexcept
Definition Time.h:598
static Timestamp utcNow() noexcept
Current utc time.
UInt32 Millisecond
Integral type presenting the millisecond component.
Definition Time.h:479
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:458
Timestamp date() const noexcept
Definition Time.h:592
Timestamp & operator=(const Timestamp &other) noexcept
Definition Time.h:609
UInt32 Microsecond
Integral type presenting the microsecond component.
Definition Time.h:482
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:501
UInt32 Second
Integral type presenting the second component.
Definition Time.h:476
UInt32 Day
Integral type presenting the day component.
Definition Time.h:467
Second second() const noexcept
Definition Time.h:568
UInt32 Nanosecond
Integral type presenting the nanosecond component.
Definition Time.h:485
OnixS::CME::MDH::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:464
Hour hour() const noexcept
Definition Time.h:556
UInt32 Year
Integral type presenting the year component.
Definition Time.h:461
Ticks sinceEpoch() const noexcept
Definition Time.h:604
Millisecond millisecond() const noexcept
Definition Time.h:574
Nanosecond nanosecond() const noexcept
Definition Time.h:586
Timestamp(const Timestamp &other) noexcept
Definition Time.h:514
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec) const
Definition Time.h:838
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:707
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:665
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:693
TimeSpan operator-(const TimeSpan &timeSpan) noexcept
Changes the sign of the Timestamp.
Definition Time.h:363
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:342
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:335
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:349
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:714
std::uint64_t UInt64
uInt64.
Definition Integral.h:37
bool operator>(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:356
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:776
Messaging::Timestamp timestamp(const Messaging::FIX::MultiContainer &, Messaging::FIX::Tag)
Retrieves last update time field value.
The months in year.
Definition Time.h:408
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:430
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:440
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:446
@ YYYYMMDDHHMMSSpsec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssssssssss format.
Definition Time.h:449
@ YYYYMMDDHHMMSSusec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssss format.
Definition Time.h:443