fix doc example typo
[boost.git] / boost / date_time / gregorian / conversion.hpp
blobb31bec0b43db6c38489381dc16a324e3a2608c26
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
9 * $Date$
12 #include <string>
13 #include <stdexcept>
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>
20 # else
21 # include <boost/date_time/gregorian/formatters.hpp>
22 # endif // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
23 #else
24 # include <sstream>
25 # include <boost/date_time/gregorian/gregorian_io.hpp>
26 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
28 namespace boost {
30 namespace gregorian {
33 //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value
34 inline
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);
41 #else
42 std::ostringstream ss;
43 ss << d;
44 s += ss.str();
45 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
46 boost::throw_exception(std::out_of_range(s));
48 std::tm datetm;
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
57 return datetm;
60 //! Converts a tm structure into a date dropping the any time values.
61 inline
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
75 #endif