OnixS C++ ICE Binary Order Entry Handler 1.0.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
21#pragma once
22
26
27#include <stdexcept>
28
30
33{
35 constexpr
36 static Int64 nanosecondsPerDay() noexcept
37 {
38 return 86400000000000ll;
39 }
40
42 constexpr
43 static Int64 nanosecondsPerHour() noexcept
44 {
45 return 3600000000000ll;
46 }
47
49 constexpr
50 static Int64 nanosecondsPerMinute() noexcept
51 {
52 return 60000000000ll;
53 }
54
56 constexpr
57 static Int32 nanosecondsPerSecond() noexcept
58 {
59 return 1000000000;
60 }
61
63 constexpr
65 {
66 return 1000000;
67 }
68
70 constexpr
72 {
73 return 1000;
74 }
75
77 constexpr
78 static Int32 hoursPerDay() noexcept
79 {
80 return 24;
81 }
82
84 constexpr
85 static Int32 minutesPerHour() noexcept
86 {
87 return 60;
88 }
89
91 constexpr
92 static Int32 secondsPerMinute() noexcept
93 {
94 return 60;
95 }
96
98 constexpr
99 static Int32 millisecondsPerSecond() noexcept
100 {
101 return 1000;
102 }
103
105 constexpr
106 static Int32 microsecondsPerSecond() noexcept
107 {
108 return 1000000;
109 }
110};
111
142
147{
148public:
150 typedef Int64 Ticks;
151
153 typedef Int32 Days;
154
156 typedef Int32 Hours;
157
159 typedef Int32 Minutes;
160
162 typedef Int32 Seconds;
163
166
169
172
176
178 explicit constexpr TimeSpan(Ticks ticks = 0) noexcept
179 : ticks_(ticks)
180 {
181 }
182
191 : ticks_(
192 numericCast<Ticks>(days) *
194 numericCast<Ticks>(hours) *
196 numericCast<Ticks>(minutes) *
198 numericCast<Ticks>(seconds) *
201 {
202 }
203
212 : ticks_(
213 numericCast<Ticks>(hours) *
215 numericCast<Ticks>(minutes) *
217 numericCast<Ticks>(seconds) *
220 {
221 }
222
224 Days days() const noexcept
225 {
226 return
227 numericCast<Days>
228 (ticks_ /
230 }
231
235 Hours hours() const noexcept
236 {
237 return
238 numericCast<Hours>(
239 (ticks_ /
242 );
243 }
244
248 Minutes minutes() const noexcept
249 {
250 return
251 numericCast<Minutes>(
252 (ticks_ /
255 );
256 }
257
261 Seconds seconds() const noexcept
262 {
263 return
264 numericCast<Seconds>(
265 (ticks_ /
268 );
269 }
270
274 Milliseconds milliseconds() const noexcept
275 {
276 return
277 numericCast<Milliseconds>
278 ((ticks_ /
281 );
282 }
283
287 Microseconds microseconds() const noexcept
288 {
289 return
290 numericCast<Microseconds>
291 ((ticks_ /
294 );
295 }
296
300 Nanoseconds nanoseconds() const noexcept
301 {
302 return
303 numericCast<Nanoseconds>
304 (ticks_ %
306 }
307
312 Ticks ticks() const noexcept
313 {
314 return ticks_;
315 }
316
318 TimeSpan& operator+=(const TimeSpan& other) noexcept
319 {
320 ticks_ += other.ticks_;
321
322 return *this;
323 }
324
326 TimeSpan& operator-=(const TimeSpan& other) noexcept
327 {
328 ticks_ -= other.ticks_;
329
330 return *this;
331 }
332
334 void swap(TimeSpan & other) noexcept
335 {
336 std::swap(ticks_, other.ticks_);
337 }
338
342 static TimeSpan fromStr(const std::string&);
343 static TimeSpan fromStr(StrRef);
344
345private:
346 Ticks ticks_;
347};
348
351inline bool operator==(const TimeSpan& left, const TimeSpan& right) noexcept
352{
353 return left.ticks() == right.ticks();
354}
355
358inline bool operator!=(const TimeSpan& left, const TimeSpan& right) noexcept
359{
360 return left.ticks() != right.ticks();
361}
362
365inline bool operator<(const TimeSpan& left, const TimeSpan& right) noexcept
366{
367 return left.ticks() < right.ticks();
368}
369
372inline bool operator>(const TimeSpan& left, const TimeSpan& right) noexcept
373{
374 return left.ticks() > right.ticks();
375}
376
379inline TimeSpan operator - (const TimeSpan& timeSpan) noexcept
380{
381 return TimeSpan(-timeSpan.ticks());
382}
383
386void toStrAsHHMMSS(std::string&, TimeSpan);
387
390void toStrAsHHMMSSmsec(std::string&, TimeSpan);
391
394void toStrAsHHMMSSusec(std::string&, TimeSpan);
395
398void toStrAsHHMMSSnsec(std::string&, TimeSpan);
399
402void toStrAsHHMMSSpsec(std::string&, TimeSpan);
403
406void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
407
411
413inline
415{
416 std::string str;
417
418 toStr(str, timeSpan, format);
419
420 return str;
421}
422
442
467
470{
471public:
473 typedef UInt64 Ticks;
474
476 typedef UInt32 Year;
477
479 typedef ONIXS_ICEBOE_MESSAGING_NAMESPACE::Month::Enum Month;
480
482 typedef UInt32 Day;
483
485 typedef UInt32 Hour;
486
488 typedef UInt32 Minute;
489
491 typedef UInt32 Second;
492
495
498
501
505
507 explicit constexpr Timestamp(Ticks ticks = 0) noexcept
508 : sinceEpoch_(ticks)
509 {
510 }
511
516 Year year,
517 Month month,
518 Day day,
519 Hour hour = 0,
520 Minute minute = 0,
521 Second second = 0,
523 : sinceEpoch_(
524 toTicks(
525 year, month, day,
527 )
528 {
529 }
530
531 Timestamp(const Timestamp& other) noexcept
532 : sinceEpoch_(other.sinceEpoch_)
533 {
534 }
535
537 Year year() const
538 {
540
541 toDate(sinceEpoch_, year, month, day);
542
543 return year;
544 }
545
547 Month month() const
548 {
550
551 toDate(sinceEpoch_, year, month, day);
552
553 return month;
554 }
555
557 Day day() const
558 {
560
561 toDate(sinceEpoch_, year, month, day);
562
563 return day;
564 }
565
567 Hour hour() const noexcept
568 {
569 return static_cast<Hour>(time().hours());
570 }
571
573 Minute minute() const noexcept
574 {
575 return static_cast<Minute>(time().minutes());
576 }
577
579 Second second() const noexcept
580 {
581 return static_cast<Second>(time().seconds());
582 }
583
585 Millisecond millisecond() const noexcept
586 {
587 return static_cast<Millisecond>(time().milliseconds());
588 }
589
591 Microsecond microsecond() const noexcept
592 {
593 return static_cast<Microsecond>(time().microseconds());
594 }
595
597 Nanosecond nanosecond() const noexcept
598 {
599 return static_cast<Nanosecond>(time().nanoseconds());
600 }
601
603 Timestamp date() const noexcept
604 {
605 return
606 Timestamp(
607 sinceEpoch_ -
608 sinceEpoch_ %
610 }
611
613 TimeSpan time() const noexcept
614 {
615 return
616 TimeSpan(
617 sinceEpoch_ %
619 }
620
622 Ticks sinceEpoch() const noexcept
623 {
624 return sinceEpoch_;
625 }
626
627 Timestamp& operator=(const Timestamp& other) noexcept
628 {
629 sinceEpoch_ = other.sinceEpoch_;
630
631 return *this;
632 }
633
635 void swap(Timestamp & other) noexcept
636 {
637 std::swap(sinceEpoch_, other.sinceEpoch_);
638 }
639
645
646private:
648 Ticks sinceEpoch_;
649
652 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
653
654 // Extracts the ddate components from ticks.
656 static void toDate(Ticks, Year&, Month&, Day&);
657};
658
662{
663 return Timestamp(ticks);
664}
665
668inline bool operator==(const Timestamp& left, const Timestamp& right) noexcept
669{
670 return (
671 left.sinceEpoch() ==
672 right.sinceEpoch()
673 );
674}
675
678inline bool operator!=(const Timestamp& left, const Timestamp& right) noexcept
679{
680 return (
681 left.sinceEpoch() !=
682 right.sinceEpoch()
683 );
684}
685
688inline bool operator<(const Timestamp& left, const Timestamp& right) noexcept
689{
690 return (
691 left.sinceEpoch() <
692 right.sinceEpoch()
693 );
694}
695
698inline bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
699{
700 return (
701 left.sinceEpoch() <=
702 right.sinceEpoch()
703 );
704}
705
708inline bool operator>(const Timestamp& left, const Timestamp& right) noexcept
709{
710 return (
711 left.sinceEpoch() >
712 right.sinceEpoch()
713 );
714}
715
718inline bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
719{
720 return (
721 left.sinceEpoch() >=
722 right.sinceEpoch()
723 );
724}
725
728inline Timestamp operator +(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
729{
730 return
731 Timestamp(
732 timestamp.sinceEpoch() +
733 timeSpan.ticks()
734 );
735}
736
739inline Timestamp operator -(const Timestamp& timestamp, const TimeSpan& timeSpan) noexcept
740{
741 return
742 Timestamp(
743 timestamp.sinceEpoch() -
744 timeSpan.ticks()
745 );
746}
747
750inline TimeSpan operator -(const Timestamp& left, const Timestamp& right) noexcept
751{
752 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
753}
754
757void toStrAsYYYYMMDD(std::string&, Timestamp);
758
762);
763
767
771
775
779
783
787{
788 std::string str;
789
790 toStr(str, timestamp, format);
791
792 return str;
793}
794
797size_t toStr(Timestamp, Char*, size_t);
798
799inline std::ostream & operator <<(std::ostream & os, const Timestamp& value)
800{
801 return os << toStr(value);
802}
803
804inline
805std::ostream & operator <<(std::ostream & os, const TimeSpan& value)
806{
807 return os << toStr(value);
808}
809
813bool fromStr(TimeSpan&, const Char*, size_t);
814
817bool fromStr(TimeSpan& ts, StrRef str)
818{
819 return fromStr(ts, str.data(), str.size());
820}
821
823bool fromStr(TimeSpan& ts, const std::string& str)
824{
825 return fromStr(ts, str.data(), str.size());
826}
827
832
836{
837 return fromStr(ts, str.data(), str.size(), format);
838}
839
843{
844 return fromStr(ts, str.data(), str.size(), format);
845}
846
847inline
849{
850 return fromStr(StrRef{str}, format);
851}
852
854{
855 Timestamp ts;
856
857 const bool result = Messaging::fromStr(ts, str, format);
858
859 if(!result)
860 throw std::runtime_error("Error parsing Timestamp, invalid value: '" + toStr(str) + "'.");
861
862 return ts;
863}
864
865inline
866TimeSpan TimeSpan::fromStr(const std::string& str)
867{
868 return fromStr(StrRef{str});;
869}
870
871inline
873{
874 TimeSpan ts;
875
876 const bool result = Messaging::fromStr(ts, str);
877
878 if(!result)
879 throw std::runtime_error("Error parsing TimeSpan, invalid value: '" + toStr(str) + "'.");
880
881 return ts;
882}
883
884
885inline
887{
888 return toStr(*this, format);
889}
890
891inline
893{
894 return toStr(*this, format);
895}
896
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:102
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_END
Definition ABI.h:106
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:153
#define ONIXS_ICEBOE_PURE
Definition Compiler.h:157
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:154
Nanoseconds nanoseconds() const noexcept
Definition Time.h:300
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:165
static TimeSpan fromStr(const std::string &)
De-serializes the timespan from the given string according to the specified pattern.
Definition Time.h:866
void swap(TimeSpan &other) noexcept
Swaps.
Definition Time.h:334
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:159
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Initializes with the given set of values.
Definition Time.h:211
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Initializes with the given set of values.
Definition Time.h:190
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:171
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:162
Microseconds microseconds() const noexcept
Definition Time.h:287
Ticks ticks() const noexcept
Definition Time.h:312
Hours hours() const noexcept
Definition Time.h:235
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:168
constexpr TimeSpan(Ticks ticks=0) noexcept
Initializes the timespan from the given number of ticks.
Definition Time.h:178
TimeSpan & operator-=(const TimeSpan &other) noexcept
Subtracts the given time interval.
Definition Time.h:326
std::string toString(TimeSpanFormat::Enum=TimeSpanFormat::SDHHMMSSnsec) const
Definition Time.h:886
Int32 Hours
Integral type for number of hours.
Definition Time.h:156
TimeSpan & operator+=(const TimeSpan &other) noexcept
Adds the given time interval.
Definition Time.h:318
Int32 Days
Integral type for number of days.
Definition Time.h:153
Days days() const noexcept
Definition Time.h:224
Minutes minutes() const noexcept
Definition Time.h:248
Milliseconds milliseconds() const noexcept
Definition Time.h:274
Seconds seconds() const noexcept
Definition Time.h:261
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:150
The time point without the time-zone information.
Definition Time.h:470
static Timestamp fromStr(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec)
De-serializes a timestamp from the given string.
Definition Time.h:848
Microsecond microsecond() const noexcept
Definition Time.h:591
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:485
Minute minute() const noexcept
Definition Time.h:573
void swap(Timestamp &other) noexcept
Exchanges the value.
Definition Time.h:635
UInt32 Minute
Integral type presenting the minute component.
Definition Time.h:488
TimeSpan time() const noexcept
Definition Time.h:613
UInt32 Millisecond
Integral type presenting the millisecond component.
Definition Time.h:494
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:473
Timestamp date() const noexcept
Definition Time.h:603
Timestamp & operator=(const Timestamp &other) noexcept
Definition Time.h:627
UInt32 Microsecond
Integral type presenting the microsecond component.
Definition Time.h:497
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Explicit time-stamp initialization.
Definition Time.h:515
constexpr Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:507
UInt32 Second
Integral type presenting the second component.
Definition Time.h:491
UInt32 Day
Integral type presenting the day component.
Definition Time.h:482
Second second() const noexcept
Definition Time.h:579
UInt32 Nanosecond
Integral type presenting the nanosecond component.
Definition Time.h:500
OnixS::ICE::BOE::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:479
Hour hour() const noexcept
Definition Time.h:567
UInt32 Year
Integral type presenting the year component.
Definition Time.h:476
Ticks sinceEpoch() const noexcept
Definition Time.h:622
Millisecond millisecond() const noexcept
Definition Time.h:585
Nanosecond nanosecond() const noexcept
Definition Time.h:597
Timestamp(const Timestamp &other) noexcept
Definition Time.h:531
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec) const
Definition Time.h:892
void toStrAsHHMMSSpsec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.ssssssssssss pattern.
bool operator>=(const Decimal &left, const Decimal &right)
Timestamp makeTimestamp(Timestamp::Ticks ticks) noexcept
Make Timestamp helper.
Definition Time.h:661
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 operator>(const Decimal &left, const Decimal &right)
std::string format(Int32 index, StrRef input)
bool operator!=(const Decimal &left, const Decimal &right)
void toStrAsHHMMSSusec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.ssssss pattern.
void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSmsec format.
char Char
Character type alias.
Definition String.h:31
void toStrAsHHMMSSmsec(std::string &, TimeSpan)
Serializes the timespan according to the HH:MM:SS.sss pattern.
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
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.
void toStrAsYYYYMMDDHHMMSSpsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSpsec format.
std::basic_string_view< Char > StrRef
Definition StrRef.h:46
bool operator<=(const Decimal &left, const Decimal &right)
void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDD format.
bool operator<(const Decimal &left, const Decimal &right) noexcept
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
Serializes the timespan according to the D.HH:MM:SS.sssssssss pattern.
bool fromStr(Decimal &, const Char *, size_t) noexcept
Deserializes a decimal number from the given text presentation.
bool operator==(const Decimal &left, const Decimal &right) noexcept
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSnsec format.
The months in year.
Definition Time.h:425
Timespan formatting patterns.
Definition Time.h:114
@ HHMMSSnsec
Indicates a time span in the "HH:MM:SS.sssssssss" format.
Definition Time.h:131
@ HHMMSSmsec
Indicates a time span in the "HH:MM:SS.sss" format.
Definition Time.h:123
@ HHMMSSpsec
Indicates a time span in the "HH:MM:SS.ssssssssssss" format.
Definition Time.h:135
@ HHMMSSusec
Indicates a time span in the "HH:MM:SS.ssssss" format.
Definition Time.h:127
@ SDHHMMSSnsec
Indicates a time span in the "D.HH:MM:SS.sssssssss" format.
Definition Time.h:139
@ HHMMSS
Indicates a time span in the "HH:MM:SS" format.
Definition Time.h:119
Miscellaneous time characteristics.
Definition Time.h:33
static constexpr Int32 hoursPerDay() noexcept
Definition Time.h:78
static constexpr Int32 nanosecondsPerSecond() noexcept
Definition Time.h:57
static constexpr Int32 secondsPerMinute() noexcept
Definition Time.h:92
static constexpr Int32 minutesPerHour() noexcept
Definition Time.h:85
static constexpr Int64 nanosecondsPerDay() noexcept
Definition Time.h:36
static constexpr Int32 microsecondsPerSecond() noexcept
Definition Time.h:106
static constexpr Int32 millisecondsPerSecond() noexcept
Definition Time.h:99
static constexpr Int64 nanosecondsPerMinute() noexcept
Definition Time.h:50
static constexpr Int32 nanosecondsPerMicrosecond() noexcept
Definition Time.h:71
static constexpr Int32 nanosecondsPerMillisecond() noexcept
Definition Time.h:64
static constexpr Int64 nanosecondsPerHour() noexcept
Definition Time.h:43
Timestamp formatting patterns.
Definition Time.h:445
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:455
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:461
@ YYYYMMDDHHMMSSpsec
Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssssssssss" format.
Definition Time.h:464
@ YYYYMMDDHHMMSSusec
Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssss" format.
Definition Time.h:458