Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:40

0001 #ifndef DATE_TIME_TIME_SYSTEM_COUNTED_HPP
0002 #define DATE_TIME_TIME_SYSTEM_COUNTED_HPP
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 
0013 #include <boost/date_time/compiler_config.hpp>
0014 #include <boost/date_time/time_defs.hpp>
0015 #include <boost/date_time/special_defs.hpp>
0016 #include <string>
0017 
0018 
0019 namespace boost {
0020 namespace date_time {
0021 
0022   //! Time representation that uses a single integer count
0023   template<class config>
0024   struct counted_time_rep
0025   {
0026     typedef typename config::int_type   int_type;
0027     typedef typename config::date_type  date_type;
0028     typedef typename config::impl_type  impl_type;
0029     typedef typename date_type::duration_type date_duration_type;
0030     typedef typename date_type::calendar_type calendar_type;
0031     typedef typename date_type::ymd_type ymd_type;
0032     typedef typename config::time_duration_type time_duration_type;
0033     typedef typename config::resolution_traits   resolution_traits;
0034 
0035     BOOST_CXX14_CONSTEXPR
0036     counted_time_rep(const date_type& d, const time_duration_type& time_of_day)
0037       : time_count_(1)
0038     {
0039       if(d.is_infinity() || d.is_not_a_date() || time_of_day.is_special()) {
0040         time_count_ = time_of_day.get_rep() + d.day_count();
0041         //std::cout << time_count_ << std::endl;
0042       }
0043       else {
0044         time_count_ = (d.day_number() * frac_sec_per_day()) + time_of_day.ticks();
0045       }
0046     }
0047     BOOST_CXX14_CONSTEXPR
0048     explicit counted_time_rep(int_type count) :
0049       time_count_(count)
0050     {}
0051     BOOST_CXX14_CONSTEXPR
0052     explicit counted_time_rep(impl_type count) :
0053       time_count_(count)
0054     {}
0055     BOOST_CXX14_CONSTEXPR
0056     date_type date() const
0057     {
0058       if(time_count_.is_special()) {
0059         return date_type(time_count_.as_special());
0060       }
0061       else {
0062         typename calendar_type::date_int_type dc = static_cast<typename calendar_type::date_int_type>(day_count());
0063         //std::cout << "time_rep here:" << dc << std::endl;
0064         ymd_type ymd = calendar_type::from_day_number(dc);
0065         return date_type(ymd);
0066       }
0067     }
0068     //int_type day_count() const
0069     BOOST_CXX14_CONSTEXPR
0070     unsigned long day_count() const
0071     {
0072       /* resolution_traits::as_number returns a boost::int64_t &
0073        * frac_sec_per_day is also a boost::int64_t so, naturally,
0074        * the division operation returns a boost::int64_t.
0075        * The static_cast to an unsigned long is ok (results in no data loss)
0076        * because frac_sec_per_day is either the number of
0077        * microseconds per day, or the number of nanoseconds per day.
0078        * Worst case scenario: resolution_traits::as_number returns the
0079        * maximum value an int64_t can hold and frac_sec_per_day
0080        * is microseconds per day (lowest possible value).
0081        * The division operation will then return a value of 106751991 -
0082        * easily fitting in an unsigned long.
0083        */
0084       return static_cast<unsigned long>(resolution_traits::as_number(time_count_) / frac_sec_per_day());
0085     }
0086     BOOST_CXX14_CONSTEXPR int_type time_count() const
0087     {
0088       return resolution_traits::as_number(time_count_);
0089     }
0090     BOOST_CXX14_CONSTEXPR int_type tod() const
0091     {
0092       return resolution_traits::as_number(time_count_) % frac_sec_per_day();
0093     }
0094     static BOOST_CXX14_CONSTEXPR int_type frac_sec_per_day()
0095     {
0096       int_type seconds_per_day = 60*60*24;
0097       int_type fractional_sec_per_sec(resolution_traits::res_adjust());
0098       return seconds_per_day*fractional_sec_per_sec;
0099     }
0100     BOOST_CXX14_CONSTEXPR bool is_pos_infinity()const
0101     {
0102       return impl_type::is_pos_inf(time_count_.as_number());
0103     }
0104     BOOST_CXX14_CONSTEXPR bool is_neg_infinity()const
0105     {
0106       return impl_type::is_neg_inf(time_count_.as_number());
0107     }
0108     BOOST_CXX14_CONSTEXPR bool is_not_a_date_time()const
0109     {
0110       return impl_type::is_not_a_number(time_count_.as_number());
0111     }
0112     BOOST_CXX14_CONSTEXPR bool is_special()const
0113     {
0114       return time_count_.is_special();
0115     }
0116     BOOST_CXX14_CONSTEXPR impl_type get_rep()const
0117     {
0118       return time_count_;
0119     }
0120   private:
0121     impl_type time_count_;
0122   };
0123 
0124   //! An unadjusted time system implementation.
0125   template<class time_rep>
0126   class counted_time_system
0127   {
0128    public:
0129     typedef time_rep time_rep_type;
0130     typedef typename time_rep_type::impl_type impl_type;
0131     typedef typename time_rep_type::time_duration_type time_duration_type;
0132     typedef typename time_duration_type::fractional_seconds_type fractional_seconds_type;
0133     typedef typename time_rep_type::date_type date_type;
0134     typedef typename time_rep_type::date_duration_type date_duration_type;
0135 
0136 
0137     template<class T> static BOOST_CXX14_CONSTEXPR void unused_var(const T&) {}
0138 
0139     static BOOST_CXX14_CONSTEXPR
0140     time_rep_type get_time_rep(const date_type& day,
0141                                const time_duration_type& tod,
0142                                date_time::dst_flags dst=not_dst)
0143     {
0144       unused_var(dst);
0145       return time_rep_type(day, tod);
0146     }
0147 
0148     static BOOST_CXX14_CONSTEXPR time_rep_type get_time_rep(special_values sv)
0149     {
0150       switch (sv) {
0151       case not_a_date_time:
0152         return time_rep_type(date_type(not_a_date_time),
0153                              time_duration_type(not_a_date_time));
0154       case pos_infin:
0155         return time_rep_type(date_type(pos_infin),
0156                              time_duration_type(pos_infin));
0157       case neg_infin:
0158         return time_rep_type(date_type(neg_infin),
0159                              time_duration_type(neg_infin));
0160       case max_date_time: {
0161         time_duration_type td = time_duration_type(24,0,0,0) - time_duration_type(0,0,0,1);
0162         return time_rep_type(date_type(max_date_time), td);
0163       }
0164       case min_date_time:
0165         return time_rep_type(date_type(min_date_time), time_duration_type(0,0,0,0));
0166 
0167       default:
0168         return time_rep_type(date_type(not_a_date_time),
0169                              time_duration_type(not_a_date_time));
0170 
0171       }
0172 
0173     }
0174 
0175     static BOOST_CXX14_CONSTEXPR date_type
0176     get_date(const time_rep_type& val)
0177     {
0178       return val.date();
0179     }
0180     static BOOST_CXX14_CONSTEXPR
0181     time_duration_type get_time_of_day(const time_rep_type& val)
0182     {
0183       if(val.is_special()) {
0184         return time_duration_type(val.get_rep().as_special());
0185       }
0186       else{
0187         return time_duration_type(0,0,0,val.tod());
0188       }
0189     }
0190     static std::string zone_name(const time_rep_type&)
0191     {
0192       return "";
0193     }
0194     static BOOST_CXX14_CONSTEXPR bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs)
0195     {
0196       return (lhs.time_count() == rhs.time_count());
0197     }
0198     static BOOST_CXX14_CONSTEXPR
0199     bool is_less(const time_rep_type& lhs, const time_rep_type& rhs)
0200     {
0201       return (lhs.time_count() < rhs.time_count());
0202     }
0203     static BOOST_CXX14_CONSTEXPR
0204     time_rep_type add_days(const time_rep_type& base,
0205                            const date_duration_type& dd)
0206     {
0207       if(base.is_special() || dd.is_special()) {
0208         return(time_rep_type(base.get_rep() + dd.get_rep()));
0209       }
0210       else {
0211         return time_rep_type(base.time_count() + (dd.days() * time_rep_type::frac_sec_per_day()));
0212       }
0213     }
0214     static BOOST_CXX14_CONSTEXPR
0215     time_rep_type subtract_days(const time_rep_type& base,
0216                                 const date_duration_type& dd)
0217     {
0218       if(base.is_special() || dd.is_special()) {
0219         return(time_rep_type(base.get_rep() - dd.get_rep()));
0220       }
0221       else{
0222         return time_rep_type(base.time_count() - (dd.days() * time_rep_type::frac_sec_per_day()));
0223       }
0224     }
0225     static BOOST_CXX14_CONSTEXPR
0226     time_rep_type subtract_time_duration(const time_rep_type& base,
0227                                          const time_duration_type& td)
0228     {
0229       if(base.is_special() || td.is_special()) {
0230         return(time_rep_type(base.get_rep() - td.get_rep()));
0231       }
0232       else {
0233         return time_rep_type(base.time_count() - td.ticks());
0234       }
0235     }
0236     static BOOST_CXX14_CONSTEXPR
0237     time_rep_type add_time_duration(const time_rep_type& base,
0238                                     time_duration_type td)
0239     {
0240       if(base.is_special() || td.is_special()) {
0241         return(time_rep_type(base.get_rep() + td.get_rep()));
0242       }
0243       else {
0244         return time_rep_type(base.time_count() + td.ticks());
0245       }
0246     }
0247     static BOOST_CXX14_CONSTEXPR
0248     time_duration_type subtract_times(const time_rep_type& lhs,
0249                                       const time_rep_type& rhs)
0250     {
0251       if(lhs.is_special() || rhs.is_special()) {
0252         return(time_duration_type(
0253           impl_type::to_special((lhs.get_rep() - rhs.get_rep()).as_number())));
0254       }
0255       else {
0256         fractional_seconds_type fs = lhs.time_count() - rhs.time_count();
0257         return time_duration_type(0,0,0,fs);
0258       }
0259     }
0260 
0261   };
0262 
0263 
0264 } } //namespace date_time
0265 
0266 
0267 
0268 #endif
0269