1 #ifndef DATE_TIME_TIME_HPP___
2 #define DATE_TIME_TIME_HPP___
4 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
14 This file contains the interface for the time associated classes.
17 #include <boost/operators.hpp>
18 #include <boost/date_time/time_defs.hpp>
19 #include <boost/date_time/special_defs.hpp>
24 //! Representation of a precise moment in time, including the date.
26 This class is a skeleton for the interface of a temporal type
27 with a resolution that is higher than a day. It is intended that
28 this class be the base class and that the actual time
29 class be derived using the BN pattern. In this way, the derived
30 class can make decisions such as 'should there be a default constructor'
31 and what should it set its value to, should there be optional constructors
32 say allowing only an time_durations that generate a time from a clock,etc.
33 So, in fact multiple time types can be created for a time_system with
34 different construction policies, and all of them can perform basic
35 operations by only writing a copy constructor. Finally, compiler
36 errors are also shorter.
38 The real behavior of the time class is provided by the time_system
39 template parameter. This class must provide all the logic
40 for addition, subtraction, as well as define all the interface
45 template <class T
, class time_system
>
46 class base_time
: private
47 boost::less_than_comparable
<T
48 , boost::equality_comparable
<T
53 typedef typename
time_system::time_rep_type time_rep_type
;
54 typedef typename
time_system::date_type date_type
;
55 typedef typename
time_system::date_duration_type date_duration_type
;
56 typedef typename
time_system::time_duration_type time_duration_type
;
57 //typedef typename time_system::hms_type hms_type;
59 base_time(const date_type
& day
,
60 const time_duration_type
& td
,
61 dst_flags dst
=not_dst
) :
62 time_(time_system::get_time_rep(day
, td
, dst
))
64 base_time(special_values sv
) :
65 time_(time_system::get_time_rep(sv
))
67 base_time(const time_rep_type
& rhs
) :
70 date_type
date() const
72 return time_system::get_date(time_
);
74 time_duration_type
time_of_day() const
76 return time_system::get_time_of_day(time_
);
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_name(bool /*as_offset*/=false) const
83 return time_system::zone_name(time_
);
85 /*! Optional bool parameter will return time zone as an offset
86 * (ie "+07:00"). Empty string is returned for classes that do
87 * not use a time_zone */
88 std::string
zone_abbrev(bool /*as_offset*/=false) const
90 return time_system::zone_name(time_
);
92 //! An empty string is returned for classes that do not use a time_zone
93 std::string
zone_as_posix_string() const
98 //! check to see if date is not a value
99 bool is_not_a_date_time() const
101 return time_
.is_not_a_date_time();
103 //! check to see if date is one of the infinity values
104 bool is_infinity() const
106 return (is_pos_infinity() || is_neg_infinity());
108 //! check to see if date is greater than all possible dates
109 bool is_pos_infinity() const
111 return time_
.is_pos_infinity();
113 //! check to see if date is greater than all possible dates
114 bool is_neg_infinity() const
116 return time_
.is_neg_infinity();
118 //! check to see if time is a special value
119 bool is_special() const
121 return(is_not_a_date_time() || is_infinity());
123 //!Equality operator -- others generated by boost::equality_comparable
124 bool operator==(const time_type
& rhs
) const
126 return time_system::is_equal(time_
,rhs
.time_
);
128 //!Equality operator -- others generated by boost::less_than_comparable
129 bool operator<(const time_type
& rhs
) const
131 return time_system::is_less(time_
,rhs
.time_
);
133 //! difference between two times
134 time_duration_type
operator-(const time_type
& rhs
) const
136 return time_system::subtract_times(time_
, rhs
.time_
);
138 //! add date durations
139 time_type
operator+(const date_duration_type
& dd
) const
141 return time_system::add_days(time_
, dd
);
143 time_type
operator+=(const date_duration_type
& dd
)
145 time_
= (time_system::get_time_rep(date() + dd
, time_of_day()));
146 return time_type(time_
);
148 //! subtract date durations
149 time_type
operator-(const date_duration_type
& dd
) const
151 return time_system::subtract_days(time_
, dd
);
153 time_type
operator-=(const date_duration_type
& dd
)
155 time_
= (time_system::get_time_rep(date() - dd
, time_of_day()));
156 return time_type(time_
);
158 //! add time durations
159 time_type
operator+(const time_duration_type
& td
) const
161 return time_type(time_system::add_time_duration(time_
, td
));
163 time_type
operator+=(const time_duration_type
& td
)
165 time_
= (time_system::get_time_rep(date(), time_of_day() + td
));
166 return time_type(time_
);
168 //! subtract time durations
169 time_type
operator-(const time_duration_type
& rhs
) const
171 return time_system::subtract_time_duration(time_
, rhs
);
173 time_type
operator-=(const time_duration_type
& td
)
175 time_
= (time_system::get_time_rep(date(), time_of_day() - td
));
176 return time_type(time_
);
187 } } //namespace date_time::boost