time.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef DATE_TIME_TIME_HPP___
  2. #define DATE_TIME_TIME_HPP___
  3. /* Copyright (c) 2002,2003,2005,2020 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. /*! @file time.hpp
  11. This file contains the interface for the time associated classes.
  12. */
  13. #include <string>
  14. #include <boost/operators.hpp>
  15. #include <boost/date_time/time_defs.hpp>
  16. #include <boost/date_time/special_defs.hpp>
  17. namespace boost {
  18. namespace date_time {
  19. //! Representation of a precise moment in time, including the date.
  20. /*!
  21. This class is a skeleton for the interface of a temporal type
  22. with a resolution that is higher than a day. It is intended that
  23. this class be the base class and that the actual time
  24. class be derived using the BN pattern. In this way, the derived
  25. class can make decisions such as 'should there be a default constructor'
  26. and what should it set its value to, should there be optional constructors
  27. say allowing only an time_durations that generate a time from a clock,etc.
  28. So, in fact multiple time types can be created for a time_system with
  29. different construction policies, and all of them can perform basic
  30. operations by only writing a copy constructor. Finally, compiler
  31. errors are also shorter.
  32. The real behavior of the time class is provided by the time_system
  33. template parameter. This class must provide all the logic
  34. for addition, subtraction, as well as define all the interface
  35. types.
  36. */
  37. template <class T, class time_system>
  38. class base_time : private
  39. boost::less_than_comparable<T
  40. , boost::equality_comparable<T
  41. > >
  42. {
  43. public:
  44. // A tag for type categorization. Can be used to detect Boost.DateTime time points in generic code.
  45. typedef void _is_boost_date_time_time_point;
  46. typedef T time_type;
  47. typedef typename time_system::time_rep_type time_rep_type;
  48. typedef typename time_system::date_type date_type;
  49. typedef typename time_system::date_duration_type date_duration_type;
  50. typedef typename time_system::time_duration_type time_duration_type;
  51. //typedef typename time_system::hms_type hms_type;
  52. BOOST_CXX14_CONSTEXPR
  53. base_time(const date_type& day,
  54. const time_duration_type& td,
  55. dst_flags dst=not_dst) :
  56. time_(time_system::get_time_rep(day, td, dst))
  57. {}
  58. BOOST_CXX14_CONSTEXPR
  59. base_time(special_values sv) :
  60. time_(time_system::get_time_rep(sv))
  61. {}
  62. BOOST_CXX14_CONSTEXPR
  63. base_time(const time_rep_type& rhs) :
  64. time_(rhs)
  65. {}
  66. BOOST_CXX14_CONSTEXPR
  67. date_type date() const
  68. {
  69. return time_system::get_date(time_);
  70. }
  71. BOOST_CXX14_CONSTEXPR
  72. time_duration_type time_of_day() const
  73. {
  74. return time_system::get_time_of_day(time_);
  75. }
  76. /*! Optional bool parameter will return time zone as an offset
  77. * (ie "+07:00"). Empty string is returned for classes that do
  78. * not use a time_zone */
  79. std::string zone_name(bool /*as_offset*/=false) const
  80. {
  81. return time_system::zone_name(time_);
  82. }
  83. /*! Optional bool parameter will return time zone as an offset
  84. * (ie "+07:00"). Empty string is returned for classes that do
  85. * not use a time_zone */
  86. std::string zone_abbrev(bool /*as_offset*/=false) const
  87. {
  88. return time_system::zone_name(time_);
  89. }
  90. //! An empty string is returned for classes that do not use a time_zone
  91. std::string zone_as_posix_string() const
  92. {
  93. return std::string();
  94. }
  95. //! check to see if date is not a value
  96. BOOST_CXX14_CONSTEXPR
  97. bool is_not_a_date_time() const
  98. {
  99. return time_.is_not_a_date_time();
  100. }
  101. //! check to see if date is one of the infinity values
  102. BOOST_CXX14_CONSTEXPR
  103. bool is_infinity() const
  104. {
  105. return (is_pos_infinity() || is_neg_infinity());
  106. }
  107. //! check to see if date is greater than all possible dates
  108. BOOST_CXX14_CONSTEXPR
  109. bool is_pos_infinity() const
  110. {
  111. return time_.is_pos_infinity();
  112. }
  113. //! check to see if date is greater than all possible dates
  114. BOOST_CXX14_CONSTEXPR
  115. bool is_neg_infinity() const
  116. {
  117. return time_.is_neg_infinity();
  118. }
  119. //! check to see if time is a special value
  120. BOOST_CXX14_CONSTEXPR
  121. bool is_special() const
  122. {
  123. return(is_not_a_date_time() || is_infinity());
  124. }
  125. //!Equality operator -- others generated by boost::equality_comparable
  126. BOOST_CXX14_CONSTEXPR
  127. bool operator==(const time_type& rhs) const
  128. {
  129. return time_system::is_equal(time_,rhs.time_);
  130. }
  131. //!Equality operator -- others generated by boost::less_than_comparable
  132. BOOST_CXX14_CONSTEXPR
  133. bool operator<(const time_type& rhs) const
  134. {
  135. return time_system::is_less(time_,rhs.time_);
  136. }
  137. //! difference between two times
  138. BOOST_CXX14_CONSTEXPR
  139. time_duration_type operator-(const time_type& rhs) const
  140. {
  141. return time_system::subtract_times(time_, rhs.time_);
  142. }
  143. //! add date durations
  144. BOOST_CXX14_CONSTEXPR
  145. time_type operator+(const date_duration_type& dd) const
  146. {
  147. return time_system::add_days(time_, dd);
  148. }
  149. BOOST_CXX14_CONSTEXPR
  150. time_type operator+=(const date_duration_type& dd)
  151. {
  152. time_ = (time_system::get_time_rep(date() + dd, time_of_day()));
  153. return time_type(time_);
  154. }
  155. //! subtract date durations
  156. BOOST_CXX14_CONSTEXPR
  157. time_type operator-(const date_duration_type& dd) const
  158. {
  159. return time_system::subtract_days(time_, dd);
  160. }
  161. BOOST_CXX14_CONSTEXPR
  162. time_type operator-=(const date_duration_type& dd)
  163. {
  164. time_ = (time_system::get_time_rep(date() - dd, time_of_day()));
  165. return time_type(time_);
  166. }
  167. //! add time durations
  168. BOOST_CXX14_CONSTEXPR
  169. time_type operator+(const time_duration_type& td) const
  170. {
  171. return time_type(time_system::add_time_duration(time_, td));
  172. }
  173. BOOST_CXX14_CONSTEXPR
  174. time_type operator+=(const time_duration_type& td)
  175. {
  176. time_ = time_system::add_time_duration(time_,td);
  177. return time_type(time_);
  178. }
  179. //! subtract time durations
  180. BOOST_CXX14_CONSTEXPR
  181. time_type operator-(const time_duration_type& rhs) const
  182. {
  183. return time_system::subtract_time_duration(time_, rhs);
  184. }
  185. BOOST_CXX14_CONSTEXPR
  186. time_type operator-=(const time_duration_type& td)
  187. {
  188. time_ = time_system::subtract_time_duration(time_, td);
  189. return time_type(time_);
  190. }
  191. protected:
  192. time_rep_type time_;
  193. };
  194. } } //namespace date_time::boost
  195. #endif