Chris@16
|
1 #ifndef _GREGORIAN__CONVERSION_HPP___
|
Chris@16
|
2 #define _GREGORIAN__CONVERSION_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
|
Chris@16
|
5 * Use, modification and distribution is subject to the
|
Chris@16
|
6 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 #include <cstring>
|
Chris@16
|
13 #include <string>
|
Chris@16
|
14 #include <stdexcept>
|
Chris@16
|
15 #include <boost/throw_exception.hpp>
|
Chris@16
|
16 #include <boost/date_time/c_time.hpp>
|
Chris@16
|
17 #include <boost/date_time/special_defs.hpp>
|
Chris@16
|
18 #include <boost/date_time/gregorian/gregorian_types.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost {
|
Chris@16
|
21
|
Chris@16
|
22 namespace gregorian {
|
Chris@16
|
23
|
Chris@16
|
24 //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value
|
Chris@16
|
25 inline
|
Chris@16
|
26 std::tm to_tm(const date& d)
|
Chris@16
|
27 {
|
Chris@16
|
28 if (d.is_special())
|
Chris@16
|
29 {
|
Chris@16
|
30 std::string s = "tm unable to handle ";
|
Chris@16
|
31 switch (d.as_special())
|
Chris@16
|
32 {
|
Chris@16
|
33 case date_time::not_a_date_time:
|
Chris@16
|
34 s += "not-a-date-time value"; break;
|
Chris@16
|
35 case date_time::neg_infin:
|
Chris@16
|
36 s += "-infinity date value"; break;
|
Chris@16
|
37 case date_time::pos_infin:
|
Chris@16
|
38 s += "+infinity date value"; break;
|
Chris@16
|
39 default:
|
Chris@16
|
40 s += "a special date value"; break;
|
Chris@16
|
41 }
|
Chris@16
|
42 boost::throw_exception(std::out_of_range(s));
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 std::tm datetm;
|
Chris@16
|
46 std::memset(&datetm, 0, sizeof(datetm));
|
Chris@16
|
47 boost::gregorian::date::ymd_type ymd = d.year_month_day();
|
Chris@16
|
48 datetm.tm_year = ymd.year - 1900;
|
Chris@16
|
49 datetm.tm_mon = ymd.month - 1;
|
Chris@16
|
50 datetm.tm_mday = ymd.day;
|
Chris@16
|
51 datetm.tm_wday = d.day_of_week();
|
Chris@16
|
52 datetm.tm_yday = d.day_of_year() - 1;
|
Chris@16
|
53 datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst
|
Chris@16
|
54 return datetm;
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 //! Converts a tm structure into a date dropping the any time values.
|
Chris@16
|
58 inline
|
Chris@16
|
59 date date_from_tm(const std::tm& datetm)
|
Chris@16
|
60 {
|
Chris@16
|
61 return date(static_cast<unsigned short>(datetm.tm_year+1900),
|
Chris@16
|
62 static_cast<unsigned short>(datetm.tm_mon+1),
|
Chris@16
|
63 static_cast<unsigned short>(datetm.tm_mday));
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 } } //namespace boost::gregorian
|
Chris@16
|
67
|
Chris@16
|
68 #endif
|