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