Chris@16: #ifndef DATE_TIME_FILETIME_FUNCTIONS_HPP__ Chris@16: #define DATE_TIME_FILETIME_FUNCTIONS_HPP__ Chris@16: Chris@16: /* Copyright (c) 2004 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: /*! @file filetime_functions.hpp Chris@16: * Function(s) for converting between a FILETIME structure and a Chris@16: * time object. This file is only available on systems that have Chris@16: * BOOST_HAS_FTIME defined. Chris@16: */ Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_HAS_FTIME) // skip this file if no FILETIME Chris@16: Chris@16: #if defined(BOOST_USE_WINDOWS_H) Chris@16: # include Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace date_time { Chris@16: Chris@16: namespace winapi { Chris@16: Chris@16: #if !defined(BOOST_USE_WINDOWS_H) Chris@16: Chris@16: extern "C" { Chris@16: Chris@16: struct FILETIME Chris@16: { Chris@16: boost::uint32_t dwLowDateTime; Chris@16: boost::uint32_t dwHighDateTime; Chris@16: }; Chris@16: struct SYSTEMTIME Chris@16: { Chris@16: boost::uint16_t wYear; Chris@16: boost::uint16_t wMonth; Chris@16: boost::uint16_t wDayOfWeek; Chris@16: boost::uint16_t wDay; Chris@16: boost::uint16_t wHour; Chris@16: boost::uint16_t wMinute; Chris@16: boost::uint16_t wSecond; Chris@16: boost::uint16_t wMilliseconds; Chris@16: }; Chris@16: Chris@16: __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(FILETIME* lpFileTime); Chris@16: __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const FILETIME* lpFileTime, FILETIME* lpLocalFileTime); Chris@16: __declspec(dllimport) void __stdcall GetSystemTime(SYSTEMTIME* lpSystemTime); Chris@16: __declspec(dllimport) int __stdcall SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, FILETIME* lpFileTime); Chris@16: Chris@16: } // extern "C" Chris@16: Chris@16: #endif // defined(BOOST_USE_WINDOWS_H) Chris@16: Chris@16: typedef FILETIME file_time; Chris@16: typedef SYSTEMTIME system_time; Chris@16: Chris@16: inline void get_system_time_as_file_time(file_time& ft) Chris@16: { Chris@16: #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) Chris@16: // Some runtime library implementations expect local times as the norm for ctime. Chris@16: file_time ft_utc; Chris@16: GetSystemTimeAsFileTime(&ft_utc); Chris@16: FileTimeToLocalFileTime(&ft_utc, &ft); Chris@16: #elif defined(BOOST_HAS_GETSYSTEMTIMEASFILETIME) Chris@16: GetSystemTimeAsFileTime(&ft); Chris@16: #else Chris@16: system_time st; Chris@16: GetSystemTime(&st); Chris@16: SystemTimeToFileTime(&st, &ft); Chris@16: #endif Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function converts file_time into number of microseconds elapsed since 1970-Jan-01 Chris@16: * Chris@16: * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped. Chris@16: * Chris@16: * \note The function is templated on the FILETIME type, so that Chris@16: * it can be used with both native FILETIME and the ad-hoc Chris@16: * boost::date_time::winapi::file_time type. Chris@16: */ Chris@16: template< typename FileTimeT > Chris@16: inline boost::uint64_t file_time_to_microseconds(FileTimeT const& ft) Chris@16: { Chris@16: /* shift is difference between 1970-Jan-01 & 1601-Jan-01 Chris@16: * in 100-nanosecond intervals */ Chris@16: const uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008 Chris@16: Chris@16: union { Chris@16: FileTimeT as_file_time; Chris@16: uint64_t as_integer; // 100-nanos since 1601-Jan-01 Chris@16: } caster; Chris@16: caster.as_file_time = ft; Chris@16: Chris@16: caster.as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01 Chris@16: return (caster.as_integer / 10); // truncate to microseconds Chris@16: } Chris@16: Chris@16: } // namespace winapi Chris@16: Chris@16: //! Create a time object from an initialized FILETIME struct. Chris@16: /*! Chris@16: * Create a time object from an initialized FILETIME struct. Chris@16: * A FILETIME struct holds 100-nanosecond units (0.0000001). When Chris@16: * built with microsecond resolution the file_time's sub second value Chris@16: * will be truncated. Nanosecond resolution has no truncation. Chris@16: * Chris@16: * \note The function is templated on the FILETIME type, so that Chris@16: * it can be used with both native FILETIME and the ad-hoc Chris@16: * boost::date_time::winapi::file_time type. Chris@16: */ Chris@16: template< typename TimeT, typename FileTimeT > Chris@16: inline Chris@16: TimeT time_from_ftime(const FileTimeT& ft) Chris@16: { Chris@16: typedef typename TimeT::date_type date_type; Chris@16: typedef typename TimeT::date_duration_type date_duration_type; Chris@16: typedef typename TimeT::time_duration_type time_duration_type; Chris@16: Chris@16: // https://svn.boost.org/trac/boost/ticket/2523 Chris@16: // Since this function can be called with arbitrary times, including ones that Chris@16: // are before 1970-Jan-01, we'll have to cast the time a bit differently, Chris@16: // than it is done in the file_time_to_microseconds function. This allows to Chris@16: // avoid integer wrapping for dates before 1970-Jan-01. Chris@16: union { Chris@16: FileTimeT as_file_time; Chris@16: uint64_t as_integer; // 100-nanos since 1601-Jan-01 Chris@16: } caster; Chris@16: caster.as_file_time = ft; Chris@16: Chris@16: uint64_t sec = caster.as_integer / 10000000UL; Chris@16: uint32_t sub_sec = (caster.as_integer % 10000000UL) // 100-nanoseconds since the last second Chris@16: #if !defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG) Chris@16: / 10; // microseconds since the last second Chris@16: #else Chris@16: * 100; // nanoseconds since the last second Chris@16: #endif Chris@16: Chris@16: // split sec into usable chunks: days, hours, minutes, & seconds Chris@16: const uint32_t sec_per_day = 86400; // seconds per day Chris@16: uint32_t days = static_cast< uint32_t >(sec / sec_per_day); Chris@16: uint32_t tmp = static_cast< uint32_t >(sec % sec_per_day); Chris@16: uint32_t hours = tmp / 3600; // sec_per_hour Chris@16: tmp %= 3600; Chris@16: uint32_t minutes = tmp / 60; // sec_per_min Chris@16: tmp %= 60; Chris@16: uint32_t seconds = tmp; // seconds Chris@16: Chris@16: date_duration_type dd(days); Chris@16: date_type d = date_type(1601, Jan, 01) + dd; Chris@16: return TimeT(d, time_duration_type(hours, minutes, seconds, sub_sec)); Chris@16: } Chris@16: Chris@16: }} // boost::date_time Chris@16: Chris@16: #endif // BOOST_HAS_FTIME Chris@16: Chris@16: #endif // DATE_TIME_FILETIME_FUNCTIONS_HPP__