Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef POSIX_TIME_CONVERSION_HPP___
0002 #define POSIX_TIME_CONVERSION_HPP___
0003 
0004 /* Copyright (c) 2002-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 <boost/cstdint.hpp>
0014 #include <boost/date_time/posix_time/ptime.hpp>
0015 #include <boost/date_time/posix_time/posix_time_duration.hpp>
0016 #include <boost/date_time/filetime_functions.hpp>
0017 #include <boost/date_time/c_time.hpp>
0018 #include <boost/date_time/time_resolution_traits.hpp> // absolute_value
0019 #include <boost/date_time/gregorian/conversion.hpp>
0020 
0021 namespace boost {
0022 
0023 namespace posix_time {
0024 
0025   //! Function that converts a time_t into a ptime.
0026   inline
0027   ptime from_time_t(std::time_t t)
0028   {
0029     return ptime(gregorian::date(1970,1,1)) + seconds(t);
0030   }
0031 
0032   //! Function that converts a ptime into a time_t
0033   inline
0034   std::time_t to_time_t(ptime pt)
0035   {
0036     return (pt - ptime(gregorian::date(1970,1,1))).total_seconds();
0037   }
0038 
0039   //! Convert a time to a tm structure truncating any fractional seconds
0040   inline
0041   std::tm to_tm(const boost::posix_time::ptime& t) {
0042     std::tm timetm = boost::gregorian::to_tm(t.date());
0043     boost::posix_time::time_duration td = t.time_of_day();
0044     timetm.tm_hour = static_cast<int>(td.hours());
0045     timetm.tm_min = static_cast<int>(td.minutes());
0046     timetm.tm_sec = static_cast<int>(td.seconds());
0047     timetm.tm_isdst = -1; // -1 used when dst info is unknown
0048     return timetm;
0049   }
0050   //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components
0051   inline
0052   std::tm to_tm(const boost::posix_time::time_duration& td) {
0053     std::tm timetm;
0054     std::memset(&timetm, 0, sizeof(timetm));
0055     timetm.tm_hour = static_cast<int>(date_time::absolute_value(td.hours()));
0056     timetm.tm_min = static_cast<int>(date_time::absolute_value(td.minutes()));
0057     timetm.tm_sec = static_cast<int>(date_time::absolute_value(td.seconds()));
0058     timetm.tm_isdst = -1; // -1 used when dst info is unknown
0059     return timetm;
0060   }
0061 
0062   //! Convert a tm struct to a ptime ignoring is_dst flag
0063   inline
0064   ptime ptime_from_tm(const std::tm& timetm) {
0065     boost::gregorian::date d = boost::gregorian::date_from_tm(timetm);
0066     return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec));
0067   }
0068 
0069 
0070 #if defined(BOOST_HAS_FTIME)
0071 
0072   //! Function to create a time object from an initialized FILETIME struct.
0073   /*! Function to create a time object from an initialized FILETIME struct.
0074    * A FILETIME struct holds 100-nanosecond units (0.0000001). When
0075    * built with microsecond resolution the FILETIME's sub second value
0076    * will be truncated. Nanosecond resolution has no truncation.
0077    *
0078    * \note FILETIME is part of the Win32 API, so it is not portable to non-windows
0079    * platforms.
0080    *
0081    * \note The function is templated on the FILETIME type, so that
0082    *       it can be used with both native FILETIME and the ad-hoc
0083    *       boost::detail::winapi::FILETIME_ type.
0084    */
0085   template< typename TimeT, typename FileTimeT >
0086   inline
0087   TimeT from_ftime(const FileTimeT& ft)
0088   {
0089     return boost::date_time::time_from_ftime<TimeT>(ft);
0090   }
0091 
0092 #endif // BOOST_HAS_FTIME
0093 
0094 } } //namespace boost::posix_time
0095 
0096 
0097 
0098 
0099 #endif
0100