OnixS C++ CME MDP Premium Market Data Handler 5.10.2
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
305inline bool operator==(const TimeSpan& left, const TimeSpan& right) noexcept
306{
307 return left.ticks() == right.ticks();
308}
309
312inline bool operator!=(const TimeSpan& left, const TimeSpan& right) noexcept
313{
314 return left.ticks() != right.ticks();
315}
316
319inline bool operator<(const TimeSpan& left, const TimeSpan& right) noexcept
320{
321 return left.ticks() < right.ticks();
322}
323
326inline bool operator>(const TimeSpan& left, const TimeSpan& right) noexcept
327{
328 return left.ticks() > right.ticks();
329}
330
333inline TimeSpan operator-(const TimeSpan& timeSpan) noexcept
334{
335 return TimeSpan(-timeSpan.ticks());
336}
337
340void toStrAsHHMMSS(std::string&, TimeSpan);
341
344void toStrAsHHMMSSmsec(std::string&, TimeSpan);
345
348void toStrAsHHMMSSusec(std::string&, TimeSpan);
349
352void toStrAsHHMMSSnsec(std::string&, TimeSpan);
353
356void toStrAsHHMMSSpsec(std::string&, TimeSpan);
357
360void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
361
365
368{
369 std::string str;
370
371 toStr(str, timeSpan, format);
372
373 return str;
374}
375
397
422
425{
426public:
428 typedef UInt64 Ticks;
429
431 typedef UInt32 Year;
432
434 using Month = ONIXS_CMEMDH_MESSAGING_NAMESPACE::Month::Enum;
435
437 typedef UInt32 Day;
438
440 typedef UInt32 Hour;
441
443 typedef UInt32 Minute;
444
446 typedef UInt32 Second;
447
450
453
456
460
462 explicit Timestamp(Ticks ticks = 0) noexcept
463 : sinceEpoch_(ticks)
464 {
465 }
466
472 Year year,
473 Month month,
474 Day day,
475 Hour hour = 0,
476 Minute minute = 0,
477 Second second = 0,
479 )
480 : sinceEpoch_(toTicks(year, month, day, hour, minute, second, nanosecond))
481 {
482 }
483
484 Timestamp(const Timestamp& other) noexcept
485 : sinceEpoch_(other.sinceEpoch_)
486 {
487 }
488
490 Year year() const
491 {
492 Year year;
493 Month month;
494 Day day;
495
496 toDate(sinceEpoch_, year, month, day);
497
498 return year;
499 }
500
502 Month month() const
503 {
504 Year year;
505 Month month;
506 Day day;
507
508 toDate(sinceEpoch_, year, month, day);
509
510 return month;
511 }
512
514 Day day() const
515 {
516 Year year;
517 Month month;
518 Day day;
519
520 toDate(sinceEpoch_, year, month, day);
521
522 return day;
523 }
524
526 Hour hour() const noexcept
527 {
528 return static_cast<Hour>(time().hours());
529 }
530
532 Minute minute() const noexcept
533 {
534 return static_cast<Minute>(time().minutes());
535 }
536
538 Second second() const noexcept
539 {
540 return static_cast<Second>(time().seconds());
541 }
542
544 Millisecond millisecond() const noexcept
545 {
546 return static_cast<Millisecond>(time().milliseconds());
547 }
548
550 Microsecond microsecond() const noexcept
551 {
552 return static_cast<Microsecond>(time().microseconds());
553 }
554
556 Nanosecond nanosecond() const noexcept
557 {
558 return static_cast<Nanosecond>(time().nanoseconds());
559 }
560
562 Timestamp date() const noexcept
563 {
564 return Timestamp(sinceEpoch_ - sinceEpoch_ % TimeTraits::nanosecondsPerDay());
565 }
566
568 TimeSpan time() const noexcept
569 {
570 return TimeSpan(sinceEpoch_ % TimeTraits::nanosecondsPerDay());
571 }
572
574 Ticks sinceEpoch() const noexcept
575 {
576 return sinceEpoch_;
577 }
578
579 Timestamp& operator=(const Timestamp& other) noexcept
580 {
581 sinceEpoch_ = other.sinceEpoch_;
582
583 return *this;
584 }
585
587 void swap(Timestamp& other) noexcept
588 {
589 std::swap(sinceEpoch_, other.sinceEpoch_);
590 }
591
596
599
602
605 struct MemberTraits
606 {
607 enum
608 {
609 Count = 1
610 };
611
612 typedef Ticks FirstArgType;
613 };
614
615 enum
616 {
618 };
619
620private:
622 Ticks sinceEpoch_;
623
626 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
627
628 // Extracts the ddate components from ticks.
630 static void toDate(Ticks, Year&, Month&, Day&);
631};
632
636{
637 return Timestamp(ticks);
638}
639
642inline bool operator==(const Timestamp& left, const Timestamp& right) noexcept
643{
644 return (left.sinceEpoch() == right.sinceEpoch());
645}
646
649inline bool operator!=(const Timestamp& left, const Timestamp& right) noexcept
650{
651 return (left.sinceEpoch() != right.sinceEpoch());
652}
653
656inline bool operator<(const Timestamp& left, const Timestamp& right) noexcept
657{
658 return (left.sinceEpoch() < right.sinceEpoch());
659}
660
663inline bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
664{
665 return (left.sinceEpoch() <= right.sinceEpoch());
666}
667
670inline bool operator>(const Timestamp& left, const Timestamp& right) noexcept
671{
672 return (left.sinceEpoch() > right.sinceEpoch());
673}
674
677inline bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
678{
679 return (left.sinceEpoch() >= right.sinceEpoch());
680}
681
684inline Timestamp operator+(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
685{
686 return Timestamp(timestamp.sinceEpoch() + timeSpan.ticks());
687}
688
691inline Timestamp operator-(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
692{
693 return Timestamp(timestamp.sinceEpoch() - timeSpan.ticks());
694}
695
698inline TimeSpan operator-(const Timestamp& left, const Timestamp& right) noexcept
699{
700 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
701}
702
705void toStrAsYYYYMMDD(std::string&, Timestamp);
706
710
714
718
722
726
730
732inline ONIXS_CMEMDH_NODISCARD std::string
734{
735 std::string str;
736
737 toStr(str, timestamp, format);
738
739 return str;
740}
741
744size_t toStr(Timestamp, Char*, size_t);
745
746inline std::ostream& operator<<(std::ostream& os, const Timestamp& value)
747{
748 return os << toStr(value);
749}
750
751inline std::ostream& operator<<(std::ostream& os, const TimeSpan& value)
752{
753 return os << toStr(value);
754}
755
759bool fromStr(TimeSpan&, const Char*, size_t);
760
762inline ONIXS_CMEMDH_NODISCARD bool fromStr(TimeSpan& ts, const std::string& str)
763{
764 return fromStr(ts, str.c_str(), str.size());
765}
766
771
773inline ONIXS_CMEMDH_NODISCARD bool
775{
776 return fromStr(ts, str.c_str(), str.size(), format);
777}
778
779inline Timestamp Timestamp::fromStr(const std::string& str, TimestampFormat::Enum format)
780{
781 Timestamp ts;
782
783 const bool result = Messaging::fromStr(ts, str, format);
784
785 if (!result)
786 throw std::runtime_error("Error parsing timestamp.");
787
788 return ts;
789}
790
791inline TimeSpan TimeSpan::fromStr(const std::string& str)
792{
793 TimeSpan ts;
794
795 const bool result = Messaging::fromStr(ts, str);
796
797 if (!result)
798 throw std::runtime_error("Error parsing timespan.");
799
800 return ts;
801}
802
803inline std::string TimeSpan::toString(TimeSpanFormat::Enum format) const
804{
805 return toStr(*this, format);
806}
807
808inline std::string Timestamp::toString(TimestampFormat::Enum format) const
809{
810 return toStr(*this, format);
811}
812
#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
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:791
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:803
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:425
static Timestamp fromStr(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec)
Definition Time.h:779
Microsecond microsecond() const noexcept
Definition Time.h:550
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:440
Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:462
Minute minute() const noexcept
Definition Time.h:532
static Timestamp now() noexcept
Current local time.
void swap(Timestamp &other) noexcept
Exchanges the value.
Definition Time.h:587
UInt32 Minute
Integral type presenting the minute component.
Definition Time.h:443
TimeSpan time() const noexcept
Definition Time.h:568
static Timestamp utcNow() noexcept
Current utc time.
UInt32 Millisecond
Integral type presenting the millisecond component.
Definition Time.h:449
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:428
Timestamp date() const noexcept
Definition Time.h:562
Timestamp & operator=(const Timestamp &other) noexcept
Definition Time.h:579
UInt32 Microsecond
Integral type presenting the microsecond component.
Definition Time.h:452
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:471
UInt32 Second
Integral type presenting the second component.
Definition Time.h:446
UInt32 Day
Integral type presenting the day component.
Definition Time.h:437
Second second() const noexcept
Definition Time.h:538
UInt32 Nanosecond
Integral type presenting the nanosecond component.
Definition Time.h:455
OnixS::CME::MDH::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:434
Hour hour() const noexcept
Definition Time.h:526
UInt32 Year
Integral type presenting the year component.
Definition Time.h:431
Ticks sinceEpoch() const noexcept
Definition Time.h:574
Millisecond millisecond() const noexcept
Definition Time.h:544
Nanosecond nanosecond() const noexcept
Definition Time.h:556
Timestamp(const Timestamp &other) noexcept
Definition Time.h:484
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec) const
Definition Time.h:808
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:677
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:635
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:663
TimeSpan operator-(const TimeSpan &timeSpan) noexcept
Changes the sign of the Timestamp.
Definition Time.h:333
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:312
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:305
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:319
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:684
std::uint64_t UInt64
uInt64.
Definition Integral.h:37
bool operator>(const TimeSpan &left, const TimeSpan &right) noexcept
Compares Timespans.
Definition Time.h:326
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:746
Messaging::Timestamp timestamp(const Messaging::FIX::MultiContainer &, Messaging::FIX::Tag)
Retrieves last update time field value.
The months in year.
Definition Time.h:378
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:400
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:410
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:416
@ YYYYMMDDHHMMSSpsec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssssssssss format.
Definition Time.h:419
@ YYYYMMDDHHMMSSusec
Indicates timestamp in YYYYMMDD-HH:MM:SS.ssssss format.
Definition Time.h:413