OnixS BME SENAF Handler C++ library  2.3.0
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 
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 /// Represents date without time component.
314 class ONIXS_BME_SENAF_EXPORT Date : public YearMonth
315 {
316 public:
317  /// Initializes as Jan 1, 0001.
318  Date();
319 
320  /// Explicit initialization.
321  ///
322  /// \throw exception if input parameters
323  /// do not form valid date.
324  Date(unsigned int year, Month month, unsigned int day);
325 
326  /// Initializes as copy of other date.
327  Date(const Date& other);
328 
329  /// Day component of date.
330  /// Valid range of values is [01, 31].
331  unsigned int day() const;
332 
333  /// Compares with other for equality.
334  bool operator==(const Date& other) const;
335 
336  /// Compares with other for inequality.
337  bool operator!=(const Date& other) const;
338 
339  /// Checks whether given date is less than other one.
340  bool operator<(const Date& other) const;
341 
342  /// Checks whether given date is greater than other one.
343  bool operator>(const Date& other) const;
344 
345  /// Adds time interval to the date.
346  Date& operator+=(TimeSpan& span);
347 
348  /// Subtracts time interval from the date.
349  Date& operator-=(TimeSpan& span);
350 
351  /// Re-initializes instance as copy of other one.
352  Date& operator=(const Date& other);
353 
354  /// Serializes date into YYYYMMDD presentation.
355  std::string toString() const;
356 
357  /// Serializes date into YYYYMMDD presentation.
358  void toString(std::string&) const;
359 
360  /// De-serializes date from its numeric
361  /// presentation as it's used by Senaf.
362  static Date deserialize(unsigned long long);
363 
364 private:
365  unsigned int day_;
366 
367  /// Initializes without verifying input parameters.
368  Date(unsigned int year, Month month, unsigned int day, const NoVerify&);
369 };
370 
371 inline unsigned int Date::day() const
372 {
373  return day_;
374 }
375 
376 inline std::string Date::toString() const
377 {
378  std::string str;
379 
380  toString(str);
381 
382  return str;
383 }
384 
385 /// Calculates time interval between two given dates.
386 ONIXS_BME_SENAF_EXPORT TimeSpan operator-(const Date& left, const Date& right);
387 
388 /// Collection of timestamp formats supported.
389 struct ONIXS_BME_SENAF_EXPORT TimestampFormats
390 {
391  enum Enum
392  {
393  /// YYYYMMDD-HH:MM:SS.
395 
396  /// YYYYMMDD-HH:MM:SS.sss.
398 
399  /// YYYYMMDD-HH:MM:SS.sssssssss.
400  YYYYMMDDHHMMSSnsec
401  };
402 };
403 
404 /// Timestamp format.
406 
407 /// Represents timestamp without time-zone information.
408 class ONIXS_BME_SENAF_EXPORT Timestamp
409 {
410 public:
411  /// Initializes as Jan 1, 0001, 00:00:00.
412  Timestamp();
413 
414  /// Initializes as date with zero time component.
415  ///
416  /// Input parameters are validated, therefore
417  /// constructor throws exception if input values
418  /// do not fit into their valid ranges.
419  Timestamp(unsigned year, Month month, unsigned day);
420 
421  /// Explicit timestamp initialization.
422  ///
423  /// Input parameters are validated, therefore
424  /// constructor throws exception if input values
425  /// do not fit into their valid ranges.
426  Timestamp(
427  unsigned year,
428  Month month,
429  unsigned day,
430  unsigned hour,
431  unsigned minute,
432  unsigned second,
433  unsigned nanosecond
434  );
435 
436  /// Initializes as copy of other instance.
437  Timestamp(const Timestamp& other);
438 
439  /// Initializes from time
440  /// interval since the Epoch.
441  Timestamp(const TimeSpan&);
442 
443  /// Year component of timestamp.
444  unsigned int year() const;
445 
446  /// Month component of timestamp.
447  Month month() const;
448 
449  /// Day component of timestamp.
450  unsigned int day() const;
451 
452  /// Hour component of timestamp.
453  unsigned int hour() const;
454 
455  /// Minute component of timestamp.
456  unsigned int minute() const;
457 
458  /// Second component of timestamp.
459  unsigned int second() const;
460 
461  /// Millisecond component of timestamp.
462  unsigned int millisecond() const;
463 
464  /// Microsecond component of timestamp.
465  unsigned int microsecond() const;
466 
467  /// Nanosecond component of timestamp.
468  unsigned int nanosecond() const;
469 
470  /// Returns timestamp without time part.
471  Timestamp date() const;
472 
473  /// Returns date component of timestamp.
474  void date(Date&) const;
475 
476  /// Return time part of timestamp.
477  TimeSpan time() const;
478 
479  /// Returns day of the week.
480  DayOfWeek dayOfWeek() const;
481 
482  /// Compares with other instance for equality.
483  bool operator==(const Timestamp& other) const;
484 
485  /// Compares with other instance for inequality.
486  bool operator!=(const Timestamp& other) const;
487 
488  /// Checks whether timestamp is less than other one.
489  bool operator<(const Timestamp& other) const;
490 
491  /// Checks whether timestamp is greater than other one.
492  bool operator>(const Timestamp& other) const;
493 
494  /// Adds time interval to given timestamp.
495  Timestamp& operator+=(const TimeSpan& span);
496 
497  /// Subtracts time interval from given timestamp.
498  Timestamp& operator-=(const TimeSpan& span);
499 
500  /// Re-initializes as copy of other timestamp.
501  Timestamp& operator=(const Timestamp& other);
502 
503  /// Return timestamp that is current date
504  /// and time expressed as local time.
505  static Timestamp now();
506 
507  /// Return timestamp that is current date
508  /// and time expressed as UTC time.
509  static Timestamp utcNow();
510 
511  /// Returns text presentation of timestamp
512  /// using specified presentation format.
513  std::string toString(TimestampFormat format = TimestampFormats::YYYYMMDDHHMMSSnsec) const;
514 
515  /// Returns text presentation of timestamp
516  /// using specified presentation format.
517  void 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:314
std::string toString() const
Serializes into text (YYYYMM) presentation.
Definition: Time.h:304
Enum
Time span formats supported.
Definition: Time.h:34
std::string toString() const
Serializes date into YYYYMMDD presentation.
Definition: Time.h:376
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:371
Represents timestamp without time-zone information.
Definition: Time.h:408
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:294
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:405
unsigned int minute() const
Minute component of timestamp.
Definition: Time.h:538
Collection of timestamp formats supported.
Definition: Time.h:389
unsigned int hour() const
Hour component of timestamp.
Definition: Time.h:533