File indexing completed on 2025-01-18 09:30:36
0001 #ifndef POSIX_TIME_CONVERSION_HPP___
0002 #define POSIX_TIME_CONVERSION_HPP___
0003
0004
0005
0006
0007
0008
0009
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
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
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
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;
0048 return timetm;
0049 }
0050
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;
0059 return timetm;
0060 }
0061
0062
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
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
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
0093
0094 } }
0095
0096
0097
0098
0099 #endif
0100