File indexing completed on 2025-01-18 09:30:36
0001 #ifndef DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
0002 #define DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <stdexcept>
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/date_time/compiler_config.hpp>
0019 #include <boost/date_time/c_time.hpp>
0020 #include <boost/numeric/conversion/cast.hpp>
0021
0022 namespace boost {
0023 namespace date_time {
0024
0025
0026
0027
0028
0029 template<class time_type>
0030 class c_local_adjustor {
0031 public:
0032 typedef typename time_type::time_duration_type time_duration_type;
0033 typedef typename time_type::date_type date_type;
0034 typedef typename date_type::duration_type date_duration_type;
0035
0036 static time_type utc_to_local(const time_type& t)
0037 {
0038 date_type time_t_start_day(1970,1,1);
0039 time_type time_t_start_time(time_t_start_day,time_duration_type(0,0,0));
0040 if (t < time_t_start_time) {
0041 boost::throw_exception(std::out_of_range("Cannot convert dates prior to Jan 1, 1970"));
0042 BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_t_start_time);
0043 }
0044 date_duration_type dd = t.date() - time_t_start_day;
0045 time_duration_type td = t.time_of_day();
0046 uint64_t t2 = static_cast<uint64_t>(dd.days())*86400 +
0047 static_cast<uint64_t>(td.hours())*3600 +
0048 static_cast<uint64_t>(td.minutes())*60 +
0049 td.seconds();
0050
0051 std::time_t tv = boost::numeric_cast<std::time_t>(t2);
0052 std::tm tms, *tms_ptr;
0053 tms_ptr = c_time::localtime(&tv, &tms);
0054 date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900),
0055 static_cast<unsigned short>(tms_ptr->tm_mon + 1),
0056 static_cast<unsigned short>(tms_ptr->tm_mday));
0057 time_duration_type td2(tms_ptr->tm_hour,
0058 tms_ptr->tm_min,
0059 tms_ptr->tm_sec,
0060 t.time_of_day().fractional_seconds());
0061
0062 return time_type(d,td2);
0063 }
0064 };
0065
0066
0067
0068 } }
0069
0070
0071
0072 #endif