Chris@16
|
1 #ifndef DATE_TIME_FILETIME_FUNCTIONS_HPP__
|
Chris@16
|
2 #define DATE_TIME_FILETIME_FUNCTIONS_HPP__
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2004 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 /*! @file filetime_functions.hpp
|
Chris@16
|
13 * Function(s) for converting between a FILETIME structure and a
|
Chris@16
|
14 * time object. This file is only available on systems that have
|
Chris@16
|
15 * BOOST_HAS_FTIME defined.
|
Chris@16
|
16 */
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/date_time/compiler_config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_HAS_FTIME) // skip this file if no FILETIME
|
Chris@16
|
21
|
Chris@16
|
22 #if defined(BOOST_USE_WINDOWS_H)
|
Chris@16
|
23 # include <windows.h>
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 #include <boost/cstdint.hpp>
|
Chris@16
|
27 #include <boost/date_time/time.hpp>
|
Chris@16
|
28 #include <boost/date_time/date_defs.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31
|
Chris@16
|
32 namespace date_time {
|
Chris@16
|
33
|
Chris@16
|
34 namespace winapi {
|
Chris@16
|
35
|
Chris@16
|
36 #if !defined(BOOST_USE_WINDOWS_H)
|
Chris@16
|
37
|
Chris@16
|
38 extern "C" {
|
Chris@16
|
39
|
Chris@16
|
40 struct FILETIME
|
Chris@16
|
41 {
|
Chris@16
|
42 boost::uint32_t dwLowDateTime;
|
Chris@16
|
43 boost::uint32_t dwHighDateTime;
|
Chris@16
|
44 };
|
Chris@16
|
45 struct SYSTEMTIME
|
Chris@16
|
46 {
|
Chris@16
|
47 boost::uint16_t wYear;
|
Chris@16
|
48 boost::uint16_t wMonth;
|
Chris@16
|
49 boost::uint16_t wDayOfWeek;
|
Chris@16
|
50 boost::uint16_t wDay;
|
Chris@16
|
51 boost::uint16_t wHour;
|
Chris@16
|
52 boost::uint16_t wMinute;
|
Chris@16
|
53 boost::uint16_t wSecond;
|
Chris@16
|
54 boost::uint16_t wMilliseconds;
|
Chris@16
|
55 };
|
Chris@16
|
56
|
Chris@16
|
57 __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(FILETIME* lpFileTime);
|
Chris@16
|
58 __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const FILETIME* lpFileTime, FILETIME* lpLocalFileTime);
|
Chris@16
|
59 __declspec(dllimport) void __stdcall GetSystemTime(SYSTEMTIME* lpSystemTime);
|
Chris@16
|
60 __declspec(dllimport) int __stdcall SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, FILETIME* lpFileTime);
|
Chris@16
|
61
|
Chris@16
|
62 } // extern "C"
|
Chris@16
|
63
|
Chris@16
|
64 #endif // defined(BOOST_USE_WINDOWS_H)
|
Chris@16
|
65
|
Chris@16
|
66 typedef FILETIME file_time;
|
Chris@16
|
67 typedef SYSTEMTIME system_time;
|
Chris@16
|
68
|
Chris@16
|
69 inline void get_system_time_as_file_time(file_time& ft)
|
Chris@16
|
70 {
|
Chris@16
|
71 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
|
Chris@16
|
72 // Some runtime library implementations expect local times as the norm for ctime.
|
Chris@16
|
73 file_time ft_utc;
|
Chris@16
|
74 GetSystemTimeAsFileTime(&ft_utc);
|
Chris@16
|
75 FileTimeToLocalFileTime(&ft_utc, &ft);
|
Chris@16
|
76 #elif defined(BOOST_HAS_GETSYSTEMTIMEASFILETIME)
|
Chris@16
|
77 GetSystemTimeAsFileTime(&ft);
|
Chris@16
|
78 #else
|
Chris@16
|
79 system_time st;
|
Chris@16
|
80 GetSystemTime(&st);
|
Chris@16
|
81 SystemTimeToFileTime(&st, &ft);
|
Chris@16
|
82 #endif
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 /*!
|
Chris@16
|
86 * The function converts file_time into number of microseconds elapsed since 1970-Jan-01
|
Chris@16
|
87 *
|
Chris@16
|
88 * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped.
|
Chris@16
|
89 *
|
Chris@16
|
90 * \note The function is templated on the FILETIME type, so that
|
Chris@16
|
91 * it can be used with both native FILETIME and the ad-hoc
|
Chris@16
|
92 * boost::date_time::winapi::file_time type.
|
Chris@16
|
93 */
|
Chris@16
|
94 template< typename FileTimeT >
|
Chris@16
|
95 inline boost::uint64_t file_time_to_microseconds(FileTimeT const& ft)
|
Chris@16
|
96 {
|
Chris@16
|
97 /* shift is difference between 1970-Jan-01 & 1601-Jan-01
|
Chris@16
|
98 * in 100-nanosecond intervals */
|
Chris@16
|
99 const uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008
|
Chris@16
|
100
|
Chris@16
|
101 union {
|
Chris@16
|
102 FileTimeT as_file_time;
|
Chris@16
|
103 uint64_t as_integer; // 100-nanos since 1601-Jan-01
|
Chris@16
|
104 } caster;
|
Chris@16
|
105 caster.as_file_time = ft;
|
Chris@16
|
106
|
Chris@16
|
107 caster.as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01
|
Chris@16
|
108 return (caster.as_integer / 10); // truncate to microseconds
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 } // namespace winapi
|
Chris@16
|
112
|
Chris@16
|
113 //! Create a time object from an initialized FILETIME struct.
|
Chris@16
|
114 /*!
|
Chris@16
|
115 * Create a time object from an initialized FILETIME struct.
|
Chris@16
|
116 * A FILETIME struct holds 100-nanosecond units (0.0000001). When
|
Chris@16
|
117 * built with microsecond resolution the file_time's sub second value
|
Chris@16
|
118 * will be truncated. Nanosecond resolution has no truncation.
|
Chris@16
|
119 *
|
Chris@16
|
120 * \note The function is templated on the FILETIME type, so that
|
Chris@16
|
121 * it can be used with both native FILETIME and the ad-hoc
|
Chris@16
|
122 * boost::date_time::winapi::file_time type.
|
Chris@16
|
123 */
|
Chris@16
|
124 template< typename TimeT, typename FileTimeT >
|
Chris@16
|
125 inline
|
Chris@16
|
126 TimeT time_from_ftime(const FileTimeT& ft)
|
Chris@16
|
127 {
|
Chris@16
|
128 typedef typename TimeT::date_type date_type;
|
Chris@16
|
129 typedef typename TimeT::date_duration_type date_duration_type;
|
Chris@16
|
130 typedef typename TimeT::time_duration_type time_duration_type;
|
Chris@16
|
131
|
Chris@16
|
132 // https://svn.boost.org/trac/boost/ticket/2523
|
Chris@16
|
133 // Since this function can be called with arbitrary times, including ones that
|
Chris@16
|
134 // are before 1970-Jan-01, we'll have to cast the time a bit differently,
|
Chris@16
|
135 // than it is done in the file_time_to_microseconds function. This allows to
|
Chris@16
|
136 // avoid integer wrapping for dates before 1970-Jan-01.
|
Chris@16
|
137 union {
|
Chris@16
|
138 FileTimeT as_file_time;
|
Chris@16
|
139 uint64_t as_integer; // 100-nanos since 1601-Jan-01
|
Chris@16
|
140 } caster;
|
Chris@16
|
141 caster.as_file_time = ft;
|
Chris@16
|
142
|
Chris@16
|
143 uint64_t sec = caster.as_integer / 10000000UL;
|
Chris@16
|
144 uint32_t sub_sec = (caster.as_integer % 10000000UL) // 100-nanoseconds since the last second
|
Chris@16
|
145 #if !defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
|
Chris@16
|
146 / 10; // microseconds since the last second
|
Chris@16
|
147 #else
|
Chris@16
|
148 * 100; // nanoseconds since the last second
|
Chris@16
|
149 #endif
|
Chris@16
|
150
|
Chris@16
|
151 // split sec into usable chunks: days, hours, minutes, & seconds
|
Chris@16
|
152 const uint32_t sec_per_day = 86400; // seconds per day
|
Chris@16
|
153 uint32_t days = static_cast< uint32_t >(sec / sec_per_day);
|
Chris@16
|
154 uint32_t tmp = static_cast< uint32_t >(sec % sec_per_day);
|
Chris@16
|
155 uint32_t hours = tmp / 3600; // sec_per_hour
|
Chris@16
|
156 tmp %= 3600;
|
Chris@16
|
157 uint32_t minutes = tmp / 60; // sec_per_min
|
Chris@16
|
158 tmp %= 60;
|
Chris@16
|
159 uint32_t seconds = tmp; // seconds
|
Chris@16
|
160
|
Chris@16
|
161 date_duration_type dd(days);
|
Chris@16
|
162 date_type d = date_type(1601, Jan, 01) + dd;
|
Chris@16
|
163 return TimeT(d, time_duration_type(hours, minutes, seconds, sub_sec));
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 }} // boost::date_time
|
Chris@16
|
167
|
Chris@16
|
168 #endif // BOOST_HAS_FTIME
|
Chris@16
|
169
|
Chris@16
|
170 #endif // DATE_TIME_FILETIME_FUNCTIONS_HPP__
|