Chris@16
|
1 #ifndef POSIX_TIME_CONVERSION_HPP___
|
Chris@16
|
2 #define POSIX_TIME_CONVERSION_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002-2005 CrystalClear Software, Inc.
|
Chris@16
|
5 * Use, modification and distribution is subject to the
|
Chris@16
|
6 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 #include <cstring>
|
Chris@16
|
13 #include <boost/date_time/posix_time/ptime.hpp>
|
Chris@16
|
14 #include <boost/date_time/posix_time/posix_time_duration.hpp>
|
Chris@16
|
15 #include <boost/date_time/filetime_functions.hpp>
|
Chris@16
|
16 #include <boost/date_time/c_time.hpp>
|
Chris@16
|
17 #include <boost/date_time/time_resolution_traits.hpp> // absolute_value
|
Chris@16
|
18 #include <boost/date_time/gregorian/conversion.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost {
|
Chris@16
|
21
|
Chris@16
|
22 namespace posix_time {
|
Chris@16
|
23
|
Chris@16
|
24
|
Chris@16
|
25 //! Function that converts a time_t into a ptime.
|
Chris@16
|
26 inline
|
Chris@16
|
27 ptime from_time_t(std::time_t t)
|
Chris@16
|
28 {
|
Chris@16
|
29 ptime start(gregorian::date(1970,1,1));
|
Chris@16
|
30 return start + seconds(static_cast<long>(t));
|
Chris@16
|
31 }
|
Chris@16
|
32
|
Chris@101
|
33 //! Function that converts a ptime into a time_t
|
Chris@101
|
34 inline
|
Chris@101
|
35 std::time_t to_time_t(ptime pt)
|
Chris@101
|
36 {
|
Chris@101
|
37 time_duration dur = pt - ptime(gregorian::date(1970,1,1));
|
Chris@101
|
38 return std::time_t(dur.total_seconds());
|
Chris@101
|
39 }
|
Chris@101
|
40
|
Chris@16
|
41 //! Convert a time to a tm structure truncating any fractional seconds
|
Chris@16
|
42 inline
|
Chris@16
|
43 std::tm to_tm(const boost::posix_time::ptime& t) {
|
Chris@16
|
44 std::tm timetm = boost::gregorian::to_tm(t.date());
|
Chris@16
|
45 boost::posix_time::time_duration td = t.time_of_day();
|
Chris@16
|
46 timetm.tm_hour = td.hours();
|
Chris@16
|
47 timetm.tm_min = td.minutes();
|
Chris@16
|
48 timetm.tm_sec = td.seconds();
|
Chris@16
|
49 timetm.tm_isdst = -1; // -1 used when dst info is unknown
|
Chris@16
|
50 return timetm;
|
Chris@16
|
51 }
|
Chris@16
|
52 //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components
|
Chris@16
|
53 inline
|
Chris@16
|
54 std::tm to_tm(const boost::posix_time::time_duration& td) {
|
Chris@16
|
55 std::tm timetm;
|
Chris@16
|
56 std::memset(&timetm, 0, sizeof(timetm));
|
Chris@16
|
57 timetm.tm_hour = date_time::absolute_value(td.hours());
|
Chris@16
|
58 timetm.tm_min = date_time::absolute_value(td.minutes());
|
Chris@16
|
59 timetm.tm_sec = date_time::absolute_value(td.seconds());
|
Chris@16
|
60 timetm.tm_isdst = -1; // -1 used when dst info is unknown
|
Chris@16
|
61 return timetm;
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@16
|
64 //! Convert a tm struct to a ptime ignoring is_dst flag
|
Chris@16
|
65 inline
|
Chris@16
|
66 ptime ptime_from_tm(const std::tm& timetm) {
|
Chris@16
|
67 boost::gregorian::date d = boost::gregorian::date_from_tm(timetm);
|
Chris@16
|
68 return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec));
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71
|
Chris@16
|
72 #if defined(BOOST_HAS_FTIME)
|
Chris@16
|
73
|
Chris@16
|
74 //! Function to create a time object from an initialized FILETIME struct.
|
Chris@16
|
75 /*! Function to create a time object from an initialized FILETIME struct.
|
Chris@16
|
76 * A FILETIME struct holds 100-nanosecond units (0.0000001). When
|
Chris@16
|
77 * built with microsecond resolution the FILETIME's sub second value
|
Chris@16
|
78 * will be truncated. Nanosecond resolution has no truncation.
|
Chris@16
|
79 *
|
Chris@16
|
80 * \note FILETIME is part of the Win32 API, so it is not portable to non-windows
|
Chris@16
|
81 * platforms.
|
Chris@16
|
82 *
|
Chris@16
|
83 * \note The function is templated on the FILETIME type, so that
|
Chris@16
|
84 * it can be used with both native FILETIME and the ad-hoc
|
Chris@16
|
85 * boost::date_time::winapi::file_time type.
|
Chris@16
|
86 */
|
Chris@16
|
87 template< typename TimeT, typename FileTimeT >
|
Chris@16
|
88 inline
|
Chris@16
|
89 TimeT from_ftime(const FileTimeT& ft)
|
Chris@16
|
90 {
|
Chris@16
|
91 return boost::date_time::time_from_ftime<TimeT>(ft);
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 #endif // BOOST_HAS_FTIME
|
Chris@16
|
95
|
Chris@16
|
96 } } //namespace boost::posix_time
|
Chris@16
|
97
|
Chris@16
|
98
|
Chris@16
|
99
|
Chris@16
|
100
|
Chris@16
|
101 #endif
|
Chris@16
|
102
|