Chris@16: #ifndef POSIX_TIME_CONVERSION_HPP___ Chris@16: #define POSIX_TIME_CONVERSION_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002-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 // absolute_value Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace posix_time { Chris@16: Chris@16: Chris@16: //! Function that converts a time_t into a ptime. Chris@16: inline Chris@16: ptime from_time_t(std::time_t t) Chris@16: { Chris@16: ptime start(gregorian::date(1970,1,1)); Chris@16: return start + seconds(static_cast(t)); Chris@16: } Chris@16: Chris@101: //! Function that converts a ptime into a time_t Chris@101: inline Chris@101: std::time_t to_time_t(ptime pt) Chris@101: { Chris@101: time_duration dur = pt - ptime(gregorian::date(1970,1,1)); Chris@101: return std::time_t(dur.total_seconds()); Chris@101: } Chris@101: Chris@16: //! Convert a time to a tm structure truncating any fractional seconds Chris@16: inline Chris@16: std::tm to_tm(const boost::posix_time::ptime& t) { Chris@16: std::tm timetm = boost::gregorian::to_tm(t.date()); Chris@16: boost::posix_time::time_duration td = t.time_of_day(); Chris@16: timetm.tm_hour = td.hours(); Chris@16: timetm.tm_min = td.minutes(); Chris@16: timetm.tm_sec = td.seconds(); Chris@16: timetm.tm_isdst = -1; // -1 used when dst info is unknown Chris@16: return timetm; Chris@16: } Chris@16: //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components Chris@16: inline Chris@16: std::tm to_tm(const boost::posix_time::time_duration& td) { Chris@16: std::tm timetm; Chris@16: std::memset(&timetm, 0, sizeof(timetm)); Chris@16: timetm.tm_hour = date_time::absolute_value(td.hours()); Chris@16: timetm.tm_min = date_time::absolute_value(td.minutes()); Chris@16: timetm.tm_sec = date_time::absolute_value(td.seconds()); Chris@16: timetm.tm_isdst = -1; // -1 used when dst info is unknown Chris@16: return timetm; Chris@16: } Chris@16: Chris@16: //! Convert a tm struct to a ptime ignoring is_dst flag Chris@16: inline Chris@16: ptime ptime_from_tm(const std::tm& timetm) { Chris@16: boost::gregorian::date d = boost::gregorian::date_from_tm(timetm); Chris@16: return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec)); Chris@16: } Chris@16: Chris@16: Chris@16: #if defined(BOOST_HAS_FTIME) Chris@16: Chris@16: //! Function to create a time object from an initialized FILETIME struct. Chris@16: /*! Function to create a time object from an initialized FILETIME struct. Chris@16: * A FILETIME struct holds 100-nanosecond units (0.0000001). When Chris@16: * built with microsecond resolution the FILETIME's sub second value Chris@16: * will be truncated. Nanosecond resolution has no truncation. Chris@16: * Chris@16: * \note FILETIME is part of the Win32 API, so it is not portable to non-windows Chris@16: * platforms. Chris@16: * Chris@16: * \note The function is templated on the FILETIME type, so that Chris@16: * it can be used with both native FILETIME and the ad-hoc Chris@16: * boost::date_time::winapi::file_time type. Chris@16: */ Chris@16: template< typename TimeT, typename FileTimeT > Chris@16: inline Chris@16: TimeT from_ftime(const FileTimeT& ft) Chris@16: { Chris@16: return boost::date_time::time_from_ftime(ft); Chris@16: } Chris@16: Chris@16: #endif // BOOST_HAS_FTIME Chris@16: Chris@16: } } //namespace boost::posix_time Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif Chris@16: