OnixS C++ CME MDP Streamlined Market Data Handler 1.2.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
23#include <algorithm>
24#include <stdexcept>
25
28
30ONIXS_CMESTREAMLINEDMDH_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 TimeSpan(Ticks ticks = 0)
133 : ticks_(ticks)
134 {
135 }
136
145 Days days,
146 Hours hours,
150 : ticks_(
151 static_cast<Ticks>(days) *
152 TimeTraits::nanosecondsPerDay() +
153 static_cast<Ticks>(hours) *
154 TimeTraits::nanosecondsPerHour() +
155 static_cast<Ticks>(minutes) *
156 TimeTraits::nanosecondsPerMinute() +
157 static_cast<Ticks>(seconds) *
158 TimeTraits::nanosecondsPerSecond() +
160 {
161 }
162
171 Hours hours,
175 : ticks_(
176 static_cast<Ticks>(hours) *
177 TimeTraits::nanosecondsPerHour() +
178 static_cast<Ticks>(minutes) *
179 TimeTraits::nanosecondsPerMinute() +
180 static_cast<Ticks>(seconds) *
181 TimeTraits::nanosecondsPerSecond() +
183 {
184 }
185
188 Days days() const
189 {
190 return
191 static_cast<Days>
192 (ticks_ /
194 }
195
198 Hours hours() const
199 {
200 return
201 static_cast<Hours>(
202 (ticks_ /
205 );
206 }
207
211 {
212 return
213 static_cast<Minutes>(
214 (ticks_ /
217 );
218 }
219
223 {
224 return
225 static_cast<Seconds>(
226 (ticks_ /
229 );
230 }
231
235 {
236 return
237 static_cast<Milliseconds>
238 ((ticks_ /
241 );
242 }
243
247 {
248 return
249 static_cast<Microseconds>
250 ((ticks_ /
253 );
254 }
255
259 {
260 return
261 static_cast<Nanoseconds>
262 (ticks_ %
264 }
265
271 Ticks ticks() const
272 {
273 return ticks_;
274 }
275
277 TimeSpan&
278 operator +=(
279 const TimeSpan& other)
280 {
281 ticks_ += other.ticks_;
282
283 return *this;
284 }
285
287 TimeSpan&
288 operator -=(
289 const TimeSpan& other)
290 {
291 ticks_ -= other.ticks_;
292
293 return *this;
294 }
295
297 void
299 {
300 std::swap(ticks_, other.ticks_);
301 }
302
303private:
304 Ticks ticks_;
305};
306
308inline
309bool
310operator ==(
311 const TimeSpan& left,
312 const TimeSpan& right)
313{
314 return left.ticks() == right.ticks();
315}
316
318inline
319bool
320operator !=(
321 const TimeSpan& left,
322 const TimeSpan& right)
323{
324 return left.ticks() != right.ticks();
325}
326
328inline
329bool
330operator <(
331 const TimeSpan& left,
332 const TimeSpan& right)
333{
334 return left.ticks() < right.ticks();
335}
336
338inline
339bool
340operator >(
341 const TimeSpan& left,
342 const TimeSpan& right)
343{
344 return left.ticks() > right.ticks();
345}
346
347// TimeSpan serialization.
348
349// Serializes timespan according to HH:MM:SS pattern.
351void
352toStrAsHHMMSS(std::string&, TimeSpan);
353
354// Serializes timespan according to HH:MM:SS.sss pattern.
356void
358
359// Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
361void
363
379
381inline
382void
384 std::string& str,
385 TimeSpan timeSpan,
386 TimeSpanFormat::Enum format =
388{
389 switch (format)
390 {
392 toStrAsHHMMSS(str, timeSpan);
393 break;
394
396 toStrAsHHMMSSmsec(str, timeSpan);
397 break;
398
400 toStrAsSDHHMMSSnsec(str, timeSpan);
401 break;
402
403 default:
404 {
405 throw std::invalid_argument(
406 "Unknown timespan format pattern specified. ");
407 }
408 }
409}
410
412inline
413std::string
415 TimeSpan timeSpan,
416 TimeSpanFormat::Enum format =
418{
419 std::string str;
420
421 toStr(str, timeSpan, format);
422
423 return str;
424}
425
445
448{
449public:
451 typedef UInt64 Ticks;
452
454 typedef UInt32 Year;
455
457 typedef
460
462 typedef UInt32 Day;
463
465 typedef UInt32 Hour;
466
468 typedef UInt32 Minute;
469
471 typedef UInt32 Second;
472
475
478
481
483 explicit Timestamp(Ticks ticks = 0)
484 : sinceEpoch_(ticks)
485 {
486 }
487
494 Year year,
495 Month month,
496 Day day,
497 Hour hour = 0,
498 Minute minute = 0,
499 Second second = 0,
501 : sinceEpoch_(
502 toTicks(
503 year, month, day,
505 )
506 {
507 }
508
511 const Timestamp& other)
512 : sinceEpoch_(other.sinceEpoch_)
513 {
514 }
515
517 Year year() const
518 {
520
521 toDate(sinceEpoch_, year, month, day);
522
523 return year;
524 }
525
527 Month month() const
528 {
530
531 toDate(sinceEpoch_, year, month, day);
532
533 return month;
534 }
535
537 Day day() const
538 {
540
541 toDate(sinceEpoch_, year, month, day);
542
543 return day;
544 }
545
547 Hour hour() const
548 {
549 return static_cast<Hour>(time().hours());
550 }
551
554 {
555 return static_cast<Minute>(time().minutes());
556 }
557
560 {
561 return static_cast<Second>(time().seconds());
562 }
563
566 {
567 return static_cast<Millisecond>(time().milliseconds());
568 }
569
572 {
573 return static_cast<Microsecond>(time().microseconds());
574 }
575
578 {
579 return static_cast<Nanosecond>(time().nanoseconds());
580 }
581
584 {
585 return
586 Timestamp(
587 sinceEpoch_ -
588 sinceEpoch_ %
590 }
591
594 {
595 return
596 TimeSpan(
597 sinceEpoch_ %
599 }
600
603 {
604 return sinceEpoch_;
605 }
606
608 Timestamp&
609 operator =(
610 const Timestamp& other)
611 {
612 sinceEpoch_ = other.sinceEpoch_;
613
614 return *this;
615 }
616
618 void
620 Timestamp& other)
621 {
622 std::swap(
623 sinceEpoch_,
624 other.sinceEpoch_);
625 }
626
627private:
628 // Time interval in nanoseconds since the 01-01-1970.
629 Ticks sinceEpoch_;
630
631 // Converts structured date-time into ticks.
633 static
634 Ticks
635 toTicks(
636 Year, Month, Day,
637 Hour, Minute, Second,
638 Nanosecond);
639
640 // Extracts date components from ticks.
642 static
643 void
644 toDate(
645 Ticks,
646 Year&, Month&, Day&);
647};
648
650inline
651bool
652operator ==(
653 const Timestamp& left,
654 const Timestamp& right)
655{
656 return left.sinceEpoch() == right.sinceEpoch();
657}
658
660inline
661bool
662operator !=(
663 const Timestamp& left,
664 const Timestamp& right)
665{
666 return left.sinceEpoch() != right.sinceEpoch();
667}
668
670template
671<
672 UInt64 Value
673>
674inline
675bool
676operator !=(
677 const Timestamp& left,
679{
680 return left.sinceEpoch() != right.value();
681}
682
683template
684<
685 UInt64 Value
686>
687inline
688bool
689operator !=(
691 const Timestamp& right)
692{
693 return left.value() != right.sinceEpoch();
694}
695
697inline
698bool
699operator <(
700 const Timestamp& left,
701 const Timestamp& right)
702{
703 return left.sinceEpoch() < right.sinceEpoch();
704}
705
707inline
708bool
709operator >(
710 const Timestamp& left,
711 const Timestamp& right)
712{
713 return left.sinceEpoch() > right.sinceEpoch();
714}
715
717inline
718Timestamp
719operator +(
720 const Timestamp& timestamp,
721 const TimeSpan& timeSpan)
722{
723 return
724 Timestamp(
725 timestamp.sinceEpoch() +
726 timeSpan.ticks()
727 );
728}
729
731inline
732Timestamp
733operator -(
734 const Timestamp& timestamp,
735 const TimeSpan& timeSpan)
736{
737 return
738 Timestamp(
739 timestamp.sinceEpoch() -
740 timeSpan.ticks()
741 );
742}
743
745inline
746TimeSpan
747operator -(
748 const Timestamp& left,
749 const Timestamp& right)
750{
751 return
752 TimeSpan(
753 left.sinceEpoch() -
754 right.sinceEpoch()
755 );
756}
757
758// Serialization.
759
762void
764
767void
769
772void
774
777void
779
798
800inline
801void
803 std::string& str,
804 Timestamp timestamp,
805 TimestampFormat::Enum format =
807{
808 switch (format)
809 {
811 toStrAsYYYYMMDD(str, timestamp);
812 break;
813
815 toStrAsYYYYMMDDHHMMSS(str, timestamp);
816 break;
817
819 toStrAsYYYYMMDDHHMMSSmsec(str, timestamp);
820 break;
821
823 toStrAsYYYYMMDDHHMMSSnsec(str, timestamp);
824 break;
825
826 default:
827 {
828 throw std::invalid_argument(
829 "Unknown timestamp format pattern specified. ");
830 }
831 }
832}
833
835inline
836std::string
838 Timestamp timestamp,
839 TimestampFormat::Enum format =
841{
842 std::string str;
843
844 toStr(str, timestamp, format);
845
846 return str;
847}
848
851bool
853 Timestamp&,
854 const Char*,
855 size_t);
856
859{
861 virtual const Char* id() const = 0;
862
864 virtual Timestamp now() = 0;
865};
866
869{
870public:
872 static Timestamp now();
873
876
877private:
878 LocalWatch(const LocalWatch&);
879 LocalWatch& operator =(const LocalWatch&);
880};
881
882// UTC watch.
884{
885public:
887 static Timestamp now();
888
891
892private:
893 UtcWatch(const UtcWatch&);
894 UtcWatch& operator =(const UtcWatch&);
895};
896
897ONIXS_CMESTREAMLINEDMDH_DATA_PACKING_END
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED_STRUCT
Definition Bootstrap.h:67
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED_CLASS
Definition Bootstrap.h:63
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:169
#define ONIXS_CMESTREAMLINEDMDH_LTWT_STRUCT
Definition Bootstrap.h:115
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_LTWT_EXPORTED
Definition Bootstrap.h:119
#define ONIXS_CMESTREAMLINEDMDH_LTWT_CLASS
Definition Bootstrap.h:111
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED
Definition Compiler.h:160
static Timestamp now()
Returns current local time.
static WatchService & service()
Returns watch service implementing local watch.
Represents time interval.
Definition Time.h:105
Milliseconds milliseconds() const
Milliseconds component of time interval.
Definition Time.h:234
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Initializes with given set of values.
Definition Time.h:144
TimeSpan(Ticks ticks=0)
Initializes timespan from given number of ticks.
Definition Time.h:132
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:123
Ticks ticks() const
Number of ticks in given time interval.
Definition Time.h:271
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:117
Int32 seconds() const
Seconds component of time interval.
Definition Time.h:222
void swap(TimeSpan &other)
Exchanges with given instance.
Definition Time.h:298
Nanoseconds nanoseconds() const
Nanoseconds component of time interval.
Definition Time.h:258
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
Microseconds component of time interval.
Definition Time.h:246
Days days() const
Days component of time interval.
Definition Time.h:188
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
Hours hours() const
Hours component of time interval.
Definition Time.h:198
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:108
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Initializes with given set of values.
Definition Time.h:170
Int32 minutes() const
Minutes component of time interval.
Definition Time.h:210
Represents time point without time-zone information.
Definition Time.h:448
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:571
Ticks sinceEpoch() const
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:602
Month month() const
Month component of given time point.
Definition Time.h:527
Year year() const
Year component of given time point.
Definition Time.h:517
Timestamp(const Timestamp &other)
Initializes as copy of other instance.
Definition Time.h:510
Day day() const
Day component of given time point.
Definition Time.h:537
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:465
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:468
Minute minute() const
Minute component of given time point.
Definition Time.h:553
Hour hour() const
Hour component of given time point.
Definition Time.h:547
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:474
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:577
Timestamp date() const
Timestamp without a time part.
Definition Time.h:583
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:451
TimeSpan time() const
Time part of timestamp.
Definition Time.h:593
Timestamp(Ticks ticks=0)
Initializes from number of ticks since epoch.
Definition Time.h:483
Second second() const
Second component of given time point.
Definition Time.h:559
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:477
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Explicit timestamp initialization.
Definition Time.h:493
OnixS::CME::Streamlined::MDH::Month::Enum Month
Type presenting month component.
Definition Time.h:459
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:619
UInt32 Second
Integral type presenting second component.
Definition Time.h:471
UInt32 Day
Integral type presenting day component.
Definition Time.h:462
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:480
UInt32 Year
Integral type presenting year component.
Definition Time.h:454
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:565
static Timestamp now()
Returns current UTC time.
static WatchService & service()
Returns watch service implementing UTC watch.
void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
char Char
Character type alias.
Definition String.h:36
void toStrAsHHMMSSmsec(std::string &, TimeSpan)
void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
void toStrAsHHMMSS(std::string &, TimeSpan)
void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
void toStr(std::string &str, const Decimal &number)
Definition Decimal.h:502
bool fromStr(Decimal &, const char *, size_t)
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
static const Value value()
Returns value of the constant.
Definition Integral.h:106
Identifies months in year.
Definition Time.h:428
Collection of timespan formatting patterns.
Definition Time.h:366
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Time.h:376
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:782
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:792
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:795
virtual const Char * id() const =0
Identifies watch service.
virtual Timestamp now()=0
Returns current time.