Back to home page

EIC code displayed by LXR

 
 

    


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 /* Copyright (c) 2002,2003,2005 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 /*! @file c_local_time_adjustor.hpp
0013   Time adjustment calculations based on machine
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   //! Adjust to / from utc using the C API
0026   /*! Warning!!! This class assumes that timezone settings of the
0027    *  machine are correct.  This can be a very dangerous assumption.
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     //! Convert a utc time to local time
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); // should never reach
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       // detect y2038 issue and throw instead of proceed with bad time
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 } } //namespace date_time
0069 
0070 
0071 
0072 #endif