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