123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #ifndef DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
- #define DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
- #include <stdexcept>
- #include <boost/throw_exception.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/c_time.hpp>
- namespace boost {
- namespace date_time {
-
-
- template<class time_type>
- class c_local_adjustor {
- public:
- typedef typename time_type::time_duration_type time_duration_type;
- typedef typename time_type::date_type date_type;
- typedef typename date_type::duration_type date_duration_type;
-
- static time_type utc_to_local(const time_type& t)
- {
- date_type time_t_start_day(1970,1,1);
- time_type time_t_start_time(time_t_start_day,time_duration_type(0,0,0));
- if (t < time_t_start_time) {
- boost::throw_exception(std::out_of_range("Cannot convert dates prior to Jan 1, 1970"));
- BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_t_start_time);
- }
- date_duration_type dd = t.date() - time_t_start_day;
- time_duration_type td = t.time_of_day();
- std::time_t t2 = dd.days()*86400 + td.hours()*3600 + td.minutes()*60 + td.seconds();
- std::tm tms, *tms_ptr;
- tms_ptr = c_time::localtime(&t2, &tms);
- date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900),
- static_cast<unsigned short>(tms_ptr->tm_mon + 1),
- static_cast<unsigned short>(tms_ptr->tm_mday));
- time_duration_type td2(tms_ptr->tm_hour,
- tms_ptr->tm_min,
- tms_ptr->tm_sec,
- t.time_of_day().fractional_seconds());
-
- return time_type(d,td2);
- }
- };
- } }
- #endif
|