1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #ifndef _GREGORIAN__CONVERSION_HPP___
- #define _GREGORIAN__CONVERSION_HPP___
- #include <cstring>
- #include <string>
- #include <stdexcept>
- #include <boost/throw_exception.hpp>
- #include <boost/date_time/c_time.hpp>
- #include <boost/date_time/special_defs.hpp>
- #include <boost/date_time/gregorian/gregorian_types.hpp>
- namespace boost {
- namespace gregorian {
-
- inline
- std::tm to_tm(const date& d)
- {
- if (d.is_special())
- {
- std::string s = "tm unable to handle ";
- switch (d.as_special())
- {
- case date_time::not_a_date_time:
- s += "not-a-date-time value"; break;
- case date_time::neg_infin:
- s += "-infinity date value"; break;
- case date_time::pos_infin:
- s += "+infinity date value"; break;
- default:
- s += "a special date value"; break;
- }
- boost::throw_exception(std::out_of_range(s));
- }
- std::tm datetm;
- std::memset(&datetm, 0, sizeof(datetm));
- boost::gregorian::date::ymd_type ymd = d.year_month_day();
- datetm.tm_year = ymd.year - 1900;
- datetm.tm_mon = ymd.month - 1;
- datetm.tm_mday = ymd.day;
- datetm.tm_wday = d.day_of_week();
- datetm.tm_yday = d.day_of_year() - 1;
- datetm.tm_isdst = -1;
- return datetm;
- }
-
- inline
- date date_from_tm(const std::tm& datetm)
- {
- return date(static_cast<unsigned short>(datetm.tm_year+1900),
- static_cast<unsigned short>(datetm.tm_mon+1),
- static_cast<unsigned short>(datetm.tm_mday));
- }
- } }
- #endif
|