OnixS C++ CME MDP Conflated UDP Handler 1.1.2
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
23#include <algorithm>
24#include <stdexcept>
25
28
30ONIXS_CONFLATEDUDP_DATA_PACKING_BEGIN(1)
31
32
34{
36 static Int64 nanosecondsPerDay()
37 {
38 return 86400000000000ll;
39 }
40
42 static Int64 nanosecondsPerHour()
43 {
44 return 3600000000000ll;
45 }
46
48 static Int64 nanosecondsPerMinute()
49 {
50 return 60000000000ll;
51 }
52
55 {
56 return 1000000000;
57 }
58
61 {
62 return 1000000;
63 }
64
67 {
68 return 1000;
69 }
70
73 {
74 return 24;
75 }
76
79 {
80 return 60;
81 }
82
85 {
86 return 60;
87 }
88
91 {
92 return 1000;
93 }
94
97 {
98 return 1000000;
99 }
100};
101
105{
106public:
108 typedef Int64 Ticks;
109
111 typedef Int32 Days;
112
114 typedef Int32 Hours;
115
117 typedef Int32 Minutes;
118
120 typedef Int32 Seconds;
121
124
127
130
132 explicit
134 Ticks ticks = 0)
135 : ticks_(ticks)
136 {
137 }
138
147 Days days,
148 Hours hours,
152 : ticks_(
153 static_cast<Ticks>(days) *
154 TimeTraits::nanosecondsPerDay() +
155 static_cast<Ticks>(hours) *
156 TimeTraits::nanosecondsPerHour() +
157 static_cast<Ticks>(minutes) *
158 TimeTraits::nanosecondsPerMinute() +
159 static_cast<Ticks>(seconds) *
160 TimeTraits::nanosecondsPerSecond() +
162 {
163 }
164
173 Hours hours,
177 : ticks_(
178 static_cast<Ticks>(hours) *
179 TimeTraits::nanosecondsPerHour() +
180 static_cast<Ticks>(minutes) *
181 TimeTraits::nanosecondsPerMinute() +
182 static_cast<Ticks>(seconds) *
183 TimeTraits::nanosecondsPerSecond() +
185 {
186 }
187
190 const TimeSpan& other)
191 : ticks_(other.ticks_)
192 {
193 }
194
197 Days days() const
198 {
199 return
200 static_cast<Days>
201 (ticks_ /
203 }
204
207 Hours hours() const
208 {
209 return
210 static_cast<Hours>(
211 (ticks_ /
214 );
215 }
216
220 {
221 return
222 static_cast<Minutes>(
223 (ticks_ /
226 );
227 }
228
232 {
233 return
234 static_cast<Seconds>(
235 (ticks_ /
238 );
239 }
240
244 {
245 return
246 static_cast<Milliseconds>
247 ((ticks_ /
250 );
251 }
252
256 {
257 return
258 static_cast<Microseconds>
259 ((ticks_ /
262 );
263 }
264
268 {
269 return
270 static_cast<Nanoseconds>
271 (ticks_ %
273 }
274
280 Ticks ticks() const
281 {
282 return ticks_;
283 }
284
286 TimeSpan&
287 operator +=(
288 const TimeSpan& other)
289 {
290 ticks_ += other.ticks_;
291
292 return *this;
293 }
294
296 TimeSpan&
297 operator -=(
298 const TimeSpan& other)
299 {
300 ticks_ -= other.ticks_;
301
302 return *this;
303 }
304
306 TimeSpan&
307 operator =(
308 const TimeSpan& other)
309 {
310 ticks_ = other.ticks_;
311
312 return *this;
313 }
314
316 void
318 {
319 std::swap(ticks_, other.ticks_);
320 }
321
322private:
323 Ticks ticks_;
324};
325
327inline
328bool
329operator ==(
330 const TimeSpan& left,
331 const TimeSpan& right)
332{
333 return left.ticks() == right.ticks();
334}
335
337inline
338bool
339operator !=(
340 const TimeSpan& left,
341 const TimeSpan& right)
342{
343 return left.ticks() != right.ticks();
344}
345
347inline
348bool
349operator <(
350 const TimeSpan& left,
351 const TimeSpan& right)
352{
353 return left.ticks() < right.ticks();
354}
355
357inline
358bool
359operator >(
360 const TimeSpan& left,
361 const TimeSpan& right)
362{
363 return left.ticks() > right.ticks();
364}
365
366// TimeSpan serialization.
367
368// Serializes timespan according to HH:MM:SS pattern.
369ONIXS_CONFLATEDUDP_EXPORTED
370void
371toStrAsHHMMSS(std::string&, TimeSpan);
372
373// Serializes timespan according to HH:MM:SS.sss pattern.
374ONIXS_CONFLATEDUDP_EXPORTED
375void
377
378// Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
379ONIXS_CONFLATEDUDP_EXPORTED
380void
382
398
400inline
401void
403 std::string& str,
404 TimeSpan timeSpan,
405 TimeSpanFormat::Enum format =
407{
408 switch (format)
409 {
411 toStrAsHHMMSS(str, timeSpan);
412 break;
413
415 toStrAsHHMMSSmsec(str, timeSpan);
416 break;
417
419 toStrAsSDHHMMSSnsec(str, timeSpan);
420 break;
421
422 default:
423 {
424 throw std::invalid_argument(
425 "Unknown timespan format pattern specified. ");
426 }
427 }
428}
429
431inline
432std::string
434 TimeSpan timeSpan,
435 TimeSpanFormat::Enum format =
437{
438 std::string str;
439
440 toStr(str, timeSpan, format);
441
442 return str;
443}
444
469
472{
473public:
475 typedef UInt64 Ticks;
476
478 typedef UInt32 Year;
479
481 typedef
484
486 typedef UInt32 Day;
487
489 typedef UInt32 Hour;
490
492 typedef UInt32 Minute;
493
495 typedef UInt32 Second;
496
499
502
505
507 explicit
509 Ticks ticks = 0)
510 : sinceEpoch_(ticks)
511 {
512 }
513
520 Year year,
521 Month month,
522 Day day,
523 Hour hour = 0,
524 Minute minute = 0,
525 Second second = 0,
527 : sinceEpoch_(
528 toTicks(
529 year, month, day,
531 )
532 {
533 }
534
536 Year year() const
537 {
539
540 toDate(sinceEpoch_, year, month, day);
541
542 return year;
543 }
544
546 Month month() const
547 {
549
550 toDate(sinceEpoch_, year, month, day);
551
552 return month;
553 }
554
556 Day day() const
557 {
559
560 toDate(sinceEpoch_, year, month, day);
561
562 return day;
563 }
564
566 Hour hour() const
567 {
568 return static_cast<Hour>(time().hours());
569 }
570
573 {
574 return static_cast<Minute>(time().minutes());
575 }
576
579 {
580 return static_cast<Second>(time().seconds());
581 }
582
585 {
586 return static_cast<Millisecond>(time().milliseconds());
587 }
588
591 {
592 return static_cast<Microsecond>(time().microseconds());
593 }
594
597 {
598 return static_cast<Nanosecond>(time().nanoseconds());
599 }
600
603 {
604 return
605 Timestamp(
606 sinceEpoch_ -
607 sinceEpoch_ %
609 }
610
613 {
614 return
615 TimeSpan(
616 sinceEpoch_ %
618 }
619
622 {
623 return sinceEpoch_;
624 }
625
627 void
629 Timestamp& other)
630 {
631 std::swap(
632 sinceEpoch_,
633 other.sinceEpoch_);
634 }
635
636private:
637 // Time interval in nanoseconds since the 01-01-1970.
638 Ticks sinceEpoch_;
639
640 // Converts structured date-time into ticks.
642 static
643 Ticks
644 toTicks(
645 Year, Month, Day,
646 Hour, Minute, Second,
647 Nanosecond);
648
649 // Extracts date components from ticks.
651 static
652 void
653 toDate(
654 Ticks,
655 Year&, Month&, Day&);
656};
657
659inline
660bool
661operator ==(
662 const Timestamp& left,
663 const Timestamp& right)
664{
665 return (
666 left.sinceEpoch() ==
667 right.sinceEpoch()
668 );
669}
670
672inline
673bool
674operator !=(
675 const Timestamp& left,
676 const Timestamp& right)
677{
678 return (
679 left.sinceEpoch() !=
680 right.sinceEpoch()
681 );
682}
683
685inline
686bool
687operator <(
688 const Timestamp& left,
689 const Timestamp& right)
690{
691 return (
692 left.sinceEpoch() <
693 right.sinceEpoch()
694 );
695}
696
698inline
699bool
700operator <=(
701 const Timestamp& left,
702 const Timestamp& right)
703{
704 return (
705 left.sinceEpoch() <=
706 right.sinceEpoch()
707 );
708}
709
711inline
712bool
713operator >(
714 const Timestamp& left,
715 const Timestamp& right)
716{
717 return (
718 left.sinceEpoch() >
719 right.sinceEpoch()
720 );
721}
722
724inline
725bool
726operator >=(
727 const Timestamp& left,
728 const Timestamp& right)
729{
730 return (
731 left.sinceEpoch() >=
732 right.sinceEpoch()
733 );
734}
735
737inline
738Timestamp
739operator +(
740 const Timestamp& timestamp,
741 const TimeSpan& timeSpan)
742{
743 return
744 Timestamp(
745 timestamp.sinceEpoch() +
746 timeSpan.ticks()
747 );
748}
749
751inline
752Timestamp
753operator -(
754 const Timestamp& timestamp,
755 const TimeSpan& timeSpan)
756{
757 return
758 Timestamp(
759 timestamp.sinceEpoch() -
760 timeSpan.ticks()
761 );
762}
763
765inline
766TimeSpan
767operator -(
768 const Timestamp& left,
769 const Timestamp& right)
770{
771 return
772 TimeSpan(
773 left.sinceEpoch() -
774 right.sinceEpoch()
775 );
776}
777
778// Serialization.
779
781ONIXS_CONFLATEDUDP_EXPORTED
782void
784(
785 std::string&,
787);
788
790ONIXS_CONFLATEDUDP_EXPORTED
791void
793(
794 std::string&,
796);
797
799ONIXS_CONFLATEDUDP_EXPORTED
800void
802(
803 std::string&,
805);
806
808ONIXS_CONFLATEDUDP_EXPORTED
809void
811(
812 std::string&,
814);
815
834
836inline
837void
839 std::string& str,
840 Timestamp timestamp,
841 TimestampFormat::Enum format =
843{
844 switch (format)
845 {
847 toStrAsYYYYMMDD(str, timestamp);
848 break;
849
851 toStrAsYYYYMMDDHHMMSS(str, timestamp);
852 break;
853
855 toStrAsYYYYMMDDHHMMSSmsec(str, timestamp);
856 break;
857
859 toStrAsYYYYMMDDHHMMSSnsec(str, timestamp);
860 break;
861
862 default:
863 {
864 throw std::invalid_argument
865 (
866 "Unknown timestamp format pattern specified. "
867 );
868 }
869 }
870}
871
873inline
874std::string
876 Timestamp timestamp,
877 TimestampFormat::Enum format =
879{
880 std::string str;
881
882 toStr(str, timestamp, format);
883
884 return str;
885}
886
888ONIXS_CONFLATEDUDP_EXPORTED
889bool
891 Timestamp&,
892 const Char*,
893 size_t);
894
895inline
896bool
898 Timestamp& ts,
899 const std::string& str)
900{
901 return
902 fromStr(
903 ts, str.c_str(), str.size());
904}
905
906
907ONIXS_CONFLATEDUDP_DATA_PACKING_END
#define ONIXS_CONFLATEDUDP_LTWT_EXPORTED
Definition Bootstrap.h:103
#define ONIXS_CONFLATEDUDP_LTWT_STRUCT
Definition Bootstrap.h:99
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition Bootstrap.h:95
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition Bootstrap.h:157
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition Bootstrap.h:153
Milliseconds milliseconds() const
Definition Time.h:243
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:146
TimeSpan(Ticks ticks=0)
Initializes timespan from given number of ticks.
Definition Time.h:133
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:123
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:117
TimeSpan(const TimeSpan &other)
Initializes instance as a copy of the other one.
Definition Time.h:189
void swap(TimeSpan &other)
Exchanges with given instance.
Definition Time.h:317
Nanoseconds nanoseconds() const
Definition Time.h:267
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:129
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:120
Microseconds microseconds() const
Definition Time.h:255
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:126
Int32 Hours
Integral type for number of hours.
Definition Time.h:114
Int32 Days
Integral type for number of days.
Definition Time.h:111
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:108
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:172
Represents time point without time-zone information.
Definition Time.h:472
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:590
Ticks sinceEpoch() const
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:621
Month month() const
Month component of given time point.
Definition Time.h:546
Year year() const
Year component of given time point.
Definition Time.h:536
Day day() const
Day component of given time point.
Definition Time.h:556
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:489
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:492
Minute minute() const
Minute component of given time point.
Definition Time.h:572
Hour hour() const
Hour component of given time point.
Definition Time.h:566
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:498
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:596
Timestamp date() const
Timestamp without a time part.
Definition Time.h:602
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:475
TimeSpan time() const
Time part of timestamp.
Definition Time.h:612
Timestamp(Ticks ticks=0)
Initializes from number of ticks since epoch.
Definition Time.h:508
Second second() const
Second component of given time point.
Definition Time.h:578
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:501
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:519
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:628
OnixS::CME::ConflatedUDP::Month::Enum Month
Type presenting month component.
Definition Time.h:483
UInt32 Second
Integral type presenting second component.
Definition Time.h:495
UInt32 Day
Integral type presenting day component.
Definition Time.h:486
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:504
UInt32 Year
Integral type presenting year component.
Definition Time.h:478
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:584
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsHHMMSSmsec(std::string &, TimeSpan)
ONIXS_CONFLATEDUDP_EXPORTED bool fromStr(Decimal &, const Char *, size_t)
UInt64 UInt64
uInt64.
Definition Fields.h:265
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
ONIXS_CONFLATEDUDP_EXPORTED void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
Int32 Int32
int32.
Definition Fields.h:69
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
char Char
Character type alias.
Definition String.h:36
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsHHMMSS(std::string &, TimeSpan)
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
UInt32 UInt32
uInt32.
Definition Fields.h:261
ONIXS_CONFLATEDUDP_EXPORTED void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
Identifies months in year.
Definition Time.h:447
Enum
Identifies months in year.
Definition Time.h:454
Collection of timespan formatting patterns.
Definition Time.h:385
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Time.h:395
Miscellaneous time characteristics.
Definition Time.h:34
static Int32 millisecondsPerSecond()
Returns number of milliseconds in single second.
Definition Time.h:90
static Int64 nanosecondsPerDay()
Returns number of nanoseconds in single day.
Definition Time.h:36
static Int32 minutesPerHour()
Returns number of minutes in single hour.
Definition Time.h:78
static Int32 hoursPerDay()
Returns number of hours in single day.
Definition Time.h:72
static Int64 nanosecondsPerMinute()
Returns number of nanoseconds in single minute.
Definition Time.h:48
static Int32 secondsPerMinute()
Returns number of seconds in single minute.
Definition Time.h:84
static Int32 microsecondsPerSecond()
Returns number of microseconds in single second.
Definition Time.h:96
static Int32 nanosecondsPerMillisecond()
Returns number of nanoseconds in single millisecond.
Definition Time.h:60
static Int32 nanosecondsPerSecond()
Returns number of nanoseconds in single second.
Definition Time.h:54
static Int32 nanosecondsPerMicrosecond()
Returns number of nanoseconds in single microsecond.
Definition Time.h:66
static Int64 nanosecondsPerHour()
Returns number of nanoseconds in single hour.
Definition Time.h:42
Collection of timestamp formatting patterns.
Definition Time.h:818
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:828
@ YYYYMMDDHHMMSS
YYYYMMDD-HH:MM:SS.
Definition Time.h:825
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:831