Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:34

0001 #ifndef _GREGORIAN__CONVERSION_HPP___
0002 #define _GREGORIAN__CONVERSION_HPP___
0003 
0004 /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 #include <cstring>
0013 #include <string>
0014 #include <stdexcept>
0015 #include <boost/throw_exception.hpp>
0016 #include <boost/date_time/c_time.hpp>
0017 #include <boost/date_time/special_defs.hpp>
0018 #include <boost/date_time/gregorian/gregorian_types.hpp>
0019 
0020 namespace boost {
0021 
0022 namespace gregorian {
0023 
0024   //! Converts a date to a tm struct. Throws out_of_range exception if date is a special value
0025   inline
0026   std::tm to_tm(const date& d)
0027   {
0028     if (d.is_special())
0029     {
0030         std::string s = "tm unable to handle ";
0031         switch (d.as_special())
0032         {
0033         case date_time::not_a_date_time:
0034             s += "not-a-date-time value"; break;
0035         case date_time::neg_infin:
0036             s += "-infinity date value"; break;
0037         case date_time::pos_infin:
0038             s += "+infinity date value"; break;
0039         default:
0040             s += "a special date value"; break;
0041         }
0042         boost::throw_exception(std::out_of_range(s));
0043     }
0044 
0045     std::tm datetm;
0046     std::memset(&datetm, 0, sizeof(datetm));
0047     boost::gregorian::date::ymd_type ymd = d.year_month_day();
0048     datetm.tm_year = ymd.year - 1900;
0049     datetm.tm_mon = ymd.month - 1;
0050     datetm.tm_mday = ymd.day;
0051     datetm.tm_wday = d.day_of_week();
0052     datetm.tm_yday = d.day_of_year() - 1;
0053     datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst
0054     return datetm;
0055   }
0056 
0057   //! Converts a tm structure into a date dropping the any time values.
0058   inline
0059   date date_from_tm(const std::tm& datetm)
0060   {
0061     return date(static_cast<unsigned short>(datetm.tm_year+1900),
0062                 static_cast<unsigned short>(datetm.tm_mon+1),
0063                 static_cast<unsigned short>(datetm.tm_mday));
0064   }
0065 
0066 } } //namespace boost::gregorian
0067 
0068 #endif