OnixS C++ LSE GTP Market Data Handler 1.0.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#pragma once
21
22#include <string>
23#include <algorithm>
24#include <stdexcept>
25
29
30namespace OnixS
31{
32 namespace LSE
33 {
34 namespace MarketData
35 {
36 namespace GTP
37 {
40 {
43 static Int64 nanosecondsPerDay()
44 {
45 return 86400000000000ll;
46 }
47
50 static Int64 nanosecondsPerHour()
51 {
52 return 3600000000000ll;
53 }
54
57 static Int64 nanosecondsPerMinute()
58 {
59 return 60000000000ll;
60 }
61
64 static Int32 nanosecondsPerSecond()
65 {
66 return 1000000000;
67 }
68
72 {
73 return 1000000;
74 }
75
79 {
80 return 1000;
81 }
82
85 static Int32 hoursPerDay()
86 {
87 return 24;
88 }
89
92 static Int32 minutesPerHour()
93 {
94 return 60;
95 }
96
99 static Int32 secondsPerMinute()
100 {
101 return 60;
102 }
103
107 {
108 return 1000;
109 }
110
114 {
115 return 1000000;
116 }
117 };
118
122 {
123 public:
125 typedef Int64 Ticks;
126
128 typedef Int32 Days;
129
131 typedef Int32 Hours;
132
134 typedef Int32 Minutes;
135
137 typedef Int32 Seconds;
138
140 typedef Int32 Milliseconds;
141
143 typedef Int32 Microseconds;
144
146 typedef Int32 Nanoseconds;
147
150 : ticks_(ticks)
151 {
152 }
153
162 Days days,
163 Hours hours,
167 : ticks_(
168 static_cast<Ticks>(days) *
169 TimeTraits::nanosecondsPerDay() +
170 static_cast<Ticks>(hours) *
171 TimeTraits::nanosecondsPerHour() +
172 static_cast<Ticks>(minutes) *
173 TimeTraits::nanosecondsPerMinute() +
174 static_cast<Ticks>(seconds) *
175 TimeTraits::nanosecondsPerSecond() +
177 {
178 }
179
188 Hours hours,
192 : ticks_(
193 static_cast<Ticks>(hours) *
194 TimeTraits::nanosecondsPerHour() +
195 static_cast<Ticks>(minutes) *
196 TimeTraits::nanosecondsPerMinute() +
197 static_cast<Ticks>(seconds) *
198 TimeTraits::nanosecondsPerSecond() +
200 {
201 }
202
205 Days days() const
206 {
207 return
208 static_cast<Days>
209 (ticks_ /
211 }
212
215 Hours hours() const
216 {
217 return
218 static_cast<Hours>(
219 (ticks_ /
222 );
223 }
224
227 Int32 minutes() const
228 {
229 return
230 static_cast<Minutes>(
231 (ticks_ /
234 );
235 }
236
239 Int32 seconds() const
240 {
241 return
242 static_cast<Seconds>(
243 (ticks_ /
246 );
247 }
248
252 {
253 return
254 static_cast<Milliseconds>
255 ((ticks_ /
258 );
259 }
260
264 {
265 return
266 static_cast<Microseconds>
267 ((ticks_ /
270 );
271 }
272
276 {
277 return
278 static_cast<Nanoseconds>
279 (ticks_ %
281 }
282
288 Ticks ticks() const
289 {
290 return ticks_;
291 }
292
294 TimeSpan&
296 const TimeSpan& other)
297 {
298 ticks_ += other.ticks_;
299 return *this;
300 }
301
303 TimeSpan&
305 const TimeSpan& other)
306 {
307 ticks_ -= other.ticks_;
308 return *this;
309 }
310
312 void
315 {
316 std::swap(ticks_, other.ticks_);
317 }
318
319 private:
320 Ticks ticks_;
321 };
322
324 inline
325 bool
327 const TimeSpan& left,
328 const TimeSpan& right)
329 {
330 return left.ticks() == right.ticks();
331 }
332
334 inline
335 bool
337 const TimeSpan& left,
338 const TimeSpan& right)
339 {
340 return left.ticks() != right.ticks();
341 }
342
344 inline
345 bool
347 const TimeSpan& left,
348 const TimeSpan& right)
349 {
350 return left.ticks() < right.ticks();
351 }
352
354 inline
355 bool
357 const TimeSpan& left,
358 const TimeSpan& right)
359 {
360 return left.ticks() > right.ticks();
361 }
362
363 // TimeSpan serialization.
364
365 // Serializes timespan according to HH:MM:SS pattern.
366 ONIXS_LSE_GTP_API
367 void
368 toStrAsHHMMSS(std::string&, TimeSpan);
369
370 // Serializes timespan according to HH:MM:SS.sss pattern.
371 ONIXS_LSE_GTP_API
372 void
374
375 // Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
376 ONIXS_LSE_GTP_API
377 void
379
382 {
383 enum Enum
384 {
387
390
393 };
394 };
395
396
398 inline
399 void toStr(
400 std::string& str,
401 TimeSpan timeSpan,
402 TimeSpanFormat::Enum format =
404 {
405 switch (format)
406 {
408 toStrAsHHMMSS(str, timeSpan);
409 break;
410
412 toStrAsHHMMSSmsec(str, timeSpan);
413 break;
414
416 toStrAsSDHHMMSSnsec(str, timeSpan);
417 break;
418
419 default:
420 {
421 throw std::invalid_argument(
422 "Unknown timespan format pattern specified. ");
423 }
424 }
425 }
426
428 inline
429 std::string toStr(
430 TimeSpan timeSpan,
431 TimeSpanFormat::Enum format =
433 {
434 std::string str;
435
436 toStr(str, timeSpan, format);
437
438 return str;
439 }
440
460
479
482 {
483 public:
485 typedef UInt64 Ticks;
486
488 typedef UInt32 Year;
489
491 typedef
494
496 typedef UInt32 Day;
497
499 typedef UInt32 Hour;
500
502 typedef UInt32 Minute;
503
505 typedef UInt32 Second;
506
508 typedef UInt32 Millisecond;
509
511 typedef UInt32 Microsecond;
512
514 typedef UInt32 Nanosecond;
515
517 ONIXS_LSE_GTP_API
518 static
520
522 ONIXS_LSE_GTP_API
523 static
525
527 explicit
530 : sinceEpoch_(ticks)
531 {
532 }
533
540 Year year,
541 Month month,
542 Day day,
543 Hour hour = 0,
544 Minute minute = 0,
545 Second second = 0,
547 : sinceEpoch_(
548 toTicks(
549 year, month, day,
551 )
552 {
553 }
554
557 const Timestamp& other)
558 : sinceEpoch_(other.sinceEpoch_)
559 {
560 }
561
563 Year year() const
564 {
566
567 toDate(sinceEpoch_, year, month, day);
568
569 return year;
570 }
571
573 Month month() const
574 {
576
577 toDate(sinceEpoch_, year, month, day);
578
579 return month;
580 }
581
583 Day day() const
584 {
586
587 toDate(sinceEpoch_, year, month, day);
588
589 return day;
590 }
591
593 Hour hour() const
594 {
595 return static_cast<Hour>(time().hours());
596 }
597
600 {
601 return static_cast<Minute>(time().minutes());
602 }
603
606 {
607 return static_cast<Second>(time().seconds());
608 }
609
612 {
613 return static_cast<Millisecond>(time().milliseconds());
614 }
615
618 {
619 return static_cast<Microsecond>(time().microseconds());
620 }
621
624 {
625 return static_cast<Nanosecond>(time().nanoseconds());
626 }
627
630 {
631 return
632 Timestamp(
633 sinceEpoch_ -
634 sinceEpoch_ %
636 }
637
640 {
641 return
642 TimeSpan(
643 sinceEpoch_ %
645 }
646
650 {
651 return sinceEpoch_;
652 }
653
655 Timestamp&
657 const Timestamp& other)
658 {
659 sinceEpoch_ = other.sinceEpoch_;
660
661 return *this;
662 }
663
665 void
667 Timestamp& other)
668 {
669 std::swap(
670 sinceEpoch_,
671 other.sinceEpoch_);
672 }
673
676 static
679 unsigned long long presentation,
681
682 private:
683 // Time interval in nanoseconds since the 01-01-1970.
684 Ticks sinceEpoch_;
685
686 // Converts structured date-time into ticks.
687 ONIXS_LSE_GTP_API
688 static
689 Ticks toTicks(
690 Year, Month, Day,
692 Nanosecond);
693
694 // Extracts date components from ticks.
695 ONIXS_LSE_GTP_API
696 static
697 void toDate(Ticks, Year&, Month&, Day&);
698 };
699
701 inline
702 Timestamp fromSeconds(UInt32 secondsSinceEpoch)
703 {
704 return Timestamp(
705 static_cast<Timestamp::Ticks>(
706 secondsSinceEpoch) * TimeTraits::nanosecondsPerSecond());
707 }
708
710 inline
711 bool
713 const Timestamp& left,
714 const Timestamp& right)
715 {
716 return (
717 left.sinceEpoch() ==
718 right.sinceEpoch()
719 );
720 }
721
723 inline
724 bool
726 const Timestamp& left,
727 const Timestamp& right)
728 {
729 return (
730 left.sinceEpoch() !=
731 right.sinceEpoch()
732 );
733 }
734
736 inline
737 bool
739 const Timestamp& left,
740 const Timestamp& right)
741 {
742 return (
743 left.sinceEpoch() <
744 right.sinceEpoch()
745 );
746 }
747
749 inline
750 bool
752 const Timestamp& left,
753 const Timestamp& right)
754 {
755 return (
756 left.sinceEpoch() <=
757 right.sinceEpoch()
758 );
759 }
760
762 inline
763 bool
765 const Timestamp& left,
766 const Timestamp& right)
767 {
768 return (
769 left.sinceEpoch() >
770 right.sinceEpoch()
771 );
772 }
773
775 inline
776 bool
778 const Timestamp& left,
779 const Timestamp& right)
780 {
781 return (
782 left.sinceEpoch() >=
783 right.sinceEpoch()
784 );
785 }
786
788 inline
789 Timestamp
791 const Timestamp& timestamp,
792 const TimeSpan& timeSpan)
793 {
794 return
795 Timestamp(
796 timestamp.sinceEpoch() +
797 timeSpan.ticks()
798 );
799 }
800
802 inline
803 Timestamp
805 const Timestamp& timestamp,
806 const TimeSpan& timeSpan)
807 {
808 return
809 Timestamp(
810 timestamp.sinceEpoch() -
811 timeSpan.ticks()
812 );
813 }
814
816 inline
817 TimeSpan
819 const Timestamp& left,
820 const Timestamp& right)
821 {
822 return
823 TimeSpan(
824 left.sinceEpoch() -
825 right.sinceEpoch()
826 );
827 }
828
829 // Serialization.
830
832 ONIXS_LSE_GTP_API
833 void
835 (
836 std::string&,
838 );
839
841 ONIXS_LSE_GTP_API
842 void
844 (
845 std::string&,
847 );
848
850 ONIXS_LSE_GTP_API
851 void
853 (
854 std::string&,
856 );
857
859 ONIXS_LSE_GTP_API
860 void
862 (
863 std::string&,
865 );
866
868 ONIXS_LSE_GTP_API
869 void
871 std::string& str,
872 Timestamp timestamp,
873 TimestampFormat::Enum format =
875
877 inline
878 std::string
880 Timestamp timestamp,
881 TimestampFormat::Enum format =
883 {
884 std::string str;
885
886 toStr(str, timestamp, format);
887
888 return str;
889 }
890
892 ONIXS_LSE_GTP_API
893 bool
895 Timestamp&,
896 const char*,
897 size_t);
898
899 inline
900 bool
902 Timestamp& ts,
903 const std::string& str)
904 {
905 return
906 fromStr(
907 ts, str.c_str(), str.size());
908 }
909 }
910 }
911 }
912}
#define ONIXS_LSE_GTP_NOTHROW
Definition Compiler.h:118
#define ONIXS_LSE_GTP_CONSTEXPR
Definition Compiler.h:124
Milliseconds milliseconds() const
Definition Time.h:251
TimeSpan(Days days, Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:161
Int32 Milliseconds
Integral type for number of milliseconds.
Definition Time.h:140
TimeSpan(Ticks ticks=0) ONIXS_LSE_GTP_NOTHROW
Initializes timespan from given number of ticks.
Definition Time.h:149
Int32 Minutes
Integral type for number of minutes.
Definition Time.h:134
Nanoseconds nanoseconds() const
Definition Time.h:275
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:146
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:137
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition Time.h:295
Microseconds microseconds() const
Definition Time.h:263
Int32 Microseconds
Integral type for number of microseconds.
Definition Time.h:143
Int32 Hours
Integral type for number of hours.
Definition Time.h:131
Int32 Days
Integral type for number of days.
Definition Time.h:128
void swap(TimeSpan &other) ONIXS_LSE_GTP_NOTHROW
Exchanges with given instance.
Definition Time.h:313
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition Time.h:304
Int64 Ticks
Integral type presenting internal ticks.
Definition Time.h:125
TimeSpan(Hours hours, Minutes minutes, Seconds seconds, Nanoseconds nanoseconds)
Definition Time.h:187
Represents time point without time-zone information.
Definition Time.h:482
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:617
Month month() const
Month component of given time point.
Definition Time.h:573
Year year() const
Year component of given time point.
Definition Time.h:563
Timestamp(const Timestamp &other)
Initializes as copy of other instance.
Definition Time.h:556
Ticks sinceEpoch() const ONIXS_LSE_GTP_NOTHROW
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:648
Day day() const
Day component of given time point.
Definition Time.h:583
static ONIXS_LSE_GTP_API Timestamp now()
Returns current local time.
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:499
static ONIXS_LSE_GTP_API Timestamp utcNow()
Returns current UTC time.
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:502
Minute minute() const
Minute component of given time point.
Definition Time.h:599
static Timestamp parse(unsigned long long presentation, TimestampFormat::Enum format=TimestampFormat::YYYYMMDD)
Hour hour() const
Hour component of given time point.
Definition Time.h:593
OnixS::LSE::MarketData::GTP::Month::Enum Month
Type presenting month component.
Definition Time.h:493
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:508
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:623
Timestamp date() const
Timestamp without a time part.
Definition Time.h:629
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:485
TimeSpan time() const
Time part of timestamp.
Definition Time.h:639
Second second() const
Second component of given time point.
Definition Time.h:605
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:511
Timestamp(Year year, Month month, Day day, Hour hour=0, Minute minute=0, Second second=0, Nanosecond nanosecond=0)
Definition Time.h:539
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:666
UInt32 Second
Integral type presenting second component.
Definition Time.h:505
UInt32 Day
Integral type presenting day component.
Definition Time.h:496
Timestamp & operator=(const Timestamp &other)
Reinitializes as copy of given instance.
Definition Time.h:656
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:514
UInt32 Year
Integral type presenting year component.
Definition Time.h:488
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:611
Timestamp(Ticks ticks=0) ONIXS_LSE_GTP_NOTHROW
Initializes from number of ticks since epoch.
Definition Time.h:528
ONIXS_LSE_GTP_API void toStrAsYYYYMMDDHHMMSSmsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSmsec format.
ONIXS_LSE_GTP_API void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
ONIXS_LSE_GTP_API void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
Timestamp fromSeconds(UInt32 secondsSinceEpoch)
Create Timestamp from whole seconds since the Epoch.
Definition Time.h:702
bool operator==(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for equality.
Definition Time.h:326
bool operator>=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Time.h:777
ONIXS_LSE_GTP_API void toStrAsHHMMSSmsec(std::string &, TimeSpan)
bool operator>(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval greater than right one.
Definition Time.h:356
ONIXS_LSE_GTP_API void toStrAsYYYYMMDDHHMMSS(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSS format.
bool operator<=(const Timestamp &left, const Timestamp &right)
Establishes order between two instances.
Definition Time.h:751
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition Time.h:790
ONIXS_LSE_GTP_API void toStrAsHHMMSS(std::string &, TimeSpan)
bool operator<(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval less than right one.
Definition Time.h:346
ONIXS_LSE_GTP_API void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
ONIXS_LSE_GTP_API void toStr(std::string &, EventCode::Enum)
Appends string presentation of object.
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition Time.h:804
bool operator!=(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for in-equality.
Definition Time.h:336
ONIXS_LSE_GTP_API bool fromStr(Timestamp &, const char *, size_t)
De-serializes a timestamp from the given string.
Identifies months in year.
Definition Time.h:443
Collection of timespan formatting patterns.
Definition Time.h:382
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Time.h:392
Miscellaneous time characteristics.
Definition Time.h:40
static ONIXS_LSE_GTP_CONSTEXPR Int32 nanosecondsPerSecond()
Returns number of nanoseconds in single second.
Definition Time.h:64
static ONIXS_LSE_GTP_CONSTEXPR Int32 millisecondsPerSecond()
Returns number of milliseconds in single second.
Definition Time.h:106
static ONIXS_LSE_GTP_CONSTEXPR Int64 nanosecondsPerHour()
Returns number of nanoseconds in single hour.
Definition Time.h:50
static ONIXS_LSE_GTP_CONSTEXPR Int32 secondsPerMinute()
Returns number of seconds in single minute.
Definition Time.h:99
static ONIXS_LSE_GTP_CONSTEXPR Int32 nanosecondsPerMicrosecond()
Returns number of nanoseconds in single microsecond.
Definition Time.h:78
static ONIXS_LSE_GTP_CONSTEXPR Int32 nanosecondsPerMillisecond()
Returns number of nanoseconds in single millisecond.
Definition Time.h:71
static ONIXS_LSE_GTP_CONSTEXPR Int32 microsecondsPerSecond()
Returns number of microseconds in single second.
Definition Time.h:113
static ONIXS_LSE_GTP_CONSTEXPR Int32 hoursPerDay()
Returns number of hours in single day.
Definition Time.h:85
static ONIXS_LSE_GTP_CONSTEXPR Int32 minutesPerHour()
Returns number of minutes in single hour.
Definition Time.h:92
static ONIXS_LSE_GTP_CONSTEXPR Int64 nanosecondsPerDay()
Returns number of nanoseconds in single day.
Definition Time.h:43
static ONIXS_LSE_GTP_CONSTEXPR Int64 nanosecondsPerMinute()
Returns number of nanoseconds in single minute.
Definition Time.h:57
Collection of timestamp formatting patterns.
Definition Time.h:463
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:473
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:476