Chris@16: #ifndef _GREGORIAN__CONVERSION_HPP___ Chris@16: #define _GREGORIAN__CONVERSION_HPP___ Chris@16: Chris@16: /* Copyright (c) 2004-2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace gregorian { Chris@16: Chris@16: //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value Chris@16: inline Chris@16: std::tm to_tm(const date& d) Chris@16: { Chris@16: if (d.is_special()) Chris@16: { Chris@16: std::string s = "tm unable to handle "; Chris@16: switch (d.as_special()) Chris@16: { Chris@16: case date_time::not_a_date_time: Chris@16: s += "not-a-date-time value"; break; Chris@16: case date_time::neg_infin: Chris@16: s += "-infinity date value"; break; Chris@16: case date_time::pos_infin: Chris@16: s += "+infinity date value"; break; Chris@16: default: Chris@16: s += "a special date value"; break; Chris@16: } Chris@16: boost::throw_exception(std::out_of_range(s)); Chris@16: } Chris@16: Chris@16: std::tm datetm; Chris@16: std::memset(&datetm, 0, sizeof(datetm)); Chris@16: boost::gregorian::date::ymd_type ymd = d.year_month_day(); Chris@16: datetm.tm_year = ymd.year - 1900; Chris@16: datetm.tm_mon = ymd.month - 1; Chris@16: datetm.tm_mday = ymd.day; Chris@16: datetm.tm_wday = d.day_of_week(); Chris@16: datetm.tm_yday = d.day_of_year() - 1; Chris@16: datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst Chris@16: return datetm; Chris@16: } Chris@16: Chris@16: //! Converts a tm structure into a date dropping the any time values. Chris@16: inline Chris@16: date date_from_tm(const std::tm& datetm) Chris@16: { Chris@16: return date(static_cast(datetm.tm_year+1900), Chris@16: static_cast(datetm.tm_mon+1), Chris@16: static_cast(datetm.tm_mday)); Chris@16: } Chris@16: Chris@16: } } //namespace boost::gregorian Chris@16: Chris@16: #endif