OnixS C++ Euronext Optiq MDG Handler 1.3.3
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#pragma once
21
22#include <string>
23#include <algorithm>
24#include <stdexcept>
25
29
30
31namespace OnixS
32{
33 namespace Euronext
34 {
35 namespace MarketData
36 {
37 namespace OptiqMdg
38 {
41 {
45 {
46 return 86400000000000ll;
47 }
48
52 {
53 return 3600000000000ll;
54 }
55
59 {
60 return 60000000000ll;
61 }
62
66 {
67 return 1000000000;
68 }
69
73 {
74 return 1000000;
75 }
76
80 {
81 return 1000;
82 }
83
87 {
88 return 24;
89 }
90
94 {
95 return 60;
96 }
97
101 {
102 return 60;
103 }
104
108 {
109 return 1000;
110 }
111
115 {
116 return 1000000;
117 }
118 };
119
123 {
124 public:
126 typedef Int64 Ticks;
127
129 typedef Int32 Days;
130
132 typedef Int32 Hours;
133
135 typedef Int32 Minutes;
136
138 typedef Int32 Seconds;
139
142
145
148
151 : ticks_(ticks)
152 {
153 }
154
163 Days days,
164 Hours hours,
168 : ticks_(
169 static_cast<Ticks>(days) *
170 TimeTraits::nanosecondsPerDay() +
171 static_cast<Ticks>(hours) *
172 TimeTraits::nanosecondsPerHour() +
173 static_cast<Ticks>(minutes) *
174 TimeTraits::nanosecondsPerMinute() +
175 static_cast<Ticks>(seconds) *
176 TimeTraits::nanosecondsPerSecond() +
178 {
179 }
180
189 Hours hours,
193 : ticks_(
194 static_cast<Ticks>(hours) *
195 TimeTraits::nanosecondsPerHour() +
196 static_cast<Ticks>(minutes) *
197 TimeTraits::nanosecondsPerMinute() +
198 static_cast<Ticks>(seconds) *
199 TimeTraits::nanosecondsPerSecond() +
201 {
202 }
203
206 Days days() const
207 {
208 return
209 static_cast<Days>
210 (ticks_ /
212 }
213
216 Hours hours() const
217 {
218 return
219 static_cast<Hours>(
220 (ticks_ /
223 );
224 }
225
229 {
230 return
231 static_cast<Minutes>(
232 (ticks_ /
235 );
236 }
237
241 {
242 return
243 static_cast<Seconds>(
244 (ticks_ /
247 );
248 }
249
253 {
254 return
255 static_cast<Milliseconds>
256 ((ticks_ /
259 );
260 }
261
265 {
266 return
267 static_cast<Microseconds>
268 ((ticks_ /
271 );
272 }
273
277 {
278 return
279 static_cast<Nanoseconds>
280 (ticks_ %
282 }
283
289 Ticks ticks() const
290 {
291 return ticks_;
292 }
293
295 TimeSpan&
297 const TimeSpan& other)
298 {
299 ticks_ += other.ticks_;
300 return *this;
301 }
302
304 TimeSpan&
306 const TimeSpan& other)
307 {
308 ticks_ -= other.ticks_;
309 return *this;
310 }
311
313 void
316 {
317 std::swap(ticks_, other.ticks_);
318 }
319
320 private:
321 Ticks ticks_;
322 };
323
325 inline
326 bool
328 const TimeSpan& left,
329 const TimeSpan& right)
330 {
331 return left.ticks() == right.ticks();
332 }
333
335 inline
336 bool
338 const TimeSpan& left,
339 const TimeSpan& right)
340 {
341 return left.ticks() != right.ticks();
342 }
343
345 inline
346 bool
348 const TimeSpan& left,
349 const TimeSpan& right)
350 {
351 return left.ticks() < right.ticks();
352 }
353
355 inline
356 bool
358 const TimeSpan& left,
359 const TimeSpan& right)
360 {
361 return left.ticks() > right.ticks();
362 }
363
364 // TimeSpan serialization.
365
366 // Serializes timespan according to HH:MM:SS pattern.
367 ONIXS_EURONEXT_OPTIQMDG_API
368 void
369 toStrAsHHMMSS(std::string&, TimeSpan);
370
371 // Serializes timespan according to HH:MM:SS.sss pattern.
372 ONIXS_EURONEXT_OPTIQMDG_API
373 void
375
376 // Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
377 ONIXS_EURONEXT_OPTIQMDG_API
378 void
380
383 {
384 enum Enum
385 {
388
391
394 };
395 };
396
397
399 inline
400 void toStr(
401 std::string& str,
402 TimeSpan timeSpan,
403 TimeSpanFormat::Enum format =
405 {
406 switch (format)
407 {
409 toStrAsHHMMSS(str, timeSpan);
410 break;
411
413 toStrAsHHMMSSmsec(str, timeSpan);
414 break;
415
417 toStrAsSDHHMMSSnsec(str, timeSpan);
418 break;
419
420 default:
421 {
422 throw std::invalid_argument(
423 "Unknown timespan format pattern specified. ");
424 }
425 }
426 }
427
429 inline
430 std::string toStr(
431 TimeSpan timeSpan,
432 TimeSpanFormat::Enum format =
434 {
435 std::string str;
436
437 toStr(str, timeSpan, format);
438
439 return str;
440 }
441
461
480
483 {
484 public:
486 typedef UInt64 Ticks;
487
489 typedef UInt32 Year;
490
492 typedef
495
497 typedef UInt32 Day;
498
500 typedef UInt32 Hour;
501
503 typedef UInt32 Minute;
504
506 typedef UInt32 Second;
507
510
513
516
518 ONIXS_EURONEXT_OPTIQMDG_API
519 static
521
523 ONIXS_EURONEXT_OPTIQMDG_API
524 static
526
528 explicit
531 : sinceEpoch_(ticks)
532 {
533 }
534
541 Year year,
542 Month month,
543 Day day,
544 Hour hour = 0,
545 Minute minute = 0,
546 Second second = 0,
548 : sinceEpoch_(
549 toTicks(
550 year, month, day,
552 )
553 {
554 }
555
558 const Timestamp& other)
559 : sinceEpoch_(other.sinceEpoch_)
560 {
561 }
562
564 Year year() const
565 {
567
568 toDate(sinceEpoch_, year, month, day);
569
570 return year;
571 }
572
574 Month month() const
575 {
577
578 toDate(sinceEpoch_, year, month, day);
579
580 return month;
581 }
582
584 Day day() const
585 {
587
588 toDate(sinceEpoch_, year, month, day);
589
590 return day;
591 }
592
594 Hour hour() const
595 {
596 return static_cast<Hour>(time().hours());
597 }
598
601 {
602 return static_cast<Minute>(time().minutes());
603 }
604
607 {
608 return static_cast<Second>(time().seconds());
609 }
610
613 {
614 return static_cast<Millisecond>(time().milliseconds());
615 }
616
619 {
620 return static_cast<Microsecond>(time().microseconds());
621 }
622
625 {
626 return static_cast<Nanosecond>(time().nanoseconds());
627 }
628
631 {
632 return
633 Timestamp(
634 sinceEpoch_ -
635 sinceEpoch_ %
637 }
638
641 {
642 return
643 TimeSpan(
644 sinceEpoch_ %
646 }
647
651 {
652 return sinceEpoch_;
653 }
654
656 Timestamp&
658 const Timestamp& other)
659 {
660 sinceEpoch_ = other.sinceEpoch_;
661
662 return *this;
663 }
664
666 void
668 Timestamp& other)
669 {
670 std::swap(
671 sinceEpoch_,
672 other.sinceEpoch_);
673 }
674
677 static
680 unsigned long long presentation,
682
683 private:
684 // Time interval in nanoseconds since the 01-01-1970.
685 Ticks sinceEpoch_;
686
687 // Converts structured date-time into ticks.
688 ONIXS_EURONEXT_OPTIQMDG_API
689 static
690 Ticks toTicks(
691 Year, Month, Day,
693 Nanosecond);
694
695 // Extracts date components from ticks.
696 ONIXS_EURONEXT_OPTIQMDG_API
697 static
698 void toDate(Ticks, Year&, Month&, Day&);
699 };
700
702 inline
703 Timestamp fromSeconds(UInt32 secondsSinceEpoch)
704 {
705 return Timestamp(
706 static_cast<Timestamp::Ticks>(
707 secondsSinceEpoch) * TimeTraits::nanosecondsPerSecond());
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 bool
727 const Timestamp& left,
728 const Timestamp& right)
729 {
730 return (
731 left.sinceEpoch() !=
732 right.sinceEpoch()
733 );
734 }
735
737 inline
738 bool
740 const Timestamp& left,
741 const Timestamp& right)
742 {
743 return (
744 left.sinceEpoch() <
745 right.sinceEpoch()
746 );
747 }
748
750 inline
751 bool
753 const Timestamp& left,
754 const Timestamp& right)
755 {
756 return (
757 left.sinceEpoch() <=
758 right.sinceEpoch()
759 );
760 }
761
763 inline
764 bool
766 const Timestamp& left,
767 const Timestamp& right)
768 {
769 return (
770 left.sinceEpoch() >
771 right.sinceEpoch()
772 );
773 }
774
776 inline
777 bool
779 const Timestamp& left,
780 const Timestamp& right)
781 {
782 return (
783 left.sinceEpoch() >=
784 right.sinceEpoch()
785 );
786 }
787
789 inline
790 Timestamp
792 const Timestamp& timestamp,
793 const TimeSpan& timeSpan)
794 {
795 return
796 Timestamp(
797 timestamp.sinceEpoch() +
798 timeSpan.ticks()
799 );
800 }
801
803 inline
804 Timestamp
806 const Timestamp& timestamp,
807 const TimeSpan& timeSpan)
808 {
809 return
810 Timestamp(
811 timestamp.sinceEpoch() -
812 timeSpan.ticks()
813 );
814 }
815
817 inline
818 TimeSpan
820 const Timestamp& left,
821 const Timestamp& right)
822 {
823 return
824 TimeSpan(
825 left.sinceEpoch() -
826 right.sinceEpoch()
827 );
828 }
829
830 // Serialization.
831
833 ONIXS_EURONEXT_OPTIQMDG_API
834 void
836 (
837 std::string&,
839 );
840
842 ONIXS_EURONEXT_OPTIQMDG_API
843 void
845 (
846 std::string&,
848 );
849
851 ONIXS_EURONEXT_OPTIQMDG_API
852 void
854 (
855 std::string&,
857 );
858
860 ONIXS_EURONEXT_OPTIQMDG_API
861 void
863 (
864 std::string&,
866 );
867
869 ONIXS_EURONEXT_OPTIQMDG_API
870 void
872 std::string& str,
873 Timestamp timestamp,
874 TimestampFormat::Enum format =
876
878 inline
879 std::string
881 Timestamp timestamp,
882 TimestampFormat::Enum format =
884 {
885 std::string str;
886
887 toStr(str, timestamp, format);
888
889 return str;
890 }
891
893 ONIXS_EURONEXT_OPTIQMDG_API
894 bool
896 Timestamp&,
897 const char*,
898 size_t);
899
900 inline
901 bool
903 Timestamp& ts,
904 const std::string& str)
905 {
906 return
907 fromStr(
908 ts, str.c_str(), str.size());
909 }
910
911
912
913
914 }
915 }
916 }
917}
#define ONIXS_EURONEXT_OPTIQMDG_NOTHROW
Definition Compiler.h:186
#define ONIXS_EURONEXT_OPTIQMDG_CONSTEXPR
Definition Compiler.h:189
Milliseconds milliseconds() const
Milliseconds component of time interval.
Definition Time.h:252
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Initializes with given set of values.
Definition Time.h:162
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:141
void swap(TimeSpan &other) noexcept
Exchanges with given instance.
Definition Time.h:314
Ticks ticks() const
Number of ticks in given time interval.
Definition Time.h:289
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:135
TimeSpan(Ticks ticks=0) noexcept
Initializes timespan from given number of ticks.
Definition Time.h:150
Int32 seconds() const
Seconds component of time interval.
Definition Time.h:240
Nanoseconds nanoseconds() const
Nanoseconds component of time interval.
Definition Time.h:276
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:147
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:138
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition Time.h:296
Microseconds microseconds() const
Microseconds component of time interval.
Definition Time.h:264
Days days() const
Days component of time interval.
Definition Time.h:206
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:144
Int32 Hours
Integral type for number of hours.
Definition Time.h:132
Int32 Days
Integral type for number of days.
Definition Time.h:129
Hours hours() const
Hours component of time interval.
Definition Time.h:216
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition Time.h:305
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:126
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Initializes with given set of values.
Definition Time.h:188
Int32 minutes() const
Minutes component of time interval.
Definition Time.h:228
Represents time point without time-zone information.
Definition Time.h:483
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:618
Month month() const
Month component of given time point.
Definition Time.h:574
Year year() const
Year component of given time point.
Definition Time.h:564
Timestamp(const Timestamp &other)
Initializes as copy of other instance.
Definition Time.h:557
static ONIXS_EURONEXT_OPTIQMDG_API Timestamp now()
Returns current local time.
Day day() const
Day component of given time point.
Definition Time.h:584
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:500
Timestamp(Ticks ticks=0) noexcept
Initializes from number of ticks since epoch.
Definition Time.h:529
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:503
Minute minute() const
Minute component of given time point.
Definition Time.h:600
static Timestamp parse(unsigned long long presentation, TimestampFormat::Enum format=TimestampFormat::YYYYMMDD)
Parses timestamp from its numeric presentation (like 20201115) only YYYYMMDD is supported.
Hour hour() const
Hour component of given time point.
Definition Time.h:594
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:509
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:624
Timestamp date() const
Timestamp without a time part.
Definition Time.h:630
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:486
TimeSpan time() const
Time part of timestamp.
Definition Time.h:640
static ONIXS_EURONEXT_OPTIQMDG_API Timestamp utcNow()
Returns current UTC time.
Second second() const
Second component of given time point.
Definition Time.h:606
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:512
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:540
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:667
UInt32 Second
Integral type presenting second component.
Definition Time.h:506
UInt32 Day
Integral type presenting day component.
Definition Time.h:497
Timestamp & operator=(const Timestamp &other)
Reinitializes as copy of given instance.
Definition Time.h:657
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:515
UInt32 Year
Integral type presenting year component.
Definition Time.h:489
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:612
Ticks sinceEpoch() const noexcept
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:649
OnixS::Euronext::MarketData::OptiqMdg::Month::Enum Month
Type presenting month component.
Definition Time.h:494
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
void toStr(std::string &str, TimeSpan timeSpan, TimeSpanFormat::Enum format=TimeSpanFormat::SDHHMMSSnsec)
Appends timespan formatted in specified pattern to given string.
Definition Time.h:400
Timestamp fromSeconds(UInt32 secondsSinceEpoch)
Create Timestamp from whole seconds since the Epoch.
Definition Time.h:703
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsHHMMSS(std::string &, TimeSpan)
bool operator==(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for equality.
Definition Time.h:327
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
bool operator>=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Time.h:778
bool operator>(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval greater than right one.
Definition Time.h:357
bool operator<=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Time.h:752
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition Time.h:791
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
ONIXS_EURONEXT_OPTIQMDG_API bool fromStr(Timestamp &, const char *, size_t)
De-serializes a timestamp from the given string.
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsHHMMSSmsec(std::string &, TimeSpan)
bool operator<(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval less than right one.
Definition Time.h:347
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition Time.h:805
bool operator!=(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for in-equality.
Definition Time.h:337
ONIXS_EURONEXT_OPTIQMDG_API void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
Identifies months in year.
Definition Time.h:444
Collection of timespan formatting patterns.
Definition Time.h:383
Miscellaneous time characteristics.
Definition Time.h:41
static constexpr Int32 secondsPerMinute()
Returns number of seconds in single minute.
Definition Time.h:100
static constexpr Int32 microsecondsPerSecond()
Returns number of microseconds in single second.
Definition Time.h:114
static constexpr Int32 millisecondsPerSecond()
Returns number of milliseconds in single second.
Definition Time.h:107
static constexpr Int64 nanosecondsPerMinute()
Returns number of nanoseconds in single minute.
Definition Time.h:58
static constexpr Int32 hoursPerDay()
Returns number of hours in single day.
Definition Time.h:86
static constexpr Int64 nanosecondsPerHour()
Returns number of nanoseconds in single hour.
Definition Time.h:51
static constexpr Int32 nanosecondsPerMicrosecond()
Returns number of nanoseconds in single microsecond.
Definition Time.h:79
static constexpr Int64 nanosecondsPerDay()
Returns number of nanoseconds in single day.
Definition Time.h:44
static constexpr Int32 minutesPerHour()
Returns number of minutes in single hour.
Definition Time.h:93
static constexpr Int32 nanosecondsPerSecond()
Returns number of nanoseconds in single second.
Definition Time.h:65
static constexpr Int32 nanosecondsPerMillisecond()
Returns number of nanoseconds in single millisecond.
Definition Time.h:72
Collection of timestamp formatting patterns.
Definition Time.h:464
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:477