OnixS C++ B3 BOE Binary Order Entry 1.3.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
25
26#include <stdexcept>
27
29
32{
36 {
37 return 86400000000000ll;
38 }
39
43 {
44 return 3600000000000ll;
45 }
46
50 {
51 return 60000000000ll;
52 }
53
57 {
58 return 1000000000;
59 }
60
64 {
65 return 1000000;
66 }
67
71 {
72 return 1000;
73 }
74
78 {
79 return 24;
80 }
81
85 {
86 return 60;
87 }
88
92 {
93 return 60;
94 }
95
99 {
100 return 1000;
101 }
102
106 {
107 return 1000000;
108 }
109};
110
141
146{
147public:
149 typedef Int64 Ticks;
150
152 typedef Int32 Days;
153
155 typedef Int32 Hours;
156
158 typedef Int32 Minutes;
159
161 typedef Int32 Seconds;
162
164 typedef Int32 Milliseconds;
165
167 typedef Int32 Microseconds;
168
170 typedef Int32 Nanoseconds;
171
175
178 : ticks_(ticks)
179 {
180 }
181
190 : ticks_(
191 numericCast<Ticks>(days) *
193 numericCast<Ticks>(hours) *
195 numericCast<Ticks>(minutes) *
197 numericCast<Ticks>(seconds) *
200 {
201 }
202
211 : ticks_(
212 numericCast<Ticks>(hours) *
214 numericCast<Ticks>(minutes) *
216 numericCast<Ticks>(seconds) *
219 {
220 }
221
224 {
225 return
226 numericCast<Days>
227 (ticks_ /
229 }
230
235 {
236 return
237 numericCast<Hours>(
238 (ticks_ /
241 );
242 }
243
248 {
249 return
250 numericCast<Minutes>(
251 (ticks_ /
254 );
255 }
256
261 {
262 return
263 numericCast<Seconds>(
264 (ticks_ /
267 );
268 }
269
274 {
275 return
276 numericCast<Milliseconds>
277 ((ticks_ /
280 );
281 }
282
287 {
288 return
289 numericCast<Microseconds>
290 ((ticks_ /
293 );
294 }
295
300 {
301 return
302 numericCast<Nanoseconds>
303 (ticks_ %
305 }
306
312 {
313 return ticks_;
314 }
315
318 {
319 ticks_ += other.ticks_;
320
321 return *this;
322 }
323
326 {
327 ticks_ -= other.ticks_;
328
329 return *this;
330 }
331
334 {
335 std::swap(ticks_, other.ticks_);
336 }
337
341 static TimeSpan fromStr(const std::string&);
342
343private:
344 Ticks ticks_;
345};
346
349inline bool operator==(const TimeSpan& left, const TimeSpan& right) ONIXS_B3_BOE_NOTHROW
350{
351 return left.ticks() == right.ticks();
352}
353
356inline bool operator!=(const TimeSpan& left, const TimeSpan& right) ONIXS_B3_BOE_NOTHROW
357{
358 return left.ticks() != right.ticks();
359}
360
363inline bool operator<(const TimeSpan& left, const TimeSpan& right) ONIXS_B3_BOE_NOTHROW
364{
365 return left.ticks() < right.ticks();
366}
367
370inline bool operator>(const TimeSpan& left, const TimeSpan& right) ONIXS_B3_BOE_NOTHROW
371{
372 return left.ticks() > right.ticks();
373}
374
377inline TimeSpan operator - (const TimeSpan& timeSpan) ONIXS_B3_BOE_NOTHROW
378{
379 return TimeSpan(-timeSpan.ticks());
380}
381
384void toStrAsHHMMSS(std::string&, TimeSpan);
385
388void toStrAsHHMMSSmsec(std::string&, TimeSpan);
389
392void toStrAsHHMMSSusec(std::string&, TimeSpan);
393
396void toStrAsHHMMSSnsec(std::string&, TimeSpan);
397
400void toStrAsHHMMSSpsec(std::string&, TimeSpan);
401
404void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
405
409
411inline
413{
414 std::string str;
415
416 toStr(str, timeSpan, format);
417
418 return str;
419}
420
440
465
468{
469public:
471 typedef UInt64 Ticks;
472
474 typedef UInt32 Year;
475
477 typedef
480
482 typedef UInt32 Day;
483
485 typedef UInt32 Hour;
486
488 typedef UInt32 Minute;
489
491 typedef UInt32 Second;
492
494 typedef UInt32 Millisecond;
495
497 typedef UInt32 Microsecond;
498
500 typedef UInt32 Nanosecond;
501
505
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
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
568 {
569 return static_cast<Hour>(time().hours());
570 }
571
574 {
575 return static_cast<Minute>(time().minutes());
576 }
577
580 {
581 return static_cast<Second>(time().seconds());
582 }
583
586 {
587 return static_cast<Millisecond>(time().milliseconds());
588 }
589
592 {
593 return static_cast<Microsecond>(time().microseconds());
594 }
595
598 {
599 return static_cast<Nanosecond>(time().nanoseconds());
600 }
601
604 {
605 return
606 Timestamp(
607 sinceEpoch_ -
608 sinceEpoch_ %
610 }
611
614 {
615 return
616 TimeSpan(
617 sinceEpoch_ %
619 }
620
623 {
624 return sinceEpoch_;
625 }
626
628 {
629 sinceEpoch_ = other.sinceEpoch_;
630
631 return *this;
632 }
633
636 {
637 std::swap(sinceEpoch_, other.sinceEpoch_);
638 }
639
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
661{
662 return Timestamp(ticks);
663}
664
667inline bool operator==(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
668{
669 return (
670 left.sinceEpoch() ==
671 right.sinceEpoch()
672 );
673}
674
677inline bool operator!=(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
678{
679 return (
680 left.sinceEpoch() !=
681 right.sinceEpoch()
682 );
683}
684
687inline bool operator<(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
688{
689 return (
690 left.sinceEpoch() <
691 right.sinceEpoch()
692 );
693}
694
697inline bool operator<=(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
698{
699 return (
700 left.sinceEpoch() <=
701 right.sinceEpoch()
702 );
703}
704
707inline bool operator>(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
708{
709 return (
710 left.sinceEpoch() >
711 right.sinceEpoch()
712 );
713}
714
717inline bool operator>=(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
718{
719 return (
720 left.sinceEpoch() >=
721 right.sinceEpoch()
722 );
723}
724
727inline Timestamp operator +(const Timestamp& timestamp, const TimeSpan& timeSpan) ONIXS_B3_BOE_NOTHROW
728{
729 return
730 Timestamp(
731 timestamp.sinceEpoch() +
732 timeSpan.ticks()
733 );
734}
735
738inline Timestamp operator -(const Timestamp& timestamp, const TimeSpan& timeSpan) ONIXS_B3_BOE_NOTHROW
739{
740 return
741 Timestamp(
742 timestamp.sinceEpoch() -
743 timeSpan.ticks()
744 );
745}
746
749inline TimeSpan operator -(const Timestamp& left, const Timestamp& right) ONIXS_B3_BOE_NOTHROW
750{
751 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
752}
753
756void toStrAsYYYYMMDD(std::string&, Timestamp);
757
761);
762
766
770
774
778
782
786{
787 std::string str;
788
789 toStr(str, timestamp, format);
790
791 return str;
792}
793
796size_t toStr(Timestamp, Char*, size_t);
797
798inline std::ostream & operator <<(std::ostream & os, const Timestamp& value)
799{
800 return os << toStr(value);
801}
802
803inline
804std::ostream & operator <<(std::ostream & os, const TimeSpan& value)
805{
806 return os << toStr(value);
807}
808
812bool fromStr(TimeSpan&, const Char*, size_t);
813
816bool fromStr(TimeSpan& ts, const std::string& str)
817{
818 return
819 fromStr(
820 ts, str.c_str(), str.size());
821}
822
827
831{
832 return
833 fromStr(
834 ts, str.c_str(), str.size(), format);
835}
836
837inline
839{
840 Timestamp ts;
841
842 const bool result =
843 Messaging::fromStr(ts, str, format);
844
845 if(!result)
846 throw std::runtime_error("Error parsing timestamp.");
847
848 return ts;
849}
850
851inline
852TimeSpan TimeSpan::fromStr(const std::string& str)
853{
854 TimeSpan ts;
855
856 const bool result =
857 Messaging::fromStr(ts, str);
858
859 if(!result)
860 throw std::runtime_error("Error parsing timespan.");
861
862 return ts;
863}
864
865inline
867{
868 return toStr(*this, format);
869}
870
871inline
873{
874 return toStr(*this, format);
875}
876
#define ONIXS_B3_BOE_LTWT_STRUCT
Definition ABI.h:88
#define ONIXS_B3_BOE_LTWT_EXPORTED
Definition ABI.h:92
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_B3_BOE_LTWT_CLASS
Definition ABI.h:84
#define ONIXS_B3_BOE_CONSTEXPR
Definition Compiler.h:185
#define ONIXS_B3_BOE_NODISCARD
Definition Compiler.h:191
#define ONIXS_B3_BOE_EXPORTED
Definition Compiler.h:181
#define ONIXS_B3_BOE_PURE
Definition Compiler.h:195
#define ONIXS_B3_BOE_NOTHROW
Definition Compiler.h:182
Nanoseconds nanoseconds() const noexcept
Definition Time.h:299
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:164
static TimeSpan fromStr(const std::string &)
De-serializes the timespan from the given string according to the specified pattern.
Definition Time.h:852
void swap(TimeSpan &other) noexcept
Swaps.
Definition Time.h:333
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:158
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Initializes with the given set of values.
Definition Time.h:210
TimeSpan(Ticks ticks=0) noexcept
Initializes the timespan from the given number of ticks.
Definition Time.h:177
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Initializes with the given set of values.
Definition Time.h:189
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:170
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:161
Microseconds microseconds() const noexcept
Definition Time.h:286
Ticks ticks() const noexcept
Definition Time.h:311
Hours hours() const noexcept
Definition Time.h:234
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:167
TimeSpan & operator-=(const TimeSpan &other) noexcept
Subtracts the given time interval.
Definition Time.h:325
std::string toString(TimeSpanFormat::Enum=TimeSpanFormat::SDHHMMSSnsec) const
Definition Time.h:866
Int32 Hours
Integral type for number of hours.
Definition Time.h:155
TimeSpan & operator+=(const TimeSpan &other) noexcept
Adds the given time interval.
Definition Time.h:317
Int32 Days
Integral type for number of days.
Definition Time.h:152
Days days() const noexcept
Definition Time.h:223
Minutes minutes() const noexcept
Definition Time.h:247
Milliseconds milliseconds() const noexcept
Definition Time.h:273
Seconds seconds() const noexcept
Definition Time.h:260
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:149
The time point without the time-zone information.
Definition Time.h:468
static Timestamp fromStr(const std::string &, TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec)
De-serializes a timestamp from the given string.
Definition Time.h:838
Microsecond microsecond() const noexcept
Definition Time.h:591
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:485
Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:507
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
OnixS::B3::BOE::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:479
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:471
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
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
Hour hour() const noexcept
Definition Time.h:567
UInt32 Year
Integral type presenting the year component.
Definition Time.h:474
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:872
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:660
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.
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:30
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.
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.
bool fromStr(Decimal &, const Char *, size_t) noexcept
Deserializes a decimal number from the given text presentation.
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSnsec format.
The months in year.
Definition Time.h:423
Timespan formatting patterns.
Definition Time.h:113
@ HHMMSSnsec
Indicates a time span in the "HH:MM:SS.sssssssss" format.
Definition Time.h:130
@ HHMMSSmsec
Indicates a time span in the "HH:MM:SS.sss" format.
Definition Time.h:122
@ HHMMSSpsec
Indicates a time span in the "HH:MM:SS.ssssssssssss" format.
Definition Time.h:134
@ HHMMSSusec
Indicates a time span in the "HH:MM:SS.ssssss" format.
Definition Time.h:126
@ SDHHMMSSnsec
Indicates a time span in the "D.HH:MM:SS.sssssssss" format.
Definition Time.h:138
@ HHMMSS
Indicates a time span in the "HH:MM:SS" format.
Definition Time.h:118
Miscellaneous time characteristics.
Definition Time.h:32
static constexpr Int32 hoursPerDay() noexcept
Definition Time.h:77
static constexpr Int32 nanosecondsPerSecond() noexcept
Definition Time.h:56
static constexpr Int32 secondsPerMinute() noexcept
Definition Time.h:91
static constexpr Int32 minutesPerHour() noexcept
Definition Time.h:84
static constexpr Int64 nanosecondsPerDay() noexcept
Definition Time.h:35
static constexpr Int32 microsecondsPerSecond() noexcept
Definition Time.h:105
static constexpr Int32 millisecondsPerSecond() noexcept
Definition Time.h:98
static constexpr Int64 nanosecondsPerMinute() noexcept
Definition Time.h:49
static constexpr Int32 nanosecondsPerMicrosecond() noexcept
Definition Time.h:70
static constexpr Int32 nanosecondsPerMillisecond() noexcept
Definition Time.h:63
static constexpr Int64 nanosecondsPerHour() noexcept
Definition Time.h:42
Timestamp formatting patterns.
Definition Time.h:443
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:453
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:459
@ YYYYMMDDHHMMSSpsec
Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssssssssss" format.
Definition Time.h:462
@ YYYYMMDDHHMMSSusec
Indicates timestamp in "YYYYMMDD-HH:MM:SS.ssssss" format.
Definition Time.h:456