Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSATime.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_TIME__INCLUDED
15 #define TSA_TIME__INCLUDED
16 
17 #include "TSATypeDef.h"
18 
19 #include <ctime>
20 #include <ostream>
21 #include <chrono>
22 #include <string>
23 
24 namespace tsa {
25  class holiday_list;
26  class out_storage_stream;
27  class in_storage_stream;
28  class date_time;
29  class variant;
30  class duration;
31  class string;
32  class date_time;
33 
40  utc_time = 0,
42  };
43 
48  };
49 
50  /*
51  ** ====================================================
52  ** >>> class time_of_day <<<
53  ** ====================================================
54  */
55 
57  class time_of_day {
58  private:
59  int64_t m_usec_since_midnight;
60  private:
61  template<class Archive>
62  void serialize(Archive & ar)
63  {
64  ar(m_usec_since_midnight);
65  }
66  public:
67  time_of_day(void);
68  time_of_day(uint32_t hour, uint32_t min, uint32_t sec = 0, uint32_t u_sec = 0);
69  time_of_day(const time_of_day&);
70  time_of_day(const std::string& time_str);
71  ~time_of_day(void);
72 
73  uint32_t hour(void)const;
74  uint32_t minute(void)const;
75  uint32_t second(void)const;
76  uint32_t usec(void)const;
77  uint32_t microsec(void)const;
78  int64_t microsec_since_midnight(void)const;
79  void microsec_since_midnight(int64_t usec_since_midnight);
80  std::string to_string(const char* format = "hh:mm:ss.f")const;
81  bool operator==(const time_of_day& other)const {
82  return m_usec_since_midnight == other.m_usec_since_midnight;
83  }
84  bool operator!=(const time_of_day& other)const {
85  return m_usec_since_midnight != other.m_usec_since_midnight;
86  }
87  bool operator<(const time_of_day& other)const {
88  return m_usec_since_midnight < other.m_usec_since_midnight;
89  }
90  bool operator>(const time_of_day& other)const {
91  return m_usec_since_midnight > other.m_usec_since_midnight;
92  }
93  bool operator<=(const time_of_day& other)const {
94  return m_usec_since_midnight <= other.m_usec_since_midnight;
95  }
96  bool operator>=(const time_of_day& other)const {
97  return m_usec_since_midnight >= other.m_usec_since_midnight;
98  }
99  };
100 
102  std::ostream& operator<<(std::ostream& stream, const time_of_day& td);
103 
104 
105  /*
106  ** ====================================================
107  ** >>> class date <<<
108  ** ====================================================
109  */
110 
119  class dll_export date {
120  tsa_declare_testable;
121  friend class variant;
122  protected:
123  int m_julian_day;
124  char m_time_zone_offset_half_hours;
125  public:
126  template<class Archive>
127  void serialize(Archive & ar)
128  {
129  ar(m_julian_day);
130  ar(m_time_zone_offset_half_hours);
131  }
132  protected:
133  static int ymd_to_jd(int y, int m, int d);
134  static void jd_to_jmd(int jd, int& y, int& m, int& d);
135  static int s_arr_days_in_month[12];
136  static std::string s_months_short_names[12];
137  static const std::string s_class_name;
138  date& add_days(int days);
139  date& subtract_days(int days);
140  int Subtract(const date other) const;
141  void to_year_month_day(int& y, int& m, int& d)const;
142  static date date_from_string__no_exception(const std::string& _str);
143  public:
145  enum week_day {
146  sun = 0, mon = 1, tue = 2, wed = 3, thu = 4, fri = 5, sat = 6
147  };
148 
150  enum Month {
151  jan = 1, feb = 2, mar = 3, apr = 4, may = 5, jun = 6, jul = 7,
152  aug = 8, sep = 9, oct = 10, nov = 11, dec = 12
153  };
154  protected:
155  static const int days_in_year;
156  static const int days_in_leap_year;
157  public:
159  static const uint32_t undefined;
160 
162  static const date min;
163 
165  static const date max;
166 
168  static const date gregorian_start;
169 
171  static const int min_julian_day;
172 
174  static const int max_julian_day;
175 
176  static const int undefined_julian_day;
177 
179  static const int min_yyyymmdd;
180 
182  static const int max_yyyymmdd;
183  public:
185  static bool is_leap_year(int year);
186 
188  date(const date& other);
189 
191  date(const char* date_str);
192 
194  date(const variant& variant);
195 
202  explicit date(uint32_t yyyymmdd = date::undefined);
203 
205  date(int year, int month, int day);
206 
208  date& operator= (const date& other);
209 
212  void assign(const date&, bool _error_on_undefined = true);
213 
215  void set_to_now(tsa::local_or_utc_time_select lu);
216 
218  void clear(void);
219 
221  void julian_day(int julian_day);
222 
224  int julian_day(void) const;
225  public:
226  struct bin_rep {
227  uint32_t part_a;
228  uint32_t part_b;
229  };
230  void reset_from_bin_rep(const bin_rep&);
231  bin_rep get_bin_rep(void) const;
232  public:
233  int GetJulianMonth(void) const;
234  int GetJulianQuarter(void) const;
235  int GetJulianSemester(void) const;
236  static int GetJulianMonth(int nJulDay);
237  static int GetJulianQuarter(int nJulDay);
238  static int GetJulianSemester(int nJulDay);
240  int year(void) const;
241 
243  int month(void) const;
244 
246  int day_of_month(void) const;
247 
249  int day_of_year(void) const;
250 
252  week_day day_of_week(void) const;
253 
255  bool is_weekend(void) const;
256 
258  void yyyymmdd(uint32_t yyyymmdd);
259 
261  uint32_t yyyymmdd(void) const;
262 
282  std::string to_string(const char* format) const;
283 
285  std::string to_string(void)const;
286  public:
287 
288  private:
289  // deprecated:
290  void set_old(const std::string& date_string, const char* format_string);
291  public:
292 
312  void set_from_string(const std::string& date_string, const char* format_string = "");
313 
319  void set_from_iso_string(const std::string& iso_date_string);
320 
322  void set(uint32_t year, uint32_t month, uint32_t day);
323 
325  bool in_leap_year(void) const;
326 
327  static bool _IsLeapYear(int year);
328 
336  static bool is_valid(int year, int month, int day);
337  bool is_valid(void) const;
338  bool not_defined(void) const;
340  bool defined(void) const;
341  void verify_defined(void) const;
342 
344  friend date operator+ (const date& date, const duration& num_days);
345 
347  friend date operator- (const date& Left, const duration& num_days);
348 
350  void operator+= (const duration& num_days);
351 
353  void operator-= (const duration& num_days);
354 
355  int ApproxNumBusinessDaysBetween(const date&) const;
356  date& operator++();
357  date operator++ (int);
358  date& operator--();
359  date operator-- (int);
360 
362  void incr_to_next_weekday(void);
363 
365  void decr_to_prev_weekday(void);
366 
368  void incr(int num_days);
369 
371  void decr(int num_days);
372  public:
374  bool operator== (const date& other) const;
375 
377  bool operator!= (const date& other) const;
378 
380  bool operator< (const date& other) const;
381 
383  bool operator<= (const date& other) const;
384 
386  bool operator> (const date& other) const;
387 
389  bool operator>= (const date& other) const;
390 
391  time_t GetAsSystemTime(local_or_utc_time_select lu = tsa::local_time) const;
392 public:
393  void WriteState(out_storage_stream& stream) const; //soon to be obsolete
394  void ReadState(in_storage_stream& stream);
395  void raise_parse_error(const std::string& _str, const std::string& _format) const;
396  public:
397  friend std::ostream& operator<< (std::ostream&, const date& date);
398  };
399 
407  date operator+ (const date& date, const duration& num_days);
408 
411  date operator- (const date& Left, const duration& numDays);
412 
414  std::ostream& operator<< (std::ostream& stream, const date& date);
415 
416 
417 
418  /*
419  ** ====================================================
420  ** >>> class date_time <<<
421  ** ====================================================
422  */
423 
428  class dll_export date_time : public date {
429  tsa_declare_testable;
430  friend class variant;
431  static const std::string s_class_name;
432  public:
434  static const int64_t microseconds_in_minute;
435 
437  static const int64_t microseconds_in_hour;
438 
440  static const int64_t microseconds_in_day;
441 
443  static const int64_t last_microsecond_in_day;
444 
446  static const int64_t microseconds_in_second;
447 
449  static const date_time min;
450 
452  static const date_time max;
453  public:
454  static const char undefined_timezone_offset;
455 
457  static const uint32_t undefined;
458 
460  static const uint32_t now_local;
461 
463  static const uint32_t now_utc;
464  private:
465  int64_t m_usec_since_midnight;
466  date_time& add_microsecs(int64_t);
467  date_time& subtract_microsecs(int64_t);
468  void get_hr_min_sec(uint32_t* hour_ptr, uint32_t* minute_ptr, uint32_t* second_ptr)const;
469  string _IntToFixedLengthString(int num, size_t len) const;
470  public:
471  template<class Archive>
472  void serialize(Archive & ar)
473  {
474  ar(m_julian_day);
475  ar(m_time_zone_offset_half_hours);
476  ar(m_usec_since_midnight);
477  }
478  public:
479 
487  explicit date_time(uint32_t yyyymmdd = date_time::undefined);
488 
490  date_time(const date_time& other);
491  public:
493  explicit date_time(const std::chrono::time_point<std::chrono::system_clock>& time_point) {
494  set(time_point);
495  }
496  public:
497 
511  date_time(const char* date_time_str);
512 
516  void verify_defined(void) const;
517 
518  // @brief Sets @b self to an 'undefined' state. defined() will thereafter return false.
519  void set_to_null(void);
520 
522  bool has_time_zone_offset(void)const;
523 
525  int timezone_half_hour_offset(void) const;
526 
528  void timezone_half_hour_offset(int);
529 
531  double timezone_offset(void)const;
532 
534  void timezone_offset(double hours);
535 
536  // internal
537  char convert_hour_offset_to_half_hour_offset(double hours);
538 
540  date_time convert_to_utc(void)const;
541 
543  date_time convert_to_time_zone_offset(double new_offset)const;
544 
546  static date_time system_time(tsa::local_or_utc_time_select local_or_utc,
547  set_timezone_offset_select set_tz_offset_flag);
548 
550  static date_time now(void) { return date_time::system_time(tsa::local_time, no_timezone_offset); }
551 
553  static date_time local_time(void) { return date_time::system_time(tsa::local_time, no_timezone_offset); }
554 
557  { return date_time::system_time(tsa::utc_time, set_tz_offset_flag); }
558 
568  void assign(const tsa::date_time& other, bool error_on_nil = true);
569 
571  date_time& operator= (const tsa::date_time&);
572 
574  explicit date_time(const date& date);
575 
577  date_time(uint32_t year, uint32_t month, uint32_t day);
578 
580  date_time(const date& date, uint32_t hour, uint32_t minute, uint32_t second = 0);
581 
583  date_time(uint32_t year, uint32_t month, uint32_t day,
584  uint32_t hour, uint32_t minute, uint32_t second = 0, uint32_t microseconds = 0);
585 
587  date_time(const date& date, uint32_t hour,
588  uint32_t minute, uint32_t second, uint32_t microseconds);
589 
596  date_time(const variant& variant);
597 
603  time_t as_system_time(local_or_utc_time_select local_or_utc = tsa::local_time) const;
604 
611  std::chrono::system_clock::time_point as_time_point(local_or_utc_time_select local_or_utc);
612 
620  void set(const std::chrono::system_clock::time_point& time_pt,
622 
623  //template<typename CLOCK>
624  //void set(const std::chrono::time_point<CLOCK>& _time_pt, local_or_utc_time_select _lu = tsa::local_time) {
625  // //time_t tm = CLOCK::to_time_t(_time_pt);
626  // //time_t tm = std::chrono::system_clock::to_time_t(_time_pt);
627  // //set_time_t(tm, _lu);
628 
629  //}
630 
637  void set_time_t(time_t system_time, local_or_utc_time_select local_or_utc,
639 
645  bool defined(void) const;
646 
650  void clear(void);
651  public:
652  tsa::time_of_day time_of_day(void)const;
653  public:
654  static bool is_valid(int hour, int minute, int second, int millisecond);
655  bool is_valid(void) const;
656  bool not_defined(void) const;
657  public:
658  void reset_from_bin_rep(const bin_rep&);
659  bin_rep get_bin_rep(void) const;
660  public:
662  uint32_t hhmmssmmm(void) const;
663 
665  uint64_t hhmmssmmmmmm(void) const;
666 
668  uint32_t hhmmss(void) const;
669 
671  uint32_t hhmm(void) const;
672 
674  int64_t microsec_since_midnight(void) const;
675 
677  void microsec_since_midnight(int64_t);
678 
681  bool is_midnight(void) const;
682 
684  uint32_t hour(void) const;
685 
687  uint32_t minute(void) const;
688 
690  uint32_t second(void) const;
691 
693  uint32_t microsec(void) const;
694 
696  uint32_t usec(void) const;
697 
698  // Sets the time-of-day from an integer.
699  void set_hhmmssmmmmmm(uint64_t hhmmssmmmmmm);
700 
702  void set_hhmmss(uint32_t hhmmss);
703 
705  void set_hhmm(uint32_t hhmm);
706 
708  void set_time(uint32_t hour, uint32_t minute, uint32_t second, uint32_t microsecond);
709 
711  void set_time(uint32_t hour, uint32_t minute, uint32_t second);
712 
714  void set_time(uint32_t hour, uint32_t minute);
715 
721  void set_to_now(local_or_utc_time_select local_or_utc_flag,
722  set_timezone_offset_select set_tz_offset_flag = no_timezone_offset);
723 
738  std::string to_time_string(const char* format = "hh:mm:ss.f") const;
739 
750  std::string to_string(const char* date_format, const char* time_format, char sep) const;
751 
759  std::string to_string(char sep = 'T')const;
760  public:
761 
795  void set_from_string(const std::string& date_time_string,
796  const char* date_format = nullptr, char sep = 'T');
797 
806  void set_from_iso_string(const std::string& date_time_string);
807 
808  //internal
809  string GetReportDateTimeString(const char* dateFormat = "yyyy-mmm-dd");
810 
812  void set_date(uint32_t year, uint32_t month, uint32_t day);
813 
815  void set_date(const date&);
816 
823  void set_date_from_string(const std::string& date_string, const char* date_format);
824 
838  void set_time_from_string(const std::string& time_string);
839  private:
840  void set_string_time__check(const std::string& time_string, const char* format = "hh:mm:ss.f");
841  public:
842  void verify_both_have_timezone(const date_time& a, const date_time& b)const;
843 
845  bool operator== (const date_time& other) const;
846 
848  bool operator!= (const date_time& other) const;
849 
851  bool operator< (const date_time& other) const;
852 
854  bool operator<= (const date_time& other) const;
855 
857  bool operator> (const date_time& other) const;
858 
860  bool operator>= (const date_time& other) const;
861 
863  void incr_date(int num_days = 1);
864 
866  void incr_microsec(int64_t n = 1);
867 
869  void decr_microsec(int64_t n = 1);
870  public:
871  void round_down_to_minute(void);
872  void round_down_to_second(void);
873  public:
874  static clock_t clock_cycles(void);
875  void write_state(out_storage_stream&) const; //soon to be obsolete
876  void read_state(in_storage_stream&);
877  //void write(std::ostream&) const;
878  //void read(std::istream&);
879  friend std::ostream& operator<< (std::ostream&, const date_time&);
880 
881  private://hide these members inherited from date (to avoid confusion)
882  date& operator++();
883  date operator++ (int);
884  date& operator--();
885  date operator-- (int);
886  void incr(void);
887  void incr(int days);
888  void decr(int days);
889  public:
891  void operator+= (const duration& duration);
893  void operator-= (const duration& duration);
894  };
895 
897  typedef date_time time_point;
898 
908  std::ostream& operator<< (std::ostream& stream, const date_time& datetime);
909 
917  date_time min(const date_time& left, const date_time& right);
918 
926  date_time max(const date_time& left, const date_time& right);
927 
928 
929 
930 
931  /*
932  ** ====================================================
933  ** >>> class duration <<<
934  ** ====================================================
935  */
936 
945  class dll_export duration {
946  int m_24_hour_periods;
947  int64_t m_microseconds;
948  static const std::string s_class_name;
949  public:
950  template<class Archive>
951  void serialize(Archive & ar)
952  {
953  ar(m_24_hour_periods);
954  ar(m_microseconds);
955  }
956  public:
958  static duration days(uint64_t);
960  static duration hours(uint64_t);
962  static duration minutes(uint64_t);
964  static duration seconds(uint64_t);
966  static duration milliseconds(uint64_t);
968  static duration microseconds(uint64_t);
969  public:
970 
978  duration(uint64_t num_24h_periods, uint64_t microsec_of_day);
980  duration(double decimal_24hour_periods);
982  duration(const duration&);
983 
988  duration(void);
990  duration(const date_time& a, const date_time& b);
992  bool operator== (const duration& other) const;
994  int get_days(void) const;
995 
1000  int64_t get_microseconds(void) const;
1001 
1007  bool is_zero()const;
1008  public:
1009 
1010  int64_t to_microseconds(void)const;
1011  int64_t to_seconds(void)const;
1012 
1017  double to_fractional_days(void) const;
1018 
1023  double to_fractional_years(void) const;
1024  public:
1026  void operator+= (const duration& other);
1028  void operator= (const duration&);
1030  bool operator< (const duration& rhs);
1032  bool operator> (const duration& rhs);
1034  bool operator<= (const duration& rhs);
1036  bool operator>= (const duration& rhs);
1038  duration operator/ (double num) const;
1040  duration operator* (double multiplier) const;
1041 
1042  public:
1043  void WriteState(out_storage_stream&) const;
1044  void ReadState(in_storage_stream&);
1045  };
1046 
1048  std::ostream& operator<< (std::ostream& stream, const duration& ts);
1049 
1051  duration operator+ (const duration& a, const duration& b);
1053  date_time operator+ (const date_time& a, const duration& b);
1055  date_time operator- (const date_time& a, const duration& b);
1057  duration operator- (const date_time& a, const date_time& b);
1058 
1060 }// namespace tsa
1061 
1062 #endif
static const int64_t last_microsecond_in_day
Constant representing the number of microseconds in a day.
Definition: TSATime.h:443
static date_time utc_time(set_timezone_offset_select set_tz_offset_flag=no_timezone_offset)
Returns the current system date-time as &#39;universal time coordinate&#39; time.
Definition: TSATime.h:556
double max(const series< double > &series, size_t period)
Returns the highest value found in series over given .
Definition: TSAFunctionsScalar.cpp:180
static const int64_t microseconds_in_minute
Constant representing the number of microseconds in a minute.
Definition: TSATime.h:434
static const int max_julian_day
Largest valid julian day.
Definition: TSATime.h:174
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
static const int64_t microseconds_in_second
Constant representing the number of microseconds in a second.
Definition: TSATime.h:446
sref< double > operator*(numeric_series_cref a, numeric_series_cref b)
Multiplies a by b.
Definition: TSADSLBase.cpp:42
static const date gregorian_start
Gregorian calendar start date (of this implementation - Jan 1 1753).
Definition: TSATime.h:168
static const date max
Largest valid date (dec 31 99999AD).
Definition: TSATime.h:165
Definition: TSATime.h:46
static const int64_t microseconds_in_hour
Constant representing the number of microseconds in an hour.
Definition: TSATime.h:437
static const uint32_t now_utc
Constructor flag to initialize self to the current system&#39;s &#39;UTC time&#39;.
Definition: TSATime.h:463
static const int64_t microseconds_in_day
Constant representing the number of microseconds in a day.
Definition: TSATime.h:440
static const int min_yyyymmdd
Smallest valid yyyymmdd.
Definition: TSATime.h:179
Definition: TSATime.h:47
sref< bool > operator>(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:104
static const date_time min
Constant representing the smallest allowed date_time.
Definition: TSATime.h:449
variant objects can represent values of different types.
Definition: TSAVariant.h:140
week_day
Values that represent week days.
Definition: TSATime.h:145
Represents a duration - the difference between two date_time values.
Definition: TSATime.h:945
std::ostream & operator<<(std::ostream &s, const transaction &t)
Writes human readable transaction information to stream.
Definition: TSAOrder.cpp:779
local_or_utc_time_select
Flag to set time to either &#39;local time&#39; or &#39;coordinate universal time&#39;.
Definition: TSATime.h:39
static const date_time max
Constant representing the largest allowed date_time.
Definition: TSATime.h:452
static const int min_julian_day
Smallest valid julian day.
Definition: TSATime.h:171
sref< double > operator+(numeric_series_cref a, numeric_series_cref b)
Adds a and b.
Definition: TSADSLBase.cpp:32
sref< bool > operator<=(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:91
A date of the Gregorian calendar.
Definition: TSATime.h:119
sref< bool > operator>=(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:117
double min(const series< double > &series, size_t period)
Returns the smallest value found in series over given .
Definition: TSAFunctionsScalar.cpp:218
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
static const uint32_t undefined
Constructor flag to initilize self to a not-initialized state.
Definition: TSATime.h:457
static const date min
Smallest valid date ( jan 1 100AD).
Definition: TSATime.h:162
static const int max_yyyymmdd
Largest valid yyyymmdd.
Definition: TSATime.h:182
set_timezone_offset_select
Values that represent set timezone offset selects.
Definition: TSATime.h:45
sref< double > operator/(numeric_series_cref a, numeric_series_cref b)
Divides a by b.
Definition: TSADSLBase.cpp:47
static date_time now(void)
Returns the current system date-time.
Definition: TSATime.h:550
static const uint32_t undefined
Constructor flag to instruct self to stay undefined (see defined()).
Definition: TSATime.h:159
static const uint32_t now_local
Constructor flag to initialize self to the current system&#39;s &#39;local time&#39;.
Definition: TSATime.h:460
static date_time local_time(void)
Returns the current system date-time. Same as now();.
Definition: TSATime.h:553
Definition: TSATime.h:40
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
date_time(const std::chrono::time_point< std::chrono::system_clock > &time_point)
Constructor. Translates given time_point as local-time.
Definition: TSATime.h:493
sref< double > operator-(numeric_series_cref a, numeric_series_cref b)
Subtracts b from a.
Definition: TSADSLBase.cpp:37
Definition: TSATime.h:41
Month
Values that represent months.
Definition: TSATime.h:150
sref< bool > operator<(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:78