OnixS C++ Eurex T7 Market and Reference Data (EMDI, MDI, RDI, EOBI) Handlers 19.0.0
Users' manual and API documentation
Loading...
Searching...
No Matches
Timestamp.h
Go to the documentation of this file.
1/*
2* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3*
4* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5* and international copyright treaties.
6*
7* Access to and use of the software is governed by the terms of the applicable OnixS Software
8* Services Agreement (the Agreement) and Customer end user license agreements granting
9* a non-assignable, non-transferable and non-exclusive license to use the software
10* for it's own data processing purposes under the terms defined in the Agreement.
11*
12* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13* of this source code or associated reference material to any other location for further reproduction
14* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15*
16* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17* the terms of the Agreement is a violation of copyright law.
18*/
19#pragma once
20
21#include <string>
22#include <ostream>
23
27
28namespace OnixS
29{
30 namespace Eurex
31 {
32 namespace MarketData
33 {
36 {
38 constexpr
39 static Int64 nanosecondsPerDay()
40 {
41 return 86400000000000ll;
42 }
43
45 constexpr
46 static Int64 nanosecondsPerHour()
47 {
48 return 3600000000000ll;
49 }
50
52 constexpr
53 static Int64 nanosecondsPerMinute()
54 {
55 return 60000000000ll;
56 }
57
59 constexpr
61 {
62 return 1000000000;
63 }
64
66 constexpr
68 {
69 return 1000000;
70 }
71
73 constexpr
75 {
76 return 1000;
77 }
78
80 constexpr
82 {
83 return 24;
84 }
85
87 constexpr
89 {
90 return 60;
91 }
92
94 constexpr
96 {
97 return 60;
98 }
99
101 constexpr
103 {
104 return 1000;
105 }
106
108 constexpr
110 {
111 return 1000000;
112 }
113 };
114
118 {
119 public:
121 typedef Int64 Ticks;
122
124 typedef Int32 Days;
125
127 typedef Int32 Hours;
128
130 typedef Int32 Minutes;
131
133 typedef Int32 Seconds;
134
137
140
143
145 explicit TimeSpan(Ticks ticks = 0) noexcept
146 : ticks_(ticks)
147 {
148 }
149
158 : ticks_(
159 static_cast<Ticks>(days) * TimeTraits::nanosecondsPerDay() +
160 static_cast<Ticks>(hours) * TimeTraits::nanosecondsPerHour() +
164 {
165 }
166
175 : ticks_(
176 static_cast<Ticks>(hours) * TimeTraits::nanosecondsPerHour() +
180 {
181 }
182
185 Days days() const
186 {
187 return
188 static_cast<Days>
189 (ticks_ /
191 }
192
195 Hours hours() const
196 {
197 return
198 static_cast<Hours>(
199 (ticks_ /
202 );
203 }
204
208 {
209 return
210 static_cast<Minutes>(
211 (ticks_ /
214 );
215 }
216
220 {
221 return
222 static_cast<Seconds>(
223 (ticks_ /
226 );
227 }
228
232 {
233 return
234 static_cast<Milliseconds>
235 ((ticks_ /
238 );
239 }
240
247
251 {
252 return static_cast<Nanoseconds>(ticks_ % TimeTraits::nanosecondsPerSecond());
253 }
254
260 Ticks ticks() const
261 {
262 return ticks_;
263 }
264
266 TimeSpan&
268 const TimeSpan& other)
269 {
270 ticks_ += other.ticks_;
271 return *this;
272 }
273
275 TimeSpan&
277 const TimeSpan& other)
278 {
279 ticks_ -= other.ticks_;
280 return *this;
281 }
282
284 void
286 noexcept
287 {
288 std::swap(ticks_, other.ticks_);
289 }
290
291 private:
292 Ticks ticks_;
293 };
294
296 inline
297 bool
299 const TimeSpan& left,
300 const TimeSpan& right)
301 {
302 return left.ticks() == right.ticks();
303 }
304
306 inline
307 bool
309 const TimeSpan& left,
310 const TimeSpan& right)
311 {
312 return left.ticks() != right.ticks();
313 }
314
316 inline
317 bool
319 const TimeSpan& left,
320 const TimeSpan& right)
321 {
322 return left.ticks() < right.ticks();
323 }
324
326 inline
327 bool
329 const TimeSpan& left,
330 const TimeSpan& right)
331 {
332 return left.ticks() > right.ticks();
333 }
334
335 // TimeSpan serialization.
336
337 // Serializes timespan according to HH:MM:SS pattern.
338 ONIXS_EUREX_EMDI_API
339 void toStrAsHHMMSS(std::string&, TimeSpan);
340
341 // Serializes timespan according to HH:MM:SS.sss pattern.
342 ONIXS_EUREX_EMDI_API
343 void toStrAsHHMMSSmsec(std::string&, TimeSpan);
344
345 // Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
346 ONIXS_EUREX_EMDI_API
347 void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
348
351 {
352 enum Enum
353 {
356
359
362 };
363 };
364
365
367 inline
368 void toStr(
369 std::string& str,
370 TimeSpan timeSpan,
371 TimeSpanFormat::Enum format =
373 {
374 switch (format)
375 {
377 toStrAsHHMMSS(str, timeSpan);
378 break;
379
381 toStrAsHHMMSSmsec(str, timeSpan);
382 break;
383
385 toStrAsSDHHMMSSnsec(str, timeSpan);
386 break;
387
388 default:
389 {
390 throw std::invalid_argument(
391 "Unknown timespan format pattern specified. ");
392 }
393 }
394 }
395
397 inline
398 std::string toStr(
399 TimeSpan timeSpan,
400 TimeSpanFormat::Enum format =
402 {
403 std::string str;
404
405 toStr(str, timeSpan, format);
406
407 return str;
408 }
409
429
448
451 {
452 public:
454 typedef UInt64 Ticks;
455
457 typedef UInt32 Year;
458
461
463 typedef UInt32 Day;
464
466 typedef UInt32 Hour;
467
469 typedef UInt32 Minute;
470
472 typedef UInt32 Second;
473
476
479
482
484 ONIXS_EUREX_EMDI_API
486
488 ONIXS_EUREX_EMDI_API
489 static Timestamp now();
490
492 explicit
493 Timestamp(Ticks ticks = 0) noexcept
494 : sinceEpoch_(ticks)
495 {
496 }
497
504 : sinceEpoch_(toTicks(year, month, day,hour, minute, second, nanosecond))
505 {
506 }
507
509 Timestamp(const Timestamp& other) noexcept
510 : sinceEpoch_(other.sinceEpoch_)
511 {
512 }
513
515 Year year() const
516 {
518
519 toDate(sinceEpoch_, year, month, day);
520
521 return year;
522 }
523
525 Month month() const
526 {
528
529 toDate(sinceEpoch_, year, month, day);
530
531 return month;
532 }
533
535 Day day() const
536 {
538
539 toDate(sinceEpoch_, year, month, day);
540
541 return day;
542 }
543
545 Hour hour() const noexcept
546 {
547 return static_cast<Hour>(time().hours());
548 }
549
551 Minute minute() const noexcept
552 {
553 return static_cast<Minute>(time().minutes());
554 }
555
557 Second second() const noexcept
558 {
559 return static_cast<Second>(time().seconds());
560 }
561
563 Millisecond millisecond() const noexcept
564 {
565 return static_cast<Millisecond>(time().milliseconds());
566 }
567
570 {
571 return static_cast<Microsecond>(time().microseconds());
572 }
573
575 Nanosecond nanosecond() const noexcept
576 {
577 return static_cast<Nanosecond>(time().nanoseconds());
578 }
579
581 Timestamp date() const noexcept
582 {
583 return Timestamp(sinceEpoch_ - sinceEpoch_ % TimeTraits::nanosecondsPerDay());
584 }
585
587 TimeSpan time() const noexcept
588 {
589 return TimeSpan(sinceEpoch_ % TimeTraits::nanosecondsPerDay());
590 }
591
593 Ticks sinceEpoch() const noexcept
594 {
595 return sinceEpoch_;
596 }
597
599 Timestamp&
601 const Timestamp& other)
602 {
603 sinceEpoch_ = other.sinceEpoch_;
604
605 return *this;
606 }
607
609 void
611 Timestamp& other)
612 {
613 std::swap(
614 sinceEpoch_,
615 other.sinceEpoch_);
616 }
617
620 static
621 ONIXS_EUREX_EMDI_API
622 Timestamp parse (unsigned long long presentation,
624
627
628 private:
629 // Time interval in nanoseconds since the 01-01-1970.
630 Ticks sinceEpoch_;
631
632 // Converts structured date-time into ticks.
633 ONIXS_EUREX_EMDI_API
634 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second,Nanosecond);
635
636 // Extracts date components from ticks.
637 ONIXS_EUREX_EMDI_API
638 static void toDate(Ticks, Year&, Month&, Day&);
639 };
640
642 inline
643 Timestamp fromSeconds(UInt32 secondsSinceEpoch)
644 {
645 return Timestamp(
646 static_cast<Timestamp::Ticks>(
647 secondsSinceEpoch) * TimeTraits::nanosecondsPerSecond());
648 }
649
651 inline
652 bool operator ==(const Timestamp& left, const Timestamp& right)
653 {
654 return (
655 left.sinceEpoch() ==
656 right.sinceEpoch()
657 );
658 }
659
661 inline
662 bool
663 operator !=(const Timestamp& left, const Timestamp& right)
664 {
665 return (
666 left.sinceEpoch() !=
667 right.sinceEpoch()
668 );
669 }
670
672 inline
673 bool
675 const Timestamp& left,
676 const Timestamp& right)
677 {
678 return (
679 left.sinceEpoch() <
680 right.sinceEpoch()
681 );
682 }
683
685 inline
686 bool
688 const Timestamp& left,
689 const Timestamp& right)
690 {
691 return (
692 left.sinceEpoch() <=
693 right.sinceEpoch()
694 );
695 }
696
698 inline
699 bool
701 const Timestamp& left,
702 const Timestamp& right)
703 {
704 return (
705 left.sinceEpoch() >
706 right.sinceEpoch()
707 );
708 }
709
711 inline
712 bool
714 const Timestamp& left,
715 const Timestamp& right)
716 {
717 return (
718 left.sinceEpoch() >=
719 right.sinceEpoch()
720 );
721 }
722
724 inline
725 Timestamp
727 const Timestamp& timestamp,
728 const TimeSpan& timeSpan)
729 {
730 return
731 Timestamp(
732 timestamp.sinceEpoch() +
733 timeSpan.ticks()
734 );
735 }
736
738 inline
739 Timestamp
741 const Timestamp& timestamp,
742 const TimeSpan& timeSpan)
743 {
744 return
745 Timestamp(
746 timestamp.sinceEpoch() -
747 timeSpan.ticks()
748 );
749 }
750
752 inline
753 TimeSpan
755 const Timestamp& left,
756 const Timestamp& right)
757 {
758 return
759 TimeSpan(
760 left.sinceEpoch() -
761 right.sinceEpoch()
762 );
763 }
764
765 // Serialization.
766
768 ONIXS_EUREX_EMDI_API
770 (
771 std::string&,
773 );
774
776 ONIXS_EUREX_EMDI_API
777 void
779 (
780 std::string&,
782 );
783
785 ONIXS_EUREX_EMDI_API
786 void
788 (
789 std::string&,
791 );
792
794 ONIXS_EUREX_EMDI_API
795 void
797 (
798 std::string&,
800 );
801
803 ONIXS_EUREX_EMDI_API
804 void
806 std::string& str,
807 Timestamp timestamp,
808 TimestampFormat::Enum format =
810
813 {
814 std::string str;
815
816 toStr(str, timestamp, format);
817
818 return str;
819 }
820
822 ONIXS_EUREX_EMDI_API
823 bool fromStr(Timestamp&, const char*, size_t);
824
825 inline bool fromStr(Timestamp& ts, const std::string& str)
826 {
827 return fromStr(ts, str.c_str(), str.size());
828 }
829
831 {
832 return toStr(*this, format);
833 }
834
835 inline std::ostream& operator<<(std::ostream& o, Timestamp ts)
836 {
837 o << ts.toString();
838 return o;
839 }
840 }
841 }
842}
Milliseconds milliseconds() const
Definition Timestamp.h:231
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Timestamp.h:136
void swap(TimeSpan &other) noexcept
Exchanges with given instance.
Definition Timestamp.h:285
Int32 Minutes
Integral type for number of minutes.
Definition Timestamp.h:130
TimeSpan(Ticks ticks=0) noexcept
Initializes timespan from given number of ticks.
Definition Timestamp.h:145
Nanoseconds nanoseconds() const
Definition Timestamp.h:250
constexpr TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Definition Timestamp.h:157
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Timestamp.h:142
Int32 Seconds
Integral type for number of seconds.
Definition Timestamp.h:133
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition Timestamp.h:267
Microseconds microseconds() const
Definition Timestamp.h:243
Int32 Microseconds
Integral type for number of microseconds.
Definition Timestamp.h:139
constexpr TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds) noexcept
Definition Timestamp.h:174
Int32 Hours
Integral type for number of hours.
Definition Timestamp.h:127
Int32 Days
Integral type for number of days.
Definition Timestamp.h:124
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition Timestamp.h:276
Int64 Ticks
Integral type presenting internal ticks.
Definition Timestamp.h:121
Represents time point without time-zone information.
Definition Timestamp.h:451
Microsecond microsecond() const
Microsecond component of given time point.
Definition Timestamp.h:569
Month month() const
Month component of given time point.
Definition Timestamp.h:525
Year year() const
Year component of given time point.
Definition Timestamp.h:515
Day day() const
Day component of given time point.
Definition Timestamp.h:535
static Timestamp now()
Returns current local time.
UInt32 Hour
Integral type presenting hour component.
Definition Timestamp.h:466
Timestamp(Ticks ticks=0) noexcept
Initializes from number of ticks since epoch.
Definition Timestamp.h:493
Minute minute() const noexcept
Minute component of given time point.
Definition Timestamp.h:551
UInt32 Minute
Integral type presenting minute component.
Definition Timestamp.h:469
TimeSpan time() const noexcept
Time part of timestamp.
Definition Timestamp.h:587
static Timestamp parse(unsigned long long presentation, TimestampFormat::Enum format=TimestampFormat::YYYYMMDD)
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Timestamp.h:475
std::string toString(TimestampFormat::Enum format=TimestampFormat::YYYYMMDDHHMMSSNsec)
Serializes timestamp according to specified pattern.
Definition Timestamp.h:830
static Timestamp utcNow()
Returns current UTC time.
UInt64 Ticks
Integral type storing internal ticks.
Definition Timestamp.h:454
Timestamp date() const noexcept
Timestamp without a time part.
Definition Timestamp.h:581
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Timestamp.h:478
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Timestamp.h:503
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Timestamp.h:610
UInt32 Second
Integral type presenting second component.
Definition Timestamp.h:472
UInt32 Day
Integral type presenting day component.
Definition Timestamp.h:463
Second second() const noexcept
Second component of given time point.
Definition Timestamp.h:557
Timestamp & operator=(const Timestamp &other)
Reinitializes as copy of given instance.
Definition Timestamp.h:600
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Timestamp.h:481
Hour hour() const noexcept
Hour component of given time point.
Definition Timestamp.h:545
UInt32 Year
Integral type presenting year component.
Definition Timestamp.h:457
Ticks sinceEpoch() const noexcept
Number of nanoseconds since the Epoch (01-01-1970).
Definition Timestamp.h:593
Millisecond millisecond() const noexcept
Millisecond component of given time point.
Definition Timestamp.h:563
Nanosecond nanosecond() const noexcept
Nanosecond component of given time point.
Definition Timestamp.h:575
Timestamp(const Timestamp &other) noexcept
Initializes as copy of other instance.
Definition Timestamp.h:509
OnixS::Eurex::MarketData::Month::Enum Month
Type presenting month component.
Definition Timestamp.h:460
std::ostream & operator<<(std::ostream &os, const Message &message)
void toStr(std::string &str, TimeSpan timeSpan, TimeSpanFormat::Enum format=TimeSpanFormat::SDHHMMSSnsec)
Appends timespan formatted in specified pattern to given string.
Definition Timestamp.h:368
Timestamp fromSeconds(UInt32 secondsSinceEpoch)
Create Timestamp from whole seconds since the Epoch.
Definition Timestamp.h:643
unsigned int UInt32
Definition Numeric.h:41
bool operator>=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Timestamp.h:713
bool operator>(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval greater than right one.
Definition Timestamp.h:328
void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
void toStrAsHHMMSSmsec(std::string &, TimeSpan)
bool operator==(const FieldValueRef &ref, const std::string &str)
bool fromStr(Timestamp &, const char *, size_t)
De-serializes a timestamp from the given string.
void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
void toStrAsHHMMSS(std::string &, TimeSpan)
bool operator<=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Timestamp.h:687
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition Timestamp.h:726
void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
bool operator<(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval less than right one.
Definition Timestamp.h:318
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition Timestamp.h:740
bool operator!=(const FieldValueRef &ref, const std::string &str)
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
Identifies months in year.
Definition Timestamp.h:412
Collection of timespan formatting patterns.
Definition Timestamp.h:351
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Timestamp.h:361
Miscellaneous time characteristics.
Definition Timestamp.h:36
static constexpr Int32 secondsPerMinute()
Returns number of seconds in single minute.
Definition Timestamp.h:95
static constexpr Int32 microsecondsPerSecond()
Returns number of microseconds in single second.
Definition Timestamp.h:109
static constexpr Int32 millisecondsPerSecond()
Returns number of milliseconds in single second.
Definition Timestamp.h:102
static constexpr Int64 nanosecondsPerMinute()
Returns number of nanoseconds in single minute.
Definition Timestamp.h:53
static constexpr Int32 hoursPerDay()
Returns number of hours in single day.
Definition Timestamp.h:81
static constexpr Int64 nanosecondsPerHour()
Returns number of nanoseconds in single hour.
Definition Timestamp.h:46
static constexpr Int32 nanosecondsPerMicrosecond()
Returns number of nanoseconds in single microsecond.
Definition Timestamp.h:74
static constexpr Int64 nanosecondsPerDay()
Returns number of nanoseconds in single day.
Definition Timestamp.h:39
static constexpr Int32 minutesPerHour()
Returns number of minutes in single hour.
Definition Timestamp.h:88
static constexpr Int32 nanosecondsPerSecond()
Returns number of nanoseconds in single second.
Definition Timestamp.h:60
static constexpr Int32 nanosecondsPerMillisecond()
Returns number of nanoseconds in single millisecond.
Definition Timestamp.h:67
Collection of timestamp formatting patterns.
Definition Timestamp.h:432
@ YYYYMMDDHHMMSSMsec
YYYYMMDD-HH:MM:SS.sss.
Definition Timestamp.h:442
@ YYYYMMDDHHMMSSNsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Timestamp.h:445