OnixS C++ CME MDP Conflated TCP Handler 1.3.6
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
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
165
168
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_CONFLATEDTCP_NOTHROW
350{
351 return left.ticks() == right.ticks();
352}
353
356inline bool operator!=(const TimeSpan& left, const TimeSpan& right) ONIXS_CONFLATEDTCP_NOTHROW
357{
358 return left.ticks() != right.ticks();
359}
360
363inline bool operator<(const TimeSpan& left, const TimeSpan& right) ONIXS_CONFLATEDTCP_NOTHROW
364{
365 return left.ticks() < right.ticks();
366}
367
370inline bool operator>(const TimeSpan& left, const TimeSpan& right) ONIXS_CONFLATEDTCP_NOTHROW
371{
372 return left.ticks() > right.ticks();
373}
374
377inline TimeSpan operator - (const TimeSpan& timeSpan) ONIXS_CONFLATEDTCP_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:
470
472 typedef UInt64 Ticks;
473
476 struct MemberTraits
477 {
478 enum
479 {
480 Count = 1
481 };
482
483 typedef Ticks FirstArgType;
484 };
485
487 enum
488 {
490 Size = sizeof(Ticks)
491 };
492
494 typedef UInt32 Year;
495
497 typedef
500
502 typedef UInt32 Day;
503
505 typedef UInt32 Hour;
506
508 typedef UInt32 Minute;
509
511 typedef UInt32 Second;
512
515
518
521
525
528 : sinceEpoch_(ticks)
529 {
530 }
531
536 Year year,
537 Month month,
538 Day day,
539 Hour hour = 0,
540 Minute minute = 0,
541 Second second = 0,
543 : sinceEpoch_(
544 toTicks(
545 year, month, day,
547 )
548 {
549 }
550
552 : sinceEpoch_(other.sinceEpoch_)
553 {
554 }
555
557 Year year() const
558 {
560
561 toDate(sinceEpoch_, year, month, day);
562
563 return year;
564 }
565
567 Month month() const
568 {
570
571 toDate(sinceEpoch_, year, month, day);
572
573 return month;
574 }
575
577 Day day() const
578 {
580
581 toDate(sinceEpoch_, year, month, day);
582
583 return day;
584 }
585
588 {
589 return static_cast<Hour>(time().hours());
590 }
591
594 {
595 return static_cast<Minute>(time().minutes());
596 }
597
600 {
601 return static_cast<Second>(time().seconds());
602 }
603
606 {
607 return static_cast<Millisecond>(time().milliseconds());
608 }
609
612 {
613 return static_cast<Microsecond>(time().microseconds());
614 }
615
618 {
619 return static_cast<Nanosecond>(time().nanoseconds());
620 }
621
624 {
625 return
626 Timestamp(
627 sinceEpoch_ -
628 sinceEpoch_ %
630 }
631
634 {
635 return
636 TimeSpan(
637 sinceEpoch_ %
639 }
640
643 {
644 return sinceEpoch_;
645 }
646
648 {
649 sinceEpoch_ = other.sinceEpoch_;
650
651 return *this;
652 }
653
656 {
657 std::swap(sinceEpoch_, other.sinceEpoch_);
658 }
659
664
665private:
667 Ticks sinceEpoch_;
668
671 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
672
673 // Extracts the ddate components from ticks.
675 static void toDate(Ticks, Year&, Month&, Day&);
676};
677
684
687inline bool operator==(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
688{
689 return (
690 left.sinceEpoch() ==
691 right.sinceEpoch()
692 );
693}
694
697inline bool operator!=(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
698{
699 return (
700 left.sinceEpoch() !=
701 right.sinceEpoch()
702 );
703}
704
707inline bool operator<(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
708{
709 return (
710 left.sinceEpoch() <
711 right.sinceEpoch()
712 );
713}
714
717inline bool operator<=(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
718{
719 return (
720 left.sinceEpoch() <=
721 right.sinceEpoch()
722 );
723}
724
727inline bool operator>(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
728{
729 return (
730 left.sinceEpoch() >
731 right.sinceEpoch()
732 );
733}
734
737inline bool operator>=(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
738{
739 return (
740 left.sinceEpoch() >=
741 right.sinceEpoch()
742 );
743}
744
747inline Timestamp operator +(const Timestamp& timestamp, const TimeSpan& timeSpan) ONIXS_CONFLATEDTCP_NOTHROW
748{
749 return
750 Timestamp(
751 timestamp.sinceEpoch() +
752 timeSpan.ticks()
753 );
754}
755
758inline Timestamp operator -(const Timestamp& timestamp, const TimeSpan& timeSpan) ONIXS_CONFLATEDTCP_NOTHROW
759{
760 return
761 Timestamp(
762 timestamp.sinceEpoch() -
763 timeSpan.ticks()
764 );
765}
766
769inline TimeSpan operator -(const Timestamp& left, const Timestamp& right) ONIXS_CONFLATEDTCP_NOTHROW
770{
771 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
772}
773
776void toStrAsYYYYMMDD(std::string&, Timestamp);
777
781);
782
786
790
794
798
802
806{
807 std::string str;
808
809 toStr(str, timestamp, format);
810
811 return str;
812}
813
816size_t toStr(Timestamp, Char*, size_t);
817
818inline std::ostream & operator <<(std::ostream & os, const Timestamp& value)
819{
820 return os << toStr(value);
821}
822
823inline
824std::ostream & operator <<(std::ostream & os, const TimeSpan& value)
825{
826 return os << toStr(value);
827}
828
832bool fromStr(TimeSpan&, const Char*, size_t);
833
836bool fromStr(TimeSpan& ts, const std::string& str)
837{
838 return
839 fromStr(
840 ts, str.c_str(), str.size());
841}
842
847
851{
852 return
853 fromStr(
854 ts, str.c_str(), str.size(), format);
855}
856
857inline
859{
860 Timestamp ts;
861
862 const bool result =
863 Messaging::fromStr(ts, str, format);
864
865 if(!result)
866 throw std::runtime_error("Error parsing timestamp.");
867
868 return ts;
869}
870
871inline
872TimeSpan TimeSpan::fromStr(const std::string& str)
873{
874 TimeSpan ts;
875
876 const bool result =
877 Messaging::fromStr(ts, str);
878
879 if(!result)
880 throw std::runtime_error("Error parsing timespan.");
881
882 return ts;
883}
884
885inline
887{
888 return toStr(*this, format);
889}
890
891inline
893{
894 return toStr(*this, format);
895}
896
#define ONIXS_CONFLATEDTCP_LTWT_EXPORTED
Definition ABI.h:92
#define ONIXS_CONFLATEDTCP_LTWT_STRUCT
Definition ABI.h:88
#define ONIXS_CONFLATEDTCP_LTWT_CLASS
Definition ABI.h:84
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_END
Definition ABI.h:143
#define ONIXS_CONFLATEDTCP_PURE
Definition Compiler.h:189
#define ONIXS_CONFLATEDTCP_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_CONFLATEDTCP_EXPORTED
Definition Compiler.h:175
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition Compiler.h:176
#define ONIXS_CONFLATEDTCP_NODISCARD
Definition Compiler.h:185
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:872
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
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:886
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
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:858
OnixS::CME::ConflatedTCP::Messaging::Month::Enum Month
Type presenting the month component.
Definition Time.h:499
Microsecond microsecond() const noexcept
Definition Time.h:611
UInt32 Hour
Integral type presenting the hour component.
Definition Time.h:505
Timestamp(Ticks ticks=0) noexcept
Initializes from the number of ticks since epoch.
Definition Time.h:527
void swap(Timestamp &other) noexcept
Exchanges the value.
Definition Time.h:655
UInt32 Minute
Integral type presenting the minute component.
Definition Time.h:508
UInt32 Millisecond
Integral type presenting the millisecond component.
Definition Time.h:514
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:472
Timestamp date() const noexcept
Definition Time.h:623
Timestamp & operator=(const Timestamp &other) noexcept
Definition Time.h:647
UInt32 Microsecond
Integral type presenting the microsecond component.
Definition Time.h:517
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:535
@ Size
Size of the class in bytes.
Definition Time.h:490
UInt32 Second
Integral type presenting the second component.
Definition Time.h:511
UInt32 Day
Integral type presenting the day component.
Definition Time.h:502
UInt32 Nanosecond
Integral type presenting the nanosecond component.
Definition Time.h:520
UInt32 Year
Integral type presenting the year component.
Definition Time.h:494
Millisecond millisecond() const noexcept
Definition Time.h:605
Nanosecond nanosecond() const noexcept
Definition Time.h:617
Timestamp(const Timestamp &other) noexcept
Definition Time.h:551
std::string toString(TimestampFormat::Enum=TimestampFormat::YYYYMMDDHHMMSSnsec) const
Definition Time.h:892
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:737
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:680
bool fromStr(Int8 &, const Char *, size_t) noexcept
Deserializes a numeric value from its text representation.
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compares instances.
Definition Time.h:717
bool operator>(const StrRef &left, const StrRef &right)
Compares instances.
Definition StrRef.h:310
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 StrRef &left, const StrRef &right)
Compares instances.
Definition StrRef.h:290
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.
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.
bool operator==(const StrRef &left, const StrRef &right)
Compares instances.
Definition StrRef.h:261
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
Serializes the timespan according to the D.HH:MM:SS.sssssssss pattern.
void toStr(std::string &str, const Negotiate200 &obj)
Serializes into a string.
bool operator!=(const StrRef &left, const StrRef &right)
Compares instances.
Definition StrRef.h:282
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes the timestamp using the YYYYMMDDHHMMSSnsec format.
@ 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
@ 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