1 #ifndef _GREGORIAN__CONVERSION_HPP___
2 #define _GREGORIAN__CONVERSION_HPP___
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.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
14 #include <boost/throw_exception.hpp>
15 #include <boost/date_time/gregorian/gregorian_types.hpp>
16 #include <boost/date_time/c_time.hpp>
17 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
18 # if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
19 # include <boost/date_time/gregorian/formatters_limited.hpp>
21 # include <boost/date_time/gregorian/formatters.hpp>
22 # endif // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
25 # include <boost/date_time/gregorian/gregorian_io.hpp>
26 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
33 //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value
35 std::tm
to_tm(const date
& d
)
37 if(d
.is_pos_infinity() || d
.is_neg_infinity() || d
.is_not_a_date()){
38 std::string s
= "tm unable to handle date value of ";
39 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
40 s
+= to_simple_string(d
);
42 std::ostringstream ss
;
45 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
46 boost::throw_exception(std::out_of_range(s
));
49 boost::gregorian::date::ymd_type ymd
= d
.year_month_day();
50 datetm
.tm_year
= ymd
.year
-1900;
51 datetm
.tm_mon
= ymd
.month
-1;
52 datetm
.tm_mday
= ymd
.day
;
53 datetm
.tm_wday
= d
.day_of_week();
54 datetm
.tm_yday
= d
.day_of_year()-1;
55 datetm
.tm_hour
= datetm
.tm_min
= datetm
.tm_sec
= 0;
56 datetm
.tm_isdst
= -1; // negative because not enough info to set tm_isdst
60 //! Converts a tm structure into a date dropping the any time values.
62 date
date_from_tm(const std::tm
& datetm
)
64 return date(static_cast<unsigned short>(datetm
.tm_year
+1900),
65 static_cast<unsigned short>(datetm
.tm_mon
+1),
66 static_cast<unsigned short>(datetm
.tm_mday
));
70 } } //namespace boost::gregorian