time.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef DATE_TIME_TIME_HPP___
  2. #define DATE_TIME_TIME_HPP___
  3. /* Copyright (c) 2002,2003,2005 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. base_time(const date_type& day,
  53. const time_duration_type& td,
  54. dst_flags dst=not_dst) :
  55. time_(time_system::get_time_rep(day, td, dst))
  56. {}
  57. base_time(special_values sv) :
  58. time_(time_system::get_time_rep(sv))
  59. {}
  60. base_time(const time_rep_type& rhs) :
  61. time_(rhs)
  62. {}
  63. date_type date() const
  64. {
  65. return time_system::get_date(time_);
  66. }
  67. time_duration_type time_of_day() const
  68. {
  69. return time_system::get_time_of_day(time_);
  70. }
  71. /*! Optional bool parameter will return time zone as an offset
  72. * (ie "+07:00"). Empty string is returned for classes that do
  73. * not use a time_zone */
  74. std::string zone_name(bool /*as_offset*/=false) const
  75. {
  76. return time_system::zone_name(time_);
  77. }
  78. /*! Optional bool parameter will return time zone as an offset
  79. * (ie "+07:00"). Empty string is returned for classes that do
  80. * not use a time_zone */
  81. std::string zone_abbrev(bool /*as_offset*/=false) const
  82. {
  83. return time_system::zone_name(time_);
  84. }
  85. //! An empty string is returned for classes that do not use a time_zone
  86. std::string zone_as_posix_string() const
  87. {
  88. return std::string();
  89. }
  90. //! check to see if date is not a value
  91. bool is_not_a_date_time() const
  92. {
  93. return time_.is_not_a_date_time();
  94. }
  95. //! check to see if date is one of the infinity values
  96. bool is_infinity() const
  97. {
  98. return (is_pos_infinity() || is_neg_infinity());
  99. }
  100. //! check to see if date is greater than all possible dates
  101. bool is_pos_infinity() const
  102. {
  103. return time_.is_pos_infinity();
  104. }
  105. //! check to see if date is greater than all possible dates
  106. bool is_neg_infinity() const
  107. {
  108. return time_.is_neg_infinity();
  109. }
  110. //! check to see if time is a special value
  111. bool is_special() const
  112. {
  113. return(is_not_a_date_time() || is_infinity());
  114. }
  115. //!Equality operator -- others generated by boost::equality_comparable
  116. bool operator==(const time_type& rhs) const
  117. {
  118. return time_system::is_equal(time_,rhs.time_);
  119. }
  120. //!Equality operator -- others generated by boost::less_than_comparable
  121. bool operator<(const time_type& rhs) const
  122. {
  123. return time_system::is_less(time_,rhs.time_);
  124. }
  125. //! difference between two times
  126. time_duration_type operator-(const time_type& rhs) const
  127. {
  128. return time_system::subtract_times(time_, rhs.time_);
  129. }
  130. //! add date durations
  131. time_type operator+(const date_duration_type& dd) const
  132. {
  133. return time_system::add_days(time_, dd);
  134. }
  135. time_type operator+=(const date_duration_type& dd)
  136. {
  137. time_ = (time_system::get_time_rep(date() + dd, time_of_day()));
  138. return time_type(time_);
  139. }
  140. //! subtract date durations
  141. time_type operator-(const date_duration_type& dd) const
  142. {
  143. return time_system::subtract_days(time_, dd);
  144. }
  145. time_type operator-=(const date_duration_type& dd)
  146. {
  147. time_ = (time_system::get_time_rep(date() - dd, time_of_day()));
  148. return time_type(time_);
  149. }
  150. //! add time durations
  151. time_type operator+(const time_duration_type& td) const
  152. {
  153. return time_type(time_system::add_time_duration(time_, td));
  154. }
  155. time_type operator+=(const time_duration_type& td)
  156. {
  157. time_ = time_system::add_time_duration(time_,td);
  158. return time_type(time_);
  159. }
  160. //! subtract time durations
  161. time_type operator-(const time_duration_type& rhs) const
  162. {
  163. return time_system::subtract_time_duration(time_, rhs);
  164. }
  165. time_type operator-=(const time_duration_type& td)
  166. {
  167. time_ = time_system::subtract_time_duration(time_, td);
  168. return time_type(time_);
  169. }
  170. protected:
  171. time_rep_type time_;
  172. };
  173. } } //namespace date_time::boost
  174. #endif