1 #ifndef DATE_TIME_FILETIME_FUNCTIONS_HPP__
2 #define DATE_TIME_FILETIME_FUNCTIONS_HPP__
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
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.
18 #include <boost/date_time/compiler_config.hpp>
20 #if defined(BOOST_HAS_FTIME) // skip this file if no FILETIME
22 #if defined(BOOST_USE_WINDOWS_H)
26 #include <boost/cstdint.hpp>
27 #include <boost/date_time/time.hpp>
28 #include <boost/date_time/date_defs.hpp>
36 #if !defined(BOOST_USE_WINDOWS_H)
42 boost::uint32_t dwLowDateTime;
43 boost::uint32_t dwHighDateTime;
47 boost::uint16_t wYear;
48 boost::uint16_t wMonth;
49 boost::uint16_t wDayOfWeek;
51 boost::uint16_t wHour;
52 boost::uint16_t wMinute;
53 boost::uint16_t wSecond;
54 boost::uint16_t wMilliseconds;
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);
64 #endif // defined(BOOST_USE_WINDOWS_H)
66 typedef FILETIME file_time;
67 typedef SYSTEMTIME system_time;
69 inline void get_system_time_as_file_time(file_time& ft)
71 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
72 // Some runtime library implementations expect local times as the norm for ctime.
74 GetSystemTimeAsFileTime(&ft_utc);
75 FileTimeToLocalFileTime(&ft_utc, &ft);
76 #elif defined(BOOST_HAS_GETSYSTEMTIMEASFILETIME)
77 GetSystemTimeAsFileTime(&ft);
81 SystemTimeToFileTime(&st, &ft);
86 * The function converts file_time into number of microseconds elapsed since 1970-Jan-01
88 * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped.
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.
94 template< typename FileTimeT >
95 inline boost::uint64_t file_time_to_microseconds(FileTimeT const& ft)
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
102 FileTimeT as_file_time;
103 uint64_t as_integer; // 100-nanos since 1601-Jan-01
105 caster.as_file_time = ft;
107 caster.as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01
108 return (caster.as_integer / 10); // truncate to microseconds
111 } // namespace winapi
113 //! Create a time object from an initialized FILETIME struct.
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.
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.
124 template< typename TimeT, typename FileTimeT >
126 TimeT time_from_ftime(const FileTimeT& ft)
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;
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.
138 FileTimeT as_file_time;
139 uint64_t as_integer; // 100-nanos since 1601-Jan-01
141 caster.as_file_time = ft;
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
148 * 100; // nanoseconds since the last second
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
157 uint32_t minutes = tmp / 60; // sec_per_min
159 uint32_t seconds = tmp; // seconds
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));
166 }} // boost::date_time
168 #endif // BOOST_HAS_FTIME
170 #endif // DATE_TIME_FILETIME_FUNCTIONS_HPP__