OnixS C++ CME MDP Premium Market Data Handler 5.9.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_CMEMDH_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 : ticks_(
146 static_cast<Ticks>(days) * TimeTraits::nanosecondsPerDay()
147 + static_cast<Ticks>(hours) * TimeTraits::nanosecondsPerHour()
148 + static_cast<Ticks>(minutes) * TimeTraits::nanosecondsPerMinute()
149 + static_cast<Ticks>(seconds) * TimeTraits::nanosecondsPerSecond() + nanoseconds
150 )
151 {
152 }
153
162 : ticks_(
163 static_cast<Ticks>(hours) * TimeTraits::nanosecondsPerHour()
164 + static_cast<Ticks>(minutes) * TimeTraits::nanosecondsPerMinute()
165 + static_cast<Ticks>(seconds) * TimeTraits::nanosecondsPerSecond() + nanoseconds
166 )
167 {
168 }
169
171 TimeSpan(const TimeSpan& other)
172 : ticks_(other.ticks_)
173 {
174 }
175
178 Days days() const
179 {
180 return static_cast<Days>(ticks_ / TimeTraits::nanosecondsPerDay());
181 }
182
185 Hours hours() const
186 {
187 return static_cast<Hours>((ticks_ / TimeTraits::nanosecondsPerHour()) % TimeTraits::hoursPerDay());
188 }
189
193 {
194 return static_cast<Minutes>((ticks_ / TimeTraits::nanosecondsPerMinute()) % TimeTraits::minutesPerHour());
195 }
196
200 {
201 return static_cast<Seconds>((ticks_ / TimeTraits::nanosecondsPerSecond()) % TimeTraits::secondsPerMinute());
202 }
203
207 {
208 return static_cast<Milliseconds>(
210 );
211 }
212
216 {
217 return static_cast<Microseconds>(
219 );
220 }
221
225 {
226 return static_cast<Nanoseconds>(ticks_ % TimeTraits::nanosecondsPerSecond());
227 }
228
234 Ticks ticks() const
235 {
236 return ticks_;
237 }
238
241 {
242 ticks_ += other.ticks_;
243
244 return *this;
245 }
246
249 {
250 ticks_ -= other.ticks_;
251
252 return *this;
253 }
254
257 {
258 ticks_ = other.ticks_;
259
260 return *this;
261 }
262
264 void swap(TimeSpan& other)
265 {
266 std::swap(ticks_, other.ticks_);
267 }
268
269private:
270 Ticks ticks_;
271};
272
274inline bool operator==(const TimeSpan& left, const TimeSpan& right)
275{
276 return left.ticks() == right.ticks();
277}
278
280inline bool operator!=(const TimeSpan& left, const TimeSpan& right)
281{
282 return left.ticks() != right.ticks();
283}
284
286inline bool operator<(const TimeSpan& left, const TimeSpan& right)
287{
288 return left.ticks() < right.ticks();
289}
290
292inline bool operator>(const TimeSpan& left, const TimeSpan& right)
293{
294 return left.ticks() > right.ticks();
295}
296
297// TimeSpan serialization.
298
299// Serializes timespan according to HH:MM:SS pattern.
301void toStrAsHHMMSS(std::string&, TimeSpan);
302
303// Serializes timespan according to HH:MM:SS.sss pattern.
305void toStrAsHHMMSSmsec(std::string&, TimeSpan);
306
307// Serializes timespan according to D.HH:MM:SS.sssssssss pattern.
309void toStrAsSDHHMMSSnsec(std::string&, TimeSpan);
310
326
328inline void toStr(std::string& str, TimeSpan timeSpan, TimeSpanFormat::Enum format = TimeSpanFormat::SDHHMMSSnsec)
329{
330 switch (format)
331 {
333 toStrAsHHMMSS(str, timeSpan);
334 break;
335
337 toStrAsHHMMSSmsec(str, timeSpan);
338 break;
339
341 toStrAsSDHHMMSSnsec(str, timeSpan);
342 break;
343
344 default:
345 {
346 throw std::invalid_argument("Unknown timespan format pattern specified. ");
347 }
348 }
349}
350
353{
354 std::string str;
355
356 toStr(str, timeSpan, format);
357
358 return str;
359}
360
385
388{
389public:
391 typedef UInt64 Ticks;
392
394 typedef UInt32 Year;
395
398
400 typedef UInt32 Day;
401
403 typedef UInt32 Hour;
404
406 typedef UInt32 Minute;
407
409 typedef UInt32 Second;
410
413
416
419
421 explicit Timestamp(Ticks ticks = 0)
422 : sinceEpoch_(ticks)
423 {
424 }
425
432 Year year,
433 Month month,
434 Day day,
435 Hour hour = 0,
436 Minute minute = 0,
437 Second second = 0,
439 )
440 : sinceEpoch_(toTicks(year, month, day, hour, minute, second, nanosecond))
441 {
442 }
443
445 Year year() const
446 {
447 Year year;
448 Month month;
449 Day day;
450
451 toDate(sinceEpoch_, year, month, day);
452
453 return year;
454 }
455
457 Month month() const
458 {
459 Year year;
460 Month month;
461 Day day;
462
463 toDate(sinceEpoch_, year, month, day);
464
465 return month;
466 }
467
469 Day day() const
470 {
471 Year year;
472 Month month;
473 Day day;
474
475 toDate(sinceEpoch_, year, month, day);
476
477 return day;
478 }
479
481 Hour hour() const
482 {
483 return static_cast<Hour>(time().hours());
484 }
485
488 {
489 return static_cast<Minute>(time().minutes());
490 }
491
494 {
495 return static_cast<Second>(time().seconds());
496 }
497
500 {
501 return static_cast<Millisecond>(time().milliseconds());
502 }
503
506 {
507 return static_cast<Microsecond>(time().microseconds());
508 }
509
512 {
513 return static_cast<Nanosecond>(time().nanoseconds());
514 }
515
518 {
519 return Timestamp(sinceEpoch_ - sinceEpoch_ % TimeTraits::nanosecondsPerDay());
520 }
521
524 {
525 return TimeSpan(sinceEpoch_ % TimeTraits::nanosecondsPerDay());
526 }
527
530 {
531 return sinceEpoch_;
532 }
533
535 void swap(Timestamp& other)
536 {
537 std::swap(sinceEpoch_, other.sinceEpoch_);
538 }
539
540private:
541 // Time interval in nanoseconds since the 01-01-1970.
542 Ticks sinceEpoch_;
543
544 // Converts structured date-time into ticks.
546 static Ticks toTicks(Year, Month, Day, Hour, Minute, Second, Nanosecond);
547
548 // Extracts date components from ticks.
550 static void toDate(Ticks, Year&, Month&, Day&);
551};
552
554inline bool operator==(const Timestamp& left, const Timestamp& right)
555{
556 return (left.sinceEpoch() == right.sinceEpoch());
557}
558
560inline bool operator!=(const Timestamp& left, const Timestamp& right)
561{
562 return (left.sinceEpoch() != right.sinceEpoch());
563}
564
566inline bool operator<(const Timestamp& left, const Timestamp& right)
567{
568 return (left.sinceEpoch() < right.sinceEpoch());
569}
570
572inline bool operator<=(const Timestamp& left, const Timestamp& right)
573{
574 return (left.sinceEpoch() <= right.sinceEpoch());
575}
576
578inline bool operator>(const Timestamp& left, const Timestamp& right)
579{
580 return (left.sinceEpoch() > right.sinceEpoch());
581}
582
584inline bool operator>=(const Timestamp& left, const Timestamp& right)
585{
586 return (left.sinceEpoch() >= right.sinceEpoch());
587}
588
590inline Timestamp operator+(const Timestamp& timestamp, const TimeSpan& timeSpan)
591{
592 return Timestamp(timestamp.sinceEpoch() + timeSpan.ticks());
593}
594
596inline Timestamp operator-(const Timestamp& timestamp, const TimeSpan& timeSpan)
597{
598 return Timestamp(timestamp.sinceEpoch() - timeSpan.ticks());
599}
600
602inline TimeSpan operator-(const Timestamp& left, const Timestamp& right)
603{
604 return TimeSpan(left.sinceEpoch() - right.sinceEpoch());
605}
606
607// Serialization.
608
611void toStrAsYYYYMMDD(std::string&, Timestamp);
612
616
620
624
643
645inline void
647{
648 switch (format)
649 {
651 toStrAsYYYYMMDD(str, timestamp);
652 break;
653
655 toStrAsYYYYMMDDHHMMSS(str, timestamp);
656 break;
657
659 toStrAsYYYYMMDDHHMMSSmsec(str, timestamp);
660 break;
661
663 toStrAsYYYYMMDDHHMMSSnsec(str, timestamp);
664 break;
665
666 default:
667 {
668 throw std::invalid_argument("Unknown timestamp format pattern specified. ");
669 }
670 }
671}
672
675{
676 std::string str;
677
678 toStr(str, timestamp, format);
679
680 return str;
681}
682
685bool fromStr(Timestamp&, const Char*, size_t);
686
687inline bool fromStr(Timestamp& ts, const std::string& str)
688{
689 return fromStr(ts, str.c_str(), str.size());
690}
691
692ONIXS_CMEMDH_DATA_PACKING_END
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:46
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition Bootstrap.h:47
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:68
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:171
Represents time interval.
Definition Time.h:105
Milliseconds milliseconds() const
Milliseconds component of time interval.
Definition Time.h:206
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:234
TimeSpan & operator=(const TimeSpan &other)
Reinitializes as copy of given instance.
Definition Time.h:256
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:171
Int32 seconds() const
Seconds component of time interval.
Definition Time.h:199
void swap(TimeSpan &other)
Exchanges with given instance.
Definition Time.h:264
Nanoseconds nanoseconds() const
Nanoseconds component of time interval.
Definition Time.h:224
Int32 Nanoseconds
Integral type for number of nanoseconds.
Definition Time.h:129
Int32 Seconds
Integral type for number of seconds.
Definition Time.h:120
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
Definition Time.h:240
Microseconds microseconds() const
Microseconds component of time interval.
Definition Time.h:215
Days days() const
Days component of time interval.
Definition Time.h:178
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:185
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
Definition Time.h:248
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:161
Int32 minutes() const
Minutes component of time interval.
Definition Time.h:192
Represents time point without time-zone information.
Definition Time.h:388
Microsecond microsecond() const
Microsecond component of given time point.
Definition Time.h:505
Ticks sinceEpoch() const
Number of nanoseconds since the Epoch (01-01-1970).
Definition Time.h:529
Month month() const
Month component of given time point.
Definition Time.h:457
Year year() const
Year component of given time point.
Definition Time.h:445
Day day() const
Day component of given time point.
Definition Time.h:469
UInt32 Hour
Integral type presenting hour component.
Definition Time.h:403
UInt32 Minute
Integral type presenting minute component.
Definition Time.h:406
Minute minute() const
Minute component of given time point.
Definition Time.h:487
Hour hour() const
Hour component of given time point.
Definition Time.h:481
UInt32 Millisecond
Integral type presenting millisecond component.
Definition Time.h:412
Nanosecond nanosecond() const
Nanosecond component of given time point.
Definition Time.h:511
Timestamp date() const
Timestamp without a time part.
Definition Time.h:517
OnixS::CME::MDH::Month::Enum Month
Type presenting month component.
Definition Time.h:397
UInt64 Ticks
Integral type storing internal ticks.
Definition Time.h:391
TimeSpan time() const
Time part of timestamp.
Definition Time.h:523
Timestamp(Ticks ticks=0)
Initializes from number of ticks since epoch.
Definition Time.h:421
Second second() const
Second component of given time point.
Definition Time.h:493
UInt32 Microsecond
Integral type presenting microsecond component.
Definition Time.h:415
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:431
void swap(Timestamp &other)
Exchanges value with other instance.
Definition Time.h:535
UInt32 Second
Integral type presenting second component.
Definition Time.h:409
UInt32 Day
Integral type presenting day component.
Definition Time.h:400
UInt32 Nanosecond
Integral type presenting nanosecond component.
Definition Time.h:418
UInt32 Year
Integral type presenting year component.
Definition Time.h:394
Millisecond millisecond() const
Millisecond component of given time point.
Definition Time.h:499
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:110
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:96
bool fromStr(Decimal &, const Char *, size_t)
Deserializes a decimal number from the given text presentation.
UInt64 UInt64
uInt64.
Definition Fields.h:205
bool operator<=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:117
Int32 Int32
int32.
Definition Fields.h:60
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.
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:89
void toStrAsHHMMSS(std::string &, TimeSpan)
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
UInt32 UInt32
uInt32.
Definition Fields.h:202
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition Time.h:590
void toStrAsYYYYMMDD(std::string &, Timestamp)
Serializes timestamp in YYYYMMDD format.
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:124
void toStrAsSDHHMMSSnsec(std::string &, TimeSpan)
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:103
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition Time.h:596
void toStrAsYYYYMMDDHHMMSSnsec(std::string &, Timestamp)
Serializes timestamp in YYYYMMDDHHMMSSnsec format.
Identifies months in year.
Definition Time.h:363
Enum
Identifies months in year.
Definition Time.h:370
Collection of timespan formatting patterns.
Definition Time.h:313
@ HHMMSSmsec
HH:MM:SS.sss.
Definition Time.h:320
@ SDHHMMSSnsec
D.HH:MM:SS.sssssssss.
Definition Time.h:323
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:627
@ YYYYMMDDHHMMSSmsec
YYYYMMDD-HH:MM:SS.sss.
Definition Time.h:637
@ YYYYMMDDHHMMSS
YYYYMMDD-HH:MM:SS.
Definition Time.h:634
@ YYYYMMDDHHMMSSnsec
YYYYMMDD-HH:MM:SS.sssssssss.
Definition Time.h:640