OnixS ICE iMpact Multicast Price Feed Handler C++ library  8.18.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  };
286 
287  /// Initializes instance
288  /// without verifying input.
289  YearMonth(unsigned int, Month, const NoVerify&);
290 
291 private:
292  unsigned int year_;
293  Month month_;
294 };
295 
296 inline unsigned int YearMonth::year() const
297 {
298  return year_;
299 }
300 
301 inline Month YearMonth::month() const
302 {
303  return month_;
304 }
305 
306 inline std::string YearMonth::toString() const
307 {
308  std::string str;
309 
310  toString(str);
311 
312  return str;
313 }
314 
315 /// Make it printable using C++ I/O streams.
316 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const YearMonth&);
317 
318 /// Represents date without time component.
319 class ONIXS_ICEMDH_EXPORT Date : public YearMonth
320 {
321 public:
322  /// Initializes as Jan 1, 0001.
323  Date();
324 
325  /// Explicit initialization.
326  ///
327  /// \throw exception if input parameters
328  /// do not form valid date.
329  Date(unsigned int year, Month month, unsigned int day);
330 
331  /// Initializes as copy of other date.
332  Date(const Date& other);
333 
334  /// Day component of date.
335  /// Valid range of values is [01, 31].
336  unsigned int day() const;
337 
338  /// Compares with other for equality.
339  bool operator==(const Date& other) const;
340 
341  /// Compares with other for inequality.
342  bool operator!=(const Date& other) const;
343 
344  /// Checks whether given date is less than other one.
345  bool operator<(const Date& other) const;
346 
347  /// Checks whether given date is greater than other one.
348  bool operator>(const Date& other) const;
349 
350  /// Adds time interval to the date.
351  Date& operator+=(TimeSpan& span);
352 
353  /// Subtracts time interval from the date.
354  Date& operator-=(TimeSpan& span);
355 
356  /// Re-initializes instance as copy of other one.
357  Date& operator=(const Date& other);
358 
359  /// Serializes date into YYYYMMDD presentation.
360  std::string toString() const;
361 
362  /// Serializes date into YYYYMMDD presentation.
363  void toString(std::string&) const;
364 
365  /// De-serializes date from its numeric
366  /// presentation as it's used by ICE.
367  static Date deserialize(unsigned long long);
368 
369 private:
370  unsigned int day_;
371 
372  /// Initializes without verifying input parameters.
373  Date(unsigned int year, Month month, unsigned int day, const NoVerify&);
374 };
375 
376 inline unsigned int Date::day() const
377 {
378  return day_;
379 }
380 
381 inline std::string Date::toString() const
382 {
383  std::string str;
384 
385  toString(str);
386 
387  return str;
388 }
389 
390 /// Calculates time interval between two given dates.
391 ONIXS_ICEMDH_EXPORT TimeSpan operator-(const Date& left, const Date& right);
392 
393 /// Make it printable using C++ I/O streams.
394 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const Date&);
395 
396 /// Collection of timestamp formats supported.
397 struct ONIXS_ICEMDH_EXPORT TimestampFormats
398 {
399  enum Enum
400  {
401  /// YYYYMMDD-HH:MM:SS.
403 
404  /// YYYYMMDD-HH:MM:SS.sss.
406 
407  /// YYYYMMDD-HH:MM:SS.sssssssss.
408  YYYYMMDDHHMMSSnsec
409  };
410 };
411 
412 /// Timestamp format.
414 
415 /// Represents timestamp without time-zone information.
416 class ONIXS_ICEMDH_EXPORT Timestamp
417 {
418 public:
419  /// Initializes as Jan 1, 0001, 00:00:00.
420  Timestamp();
421 
422  /// Initializes as date with zero time component.
423  ///
424  /// Input parameters are validated, therefore
425  /// constructor throws exception if input values
426  /// do not fit into their valid ranges.
427  Timestamp(unsigned year, Month month, unsigned day);
428 
429  /// Explicit timestamp initialization.
430  ///
431  /// Input parameters are validated, therefore
432  /// constructor throws exception if input values
433  /// do not fit into their valid ranges.
434  Timestamp(
435  unsigned year,
436  Month month,
437  unsigned day,
438  unsigned hour,
439  unsigned minute,
440  unsigned second,
441  unsigned nanosecond
442  );
443 
444  /// Initializes as copy of other instance.
445  Timestamp(const Timestamp& other);
446 
447  /// Initializes from time
448  /// interval since the Epoch.
449  Timestamp(const TimeSpan&);
450 
451  /// Year component of timestamp.
452  unsigned int year() const;
453 
454  /// Month component of timestamp.
455  Month month() const;
456 
457  /// Day component of timestamp.
458  unsigned int day() const;
459 
460  /// Hour component of timestamp.
461  unsigned int hour() const;
462 
463  /// Minute component of timestamp.
464  unsigned int minute() const;
465 
466  /// Second component of timestamp.
467  unsigned int second() const;
468 
469  /// Millisecond component of timestamp.
470  unsigned int millisecond() const;
471 
472  /// Microsecond component of timestamp.
473  unsigned int microsecond() const;
474 
475  /// Nanosecond component of timestamp.
476  unsigned int nanosecond() const;
477 
478  /// Returns timestamp without time part.
479  Timestamp date() const;
480 
481  /// Returns date component of timestamp.
482  void date(Date&) const;
483 
484  /// Return time part of timestamp.
485  TimeSpan time() const;
486 
487  /// Returns day of the week.
488  DayOfWeek dayOfWeek() const;
489 
490  /// Compares with other instance for equality.
491  bool operator==(const Timestamp& other) const;
492 
493  /// Compares with other instance for inequality.
494  bool operator!=(const Timestamp& other) const;
495 
496  /// Checks whether timestamp is less than other one.
497  bool operator<(const Timestamp& other) const;
498 
499  /// Checks whether timestamp is greater than other one.
500  bool operator>(const Timestamp& other) const;
501 
502  /// Adds time interval to given timestamp.
503  Timestamp& operator+=(const TimeSpan& span);
504 
505  /// Subtracts time interval from given timestamp.
506  Timestamp& operator-=(const TimeSpan& span);
507 
508  /// Re-initializes as copy of other timestamp.
509  Timestamp& operator=(const Timestamp& other);
510 
511  /// Return timestamp that is current date
512  /// and time expressed as local time.
513  static Timestamp now();
514 
515  /// Return timestamp that is current date
516  /// and time expressed as UTC time.
517  static Timestamp utcNow();
518 
519  /// Returns text presentation of timestamp
520  /// using specified presentation format.
521  std::string toString(TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
522 
523  /// Returns text presentation of timestamp
524  /// using specified presentation format.
525  void toString(std::string& str, TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
526 
527  /// De-serializes timestamp from text presentation.
528  static Timestamp deserialize(const std::string&);
529 
530  /// De-serializes timestamp from its numeric
531  /// presentation as it's used by the ICE.
532  static Timestamp deserialize(unsigned long long presentation, TimestampFormat format);
533 
534 private:
535  friend ONIXS_ICEMDH_EXPORT TimeSpan operator-(const Timestamp& left, const Timestamp& right);
536 
537  /// Time interval since the Epoch.
538  TimeSpan sinceEpoch_;
539 };
540 
541 inline unsigned int Timestamp::hour() const
542 {
543  return static_cast<unsigned int>(sinceEpoch_.hours());
544 }
545 
546 inline unsigned int Timestamp::minute() const
547 {
548  return static_cast<unsigned int>(sinceEpoch_.minutes());
549 }
550 
551 inline unsigned int Timestamp::second() const
552 {
553  return static_cast<unsigned int>(sinceEpoch_.seconds());
554 }
555 
556 inline unsigned int Timestamp::millisecond() const
557 {
558  return static_cast<unsigned int>(sinceEpoch_.milliseconds());
559 }
560 
561 inline unsigned int Timestamp::microsecond() const
562 {
563  return static_cast<unsigned int>(sinceEpoch_.microseconds());
564 }
565 
566 inline unsigned int Timestamp::nanosecond() const
567 {
568  return static_cast<unsigned int>(sinceEpoch_.nanoseconds());
569 }
570 
571 inline std::string Timestamp::toString(TimestampFormat format) const
572 {
573  std::string str;
574 
575  toString(str, format);
576 
577  return str;
578 }
579 
580 /// Calculates time interval between two timestamps.
581 ONIXS_ICEMDH_EXPORT TimeSpan operator-(const Timestamp& left, const Timestamp& right);
582 
583 /// Make it printable using C++ I/O streams.
584 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream&, const Timestamp&);
585 
586 }}}} // namespace OnixS::ICE::iMpact::MarketData
unsigned int nanosecond() const
Nanosecond component of timestamp.
Definition: Time.h:566
unsigned int second() const
Second component of timestamp.
Definition: Time.h:551
unsigned int microsecond() const
Microsecond component of timestamp.
Definition: Time.h:561
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:413
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:571
std::string toString() const
Serializes into text (YYYYMM) presentation.
Definition: Time.h:306
unsigned int year() const
Definition: Time.h:296
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:541
TimeSpanFormats::Enum TimeSpanFormat
Time span format.
Definition: Time.h:47
std::string toString() const
Serializes date into YYYYMMDD presentation.
Definition: Time.h:381
unsigned int millisecond() const
Millisecond component of timestamp.
Definition: Time.h:556
Identifies months in year.
Definition: Time.h:178
Time span formats supported.
Definition: Time.h:30
Represents date without time component.
Definition: Time.h:319
Enum
Time span formats supported.
Definition: Time.h:33
Represents timestamp without time-zone information.
Definition: Time.h:416
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
Collection of timestamp formats supported.
Definition: Time.h:397
unsigned int day() const
Definition: Time.h:376
unsigned int minute() const
Minute component of timestamp.
Definition: Time.h:546