source: NonGTP/Boost/boost/date_time/c_local_time_adjustor.hpp @ 857

Revision 857, 2.2 KB checked in by igarcia, 18 years ago (diff)
Line 
1#ifndef DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
2#define DATE_TIME_C_LOCAL_TIME_ADJUSTOR_HPP__
3
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 or http://www.boost.org/LICENSE-1.0)
8 * Author: Jeff Garland, Bart Garst
9 * $Date: 2005/03/19 21:39:58 $
10 */
11
12/*! @file c_local_time_adjustor.hpp
13  Time adjustment calculations based on machine
14*/
15
16#include <stdexcept>
17#include "boost/date_time/c_time.hpp"
18
19namespace boost {
20namespace date_time {
21
22  //! Adjust to / from utc using the C API
23  /*! Warning!!! This class assumes that timezone settings of the
24   *  machine are correct.  This can be a very dangerous assumption.
25   */
26  template<class time_type>
27  class c_local_adjustor {
28  public:
29    typedef typename time_type::time_duration_type time_duration_type;
30    typedef typename time_type::date_type date_type;
31    typedef typename date_type::duration_type date_duration_type;
32    //! Convert a utc time to local time
33    static time_type utc_to_local(const time_type& t)
34    {
35      date_type time_t_start_day(1970,1,1);
36      time_type time_t_start_time(time_t_start_day,time_duration_type(0,0,0));
37      if (t < time_t_start_time) {
38        throw std::out_of_range("Cannot convert dates prior to Jan 1, 1970");
39      }
40      date_duration_type dd = t.date() - time_t_start_day;
41      time_duration_type td = t.time_of_day();
42      std::time_t t2 = dd.days()*86400 + td.hours()*3600 + td.minutes()*60 + td.seconds();
43      std::tm tms, *tms_ptr;
44      tms_ptr = c_time::localtime(&t2, &tms);
45      //tms_ptr = std::localtime(&t2);
46      date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900),
47                  static_cast<unsigned short>(tms_ptr->tm_mon + 1),
48                  static_cast<unsigned short>(tms_ptr->tm_mday));
49      time_duration_type td2(tms_ptr->tm_hour,
50                             tms_ptr->tm_min,
51                             tms_ptr->tm_sec,
52                             t.time_of_day().fractional_seconds());
53     
54      return time_type(d,td2);
55    }
56  };
57
58
59
60} } //namespace date_time
61
62
63
64#endif
Note: See TracBrowser for help on using the repository browser.