source: NonGTP/Boost/boost/date_time/gregorian/conversion.hpp @ 857

Revision 857, 2.3 KB checked in by igarcia, 19 years ago (diff)
Line 
1#ifndef _GREGORIAN__CONVERSION_HPP___
2#define _GREGORIAN__CONVERSION_HPP___
3
4/* Copyright (c) 2004-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/10/28 13:32:38 $
10 */
11
12#include <exception>
13#include "boost/date_time/gregorian/gregorian_types.hpp"
14#include "boost/date_time/c_time.hpp"
15#if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
16#  if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
17#    include "boost/date_time/gregorian/formatters_limited.hpp"
18#  else
19#    include "boost/date_time/gregorian/formatters.hpp"
20#  endif // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
21#else
22#  include <sstream>
23#  include "boost/date_time/gregorian/gregorian_io.hpp"
24#endif // USE_DATE_TIME_PRE_1_33_FACET_IO
25
26namespace boost {
27
28namespace gregorian {
29
30
31  //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value
32  inline
33  std::tm to_tm(const date& d)
34  {
35    if(d.is_pos_infinity() || d.is_neg_infinity() || d.is_not_a_date()){
36#if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
37      std::string s("tm unable to handle date value of " + to_simple_string(d));
38      throw std::out_of_range(s);
39#else
40      std::stringstream ss;
41      ss << "tm unable to handle date value of " << d;
42      throw std::out_of_range(ss.str());
43#endif // USE_DATE_TIME_PRE_1_33_FACET_IO
44    }
45    std::tm datetm;
46    boost::gregorian::date::ymd_type ymd = d.year_month_day();
47    datetm.tm_year = ymd.year-1900;
48    datetm.tm_mon = ymd.month-1;
49    datetm.tm_mday = ymd.day;
50    datetm.tm_wday = d.day_of_week();
51    datetm.tm_yday = d.day_of_year()-1;
52    datetm.tm_hour = datetm.tm_min = datetm.tm_sec = 0;
53    datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst
54    return datetm;
55  }
56
57  //! Converts a tm structure into a date dropping the any time values.
58  inline
59  date date_from_tm(const std::tm& datetm)
60  {
61    return date(static_cast<unsigned short>(datetm.tm_year+1900),
62                static_cast<unsigned short>(datetm.tm_mon+1),
63                static_cast<unsigned short>(datetm.tm_mday));
64  }
65 
66
67} } //namespace boost::gregorian
68
69
70
71
72#endif
73
Note: See TracBrowser for help on using the repository browser.