OnixS ICE iMpact Multicast Price Feed Handler C++ library  8.7.0
API documentation
Time.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Onix Solutions Limited. All rights reserved.
3  *
4  * This software owned by Onix Solutions Limited 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 
20 #pragma once
21 
22 #include "Export.h"
23 
24 #include <iosfwd>
25 #include <string>
26 
27 namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
28 
29 /// Time span formats supported.
30 struct ONIXS_ICEMDH_EXPORT TimeSpanFormats
31 {
32  /// \copydoc TimeSpanFormats
33  enum Enum
34  {
35  /// HH:MM:SS.
37 
38  /// HH:MM:SS.sss.
40 
41  /// HH:MM:SS.sssssssss.
42  SDHHMMSSnsec
43  };
44 };
45 
46 /// Time span format.
48 
49 /// Represents time interval. Used primarily to
50 /// present time-only stamps and to measure time
51 /// intervals between two timestamps.
52 class ONIXS_ICEMDH_EXPORT TimeSpan
53 {
54 public:
55  /// Initializes zero span.
56  TimeSpan();
57 
58  /// Initializes with given set of values.
59  /// Input parameters are treated as quantities,
60  /// but not as a time stamp. Therefore, there's
61  /// no requirement to fit in a certain range like
62  /// hours must fit into [0, 24) range. After
63  /// initialization time span will be normalized.
64  TimeSpan(int hours, int minutes, int seconds, int nanoseconds = 0);
65 
66  /// Initializes with given set of values.
67  /// Input parameters are treated as quantities,
68  /// but not as a time stamp. Therefore, there's
69  /// no requirement to fit in a certain range like
70  /// hours must fit into [0, 24) range. After
71  /// initialization time span will be normalized.
72  TimeSpan(int days, int hours, int minutes, int seconds, int nanoseconds);
73 
74  /// Initializes time interval from total number
75  /// seconds and its fractional (nanosecond) part.
76  TimeSpan(long long totalSeconds, int nanoseconds);
77 
78  /// Initializes as clone of other instance.
79  TimeSpan(const TimeSpan& other);
80 
81  /// Whole number of seconds in time interval.
82  long long totalSeconds() const;
83 
84  /// Days component of time interval.
85  /// Whole number of days in time interval.
86  int days() const;
87 
88  /// Hours component of time interval.
89  /// Values are in range from -23 through 23.
90  int hours() const;
91 
92  /// Minutes component of time interval.
93  /// Values are in range from -59 through 59.
94  int minutes() const;
95 
96  /// Seconds component of time interval.
97  /// Values are in range from -59 through 59.
98  int seconds() const;
99 
100  /// Milliseconds component of time interval.
101  /// Values are in range from -999 through 999.
102  int milliseconds() const;
103 
104  /// Microseconds component of time interval.
105  /// Values are in range from -999999 through 999999.
106  int microseconds() const;
107 
108  /// Nanoseconds component of time interval.
109  /// Values are in range from -999999999 through 999999999.
110  int nanoseconds() const;
111 
112  /// Compares with other instance for equality.
113  bool operator ==(const TimeSpan& other) const;
114 
115  /// Compares with other instance for in-equality.
116  bool operator !=(const TimeSpan& other) const;
117 
118  /// Checks whether time interval less than other one.
119  bool operator <(const TimeSpan& other) const;
120 
121  /// Checks whether time interval greater than other one.
122  bool operator >(const TimeSpan& other) const;
123 
124  /// Adds time interval to current one.
125  TimeSpan& operator +=(const TimeSpan& other);
126 
127  /// Subtracts time interval from current one.
128  TimeSpan& operator -=(const TimeSpan& other);
129 
130  /// Re-assigns time interval from other one.
131  TimeSpan& operator =(const TimeSpan& other);
132 
133  /// Serializes time stamp into text presentation
134  /// using specified time-span presentation format.
135  void toString(std::string& str, TimeSpanFormat format = TimeSpanFormats::SDHHMMSSnsec) const;
136 
137  /// Serializes time stamp into text presentation
138  /// using specified time-span presentation format.
139  std::string toString(TimeSpanFormat format = TimeSpanFormats::SDHHMMSSnsec) const;
140 
141  /// De-serializes time interval from its text presentation.
142  static TimeSpan deserialize(const std::string& str);
143 
144  /// De-serializes time-span from presentation as it's used by the ICE.
145  static TimeSpan deserialize(unsigned long long presentation, TimeSpanFormat format);
146 
147  /// Time interval of zero length.
148  static const TimeSpan Zero;
149 
150 private:
151  long long seconds_;
152  int nanoseconds_;
153 };
154 
155 inline long long TimeSpan::totalSeconds() const
156 {
157  return seconds_;
158 }
159 
160 inline int TimeSpan::nanoseconds() const
161 {
162  return nanoseconds_;
163 }
164 
165 inline std::string TimeSpan::toString(TimeSpanFormat format) const
166 {
167  std::string str;
168 
169  toString(str, format);
170 
171  return str;
172 }
173 
174 /// Make it printable using C++ I/O streams.
175 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const TimeSpan&);
176 
177 /// Identifies months in year.
178 struct ONIXS_ICEMDH_EXPORT Months
179 {
180  enum Enum
181  {
183 
196 
197  Count = December
198  };
199 
200  /// Deserializes value from text presentation.
201  static Enum deserialize(const char*);
202 
203  /// Returns text presentation for given value.
204  static const char* toString(Enum);
205 };
206 
207 /// Identifies months in year.
209 
210 /// Identifies day within week.
211 struct ONIXS_ICEMDH_EXPORT DaysOfWeek
212 {
213  enum Enum
214  {
222 
223  Total
224  };
225 
226  /// Deserializes value from text presentation.
227  static Enum deserialize(const char*);
228 
229  /// Returns text presentation for given value.
230  static const char* toString(Enum);
231 };
232 
233 /// Identifies day within week.
235 
236 /// Represents month-year pair.
237 /// Year must fit into [0001, 9999] range.
238 /// Month must fit into [01, 12] range.
239 class ONIXS_ICEMDH_EXPORT YearMonth
240 {
241 public:
242  /// Initializes instance as Jan, 0001.
243  YearMonth();
244 
245  /// Initializes instance with given values.
246  /// Input parameters are checked for validness.
247  /// \throw exception if input parameters do
248  /// not fit into a valid range.
249  YearMonth(unsigned int year, Month month);
250 
251  /// Initializes as copy of other instance.
252  YearMonth(const YearMonth& other);
253 
254  /// Year component.
255  /// Valid range of values is [0001, 9999].
256  unsigned int year() const;
257 
258  /// Month component.
259  /// Valid range of values is [01, 12].
260  Month month() const;
261 
262  /// Compares with other instance for equality.
263  bool operator ==(const YearMonth&) const;
264 
265  /// Compares with other instance for inequality.
266  bool operator !=(const YearMonth&) const;
267 
268  // Re-initializes instance as copy of other one.
269  YearMonth& operator =(const YearMonth& other);
270 
271  /// Serializes into text (YYYYMM) presentation.
272  std::string toString() const;
273 
274  /// Serializes into text (YYYYMM) presentation.
275  void toString(std::string&) const;
276 
277  /// De-serializes instance from its numeric
278  /// presentation as it's used by ICE.
279  static YearMonth deserialize(unsigned long long);
280 
281 protected:
282  /// Suppresses verification.
283  struct NoVerify {};
284 
285  /// Initializes instance
286  /// without verifying input.
287  YearMonth(unsigned int, Month, const NoVerify&);
288 
289 private:
290  unsigned int year_;
291  Month month_;
292 };
293 
294 inline unsigned int YearMonth::year() const
295 {
296  return year_;
297 }
298 
299 inline Month YearMonth::month() const
300 {
301  return month_;
302 }
303 
304 inline std::string YearMonth::toString() const
305 {
306  std::string str;
307 
308  toString(str);
309 
310  return str;
311 }
312 
313 /// Make it printable using C++ I/O streams.
314 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const YearMonth&);
315 
316 /// Represents date without time component.
317 class ONIXS_ICEMDH_EXPORT Date : public YearMonth
318 {
319 public:
320  /// Initializes as Jan 1, 0001.
321  Date();
322 
323  /// Explicit initialization.
324  ///
325  /// \throw exception if input parameters
326  /// do not form valid date.
327  Date(unsigned int year, Month month, unsigned int day);
328 
329  /// Initializes as copy of other date.
330  Date(const Date& other);
331 
332  /// Day component of date.
333  /// Valid range of values is [01, 31].
334  unsigned int day() const;
335 
336  /// Compares with other for equality.
337  bool operator ==(const Date& other) const;
338 
339  /// Compares with other for inequality.
340  bool operator !=(const Date& other) const;
341 
342  /// Checks whether given date is less than other one.
343  bool operator <(const Date& other) const;
344 
345  /// Checks whether given date is greater than other one.
346  bool operator >(const Date& other) const;
347 
348  /// Adds time interval to the date.
349  Date& operator +=(TimeSpan& span);
350 
351  /// Subtracts time interval from the date.
352  Date& operator -=(TimeSpan& span);
353 
354  /// Re-initializes instance as copy of other one.
355  Date& operator =(const Date& other);
356 
357  /// Serializes date into YYYYMMDD presentation.
358  std::string toString() const;
359 
360  /// Serializes date into YYYYMMDD presentation.
361  void toString(std::string&) const;
362 
363  /// De-serializes date from its numeric
364  /// presentation as it's used by ICE.
365  static Date deserialize(unsigned long long);
366 
367 private:
368  unsigned int day_;
369 
370  /// Initializes without verifying input parameters.
371  Date(unsigned int year, Month month, unsigned int day, const NoVerify&);
372 };
373 
374 inline unsigned int Date::day() const
375 {
376  return day_;
377 }
378 
379 inline std::string Date::toString() const
380 {
381  std::string str;
382 
383  toString(str);
384 
385  return str;
386 }
387 
388 /// Calculates time interval between two given dates.
389 ONIXS_ICEMDH_EXPORT TimeSpan operator -(const Date& left, const Date& right);
390 
391 /// Make it printable using C++ I/O streams.
392 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const Date&);
393 
394 /// Collection of timestamp formats supported.
395 struct ONIXS_ICEMDH_EXPORT TimestampFormats
396 {
397  enum Enum
398  {
399  /// YYYYMMDD-HH:MM:SS.
401 
402  /// YYYYMMDD-HH:MM:SS.sss.
404 
405  /// YYYYMMDD-HH:MM:SS.sssssssss.
406  YYYYMMDDHHMMSSnsec
407  };
408 };
409 
410 /// Timestamp format.
412 
413 /// Represents timestamp without time-zone information.
414 class ONIXS_ICEMDH_EXPORT Timestamp
415 {
416 public:
417  /// Initializes as Jan 1, 0001, 00:00:00.
418  Timestamp();
419 
420  /// Initializes as date with zero time component.
421  ///
422  /// Input parameters are validated, therefore
423  /// constructor throws exception if input values
424  /// do not fit into their valid ranges.
425  Timestamp(unsigned year, Month month, unsigned day);
426 
427  /// Explicit timestamp initialization.
428  ///
429  /// Input parameters are validated, therefore
430  /// constructor throws exception if input values
431  /// do not fit into their valid ranges.
432  Timestamp(
433  unsigned year
434  , Month month
435  , unsigned day
436  , unsigned hour
437  , unsigned minute
438  , unsigned second
439  , unsigned nanosecond
440  );
441 
442  /// Initializes as copy of other instance.
443  Timestamp(const Timestamp& other);
444 
445  /// Initializes from time
446  /// interval since the Epoch.
447  Timestamp(const TimeSpan&);
448 
449  /// Year component of timestamp.
450  unsigned int year() const;
451 
452  /// Month component of timestamp.
453  Month month() const;
454 
455  /// Day component of timestamp.
456  unsigned int day() const;
457 
458  /// Hour component of timestamp.
459  unsigned int hour() const;
460 
461  /// Minute component of timestamp.
462  unsigned int minute() const;
463 
464  /// Second component of timestamp.
465  unsigned int second() const;
466 
467  /// Millisecond component of timestamp.
468  unsigned int millisecond() const;
469 
470  /// Microsecond component of timestamp.
471  unsigned int microsecond() const;
472 
473  /// Nanosecond component of timestamp.
474  unsigned int nanosecond() const;
475 
476  /// Returns timestamp without time part.
477  Timestamp date() const;
478 
479  /// Returns date component of timestamp.
480  void date(Date&) const;
481 
482  /// Return time part of timestamp.
483  TimeSpan time() const;
484 
485  /// Returns day of the week.
486  DayOfWeek dayOfWeek() const;
487 
488  /// Compares with other instance for equality.
489  bool operator ==(const Timestamp& other) const;
490 
491  /// Compares with other instance for inequality.
492  bool operator !=(const Timestamp& other) const;
493 
494  /// Checks whether timestamp is less than other one.
495  bool operator <(const Timestamp& other) const;
496 
497  /// Checks whether timestamp is greater than other one.
498  bool operator >(const Timestamp& other) const;
499 
500  /// Adds time interval to given timestamp.
501  Timestamp& operator +=(const TimeSpan& span);
502 
503  /// Subtracts time interval from given timestamp.
504  Timestamp& operator -=(const TimeSpan& span);
505 
506  /// Re-initializes as copy of other timestamp.
507  Timestamp& operator =(const Timestamp& other);
508 
509  /// Return timestamp that is current date
510  /// and time expressed as local time.
511  static Timestamp now();
512 
513  /// Return timestamp that is current date
514  /// and time expressed as UTC time.
515  static Timestamp utcNow();
516 
517  /// Returns text presentation of timestamp
518  /// using specified presentation format.
519  std::string toString(TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
520 
521  /// Returns text presentation of timestamp
522  /// using specified presentation format.
523  void toString(std::string& str, TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
524 
525  /// De-serializes timestamp from text presentation.
526  static Timestamp deserialize(const std::string&);
527 
528  /// De-serializes timestamp from its numeric
529  /// presentation as it's used by the ICE.
530  static Timestamp deserialize(unsigned long long presentation, TimestampFormat format);
531 
532 private:
533  friend ONIXS_ICEMDH_EXPORT TimeSpan operator -(const Timestamp& left, const Timestamp& right);
534 
535  /// Time interval since the Epoch.
536  TimeSpan sinceEpoch_;
537 };
538 
539 inline unsigned int Timestamp::hour() const
540 {
541  return static_cast<unsigned int>(sinceEpoch_.hours());
542 }
543 
544 inline unsigned int Timestamp::minute() const
545 {
546  return static_cast<unsigned int>(sinceEpoch_.minutes());
547 }
548 
549 inline unsigned int Timestamp::second() const
550 {
551  return static_cast<unsigned int>(sinceEpoch_.seconds());
552 }
553 
554 inline unsigned int Timestamp::millisecond() const
555 {
556  return static_cast<unsigned int>(sinceEpoch_.milliseconds());
557 }
558 
559 inline unsigned int Timestamp::microsecond() const
560 {
561  return static_cast<unsigned int>(sinceEpoch_.microseconds());
562 }
563 
564 inline unsigned int Timestamp::nanosecond() const
565 {
566  return static_cast<unsigned int>(sinceEpoch_.nanoseconds());
567 }
568 
569 inline std::string Timestamp::toString(TimestampFormat format) const
570 {
571  std::string str;
572 
573  toString(str, format);
574 
575  return str;
576 }
577 
578 /// Calculates time interval between two timestamps.
579 ONIXS_ICEMDH_EXPORT TimeSpan operator -(const Timestamp& left, const Timestamp& right);
580 
581 /// Make it printable using C++ I/O streams.
582 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const Timestamp&);
583 
584 }}}} // namespace MarketData, iMpact, ICE, OnixS
unsigned int nanosecond() const
Nanosecond component of timestamp.
Definition: Time.h:564
unsigned int second() const
Second component of timestamp.
Definition: Time.h:549
unsigned int microsecond() const
Microsecond component of timestamp.
Definition: Time.h:559
TimeSpan operator-(const Date &left, const Date &right)
Calculates time interval between two given dates.
DaysOfWeek::Enum DayOfWeek
Identifies day within week.
Definition: Time.h:234
Months::Enum Month
Identifies months in year.
Definition: Time.h:208
TimestampFormats::Enum TimestampFormat
Timestamp format.
Definition: Time.h:411
Identifies day within week.
Definition: Time.h:211
std::ostream & operator<<(std::ostream &, const Error &)
Make it printable to formatted C++ I/O streams.
static const TimeSpan Zero
Time interval of zero length.
Definition: Time.h:148
std::string toString(TimestampFormat format=TimestampFormats::YYYYMMDDHHMMSSnsec) const
Definition: Time.h:569
std::string toString() const
Serializes into text (YYYYMM) presentation.
Definition: Time.h:304
unsigned int year() const
Definition: Time.h:294
long long totalSeconds() const
Whole number of seconds in time interval.
Definition: Time.h:155
unsigned int hour() const
Hour component of timestamp.
Definition: Time.h:539
TimeSpanFormats::Enum TimeSpanFormat
Time span format.
Definition: Time.h:47
std::string toString() const
Serializes date into YYYYMMDD presentation.
Definition: Time.h:379
unsigned int millisecond() const
Millisecond component of timestamp.
Definition: Time.h:554
Identifies months in year.
Definition: Time.h:178
Time span formats supported.
Definition: Time.h:30
Represents date without time component.
Definition: Time.h:317
Enum
Time span formats supported.
Definition: Time.h:33
Represents timestamp without time-zone information.
Definition: Time.h:414
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
Collection of timestamp formats supported.
Definition: Time.h:395
unsigned int day() const
Definition: Time.h:374
unsigned int minute() const
Minute component of timestamp.
Definition: Time.h:544