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