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 or http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2004/08/04 22:06:05 $ |
---|
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 | #if defined(BOOST_HAS_FTIME) // skip this file if no FILETIME |
---|
20 | #include <windows.h> |
---|
21 | #include <boost/cstdint.hpp> |
---|
22 | #include <boost/date_time/time.hpp> |
---|
23 | |
---|
24 | |
---|
25 | namespace boost { |
---|
26 | namespace date_time { |
---|
27 | |
---|
28 | |
---|
29 | //! Create a time object from an initialized FILETIME struct. |
---|
30 | /*! Create a time object from an initialized FILETIME struct. |
---|
31 | * A FILETIME struct holds 100-nanosecond units (0.0000001). When |
---|
32 | * built with microsecond resolution the FILETIME's sub second value |
---|
33 | * will be truncated. Nanosecond resolution has no truncation. */ |
---|
34 | template<class time_type> |
---|
35 | inline |
---|
36 | time_type time_from_ftime(const FILETIME& ft){ |
---|
37 | typedef typename time_type::date_type date_type; |
---|
38 | typedef typename time_type::date_duration_type date_duration_type; |
---|
39 | typedef typename time_type::time_duration_type time_duration_type; |
---|
40 | |
---|
41 | /* OFFSET is difference between 1970-Jan-01 & 1601-Jan-01 |
---|
42 | * in 100-nanosecond intervals */ |
---|
43 | uint64_t c1 = 27111902UL; |
---|
44 | uint64_t c2 = 3577643008UL; // issues warning without 'UL' |
---|
45 | const uint64_t OFFSET = (c1 << 32) + c2; |
---|
46 | const long sec_pr_day = 86400; // seconds per day |
---|
47 | |
---|
48 | uint64_t filetime = ft.dwHighDateTime; |
---|
49 | filetime <<= 32; |
---|
50 | filetime += ft.dwLowDateTime; |
---|
51 | filetime -= OFFSET; // filetime is now 100-nanos since 1970-Jan-01 |
---|
52 | |
---|
53 | uint64_t sec = filetime / 10000000; |
---|
54 | #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG) |
---|
55 | uint64_t sub_sec = (filetime % 10000000) * 100; // nanoseconds |
---|
56 | #else |
---|
57 | uint64_t sub_sec = (filetime % 10000000) / 10; // truncate to microseconds |
---|
58 | #endif |
---|
59 | |
---|
60 | // split sec into usable chunks: days, hours, minutes, & seconds |
---|
61 | long _d = sec / sec_pr_day; |
---|
62 | long tmp = sec % sec_pr_day; |
---|
63 | long _h = tmp / 3600; // sec_pr_hour |
---|
64 | tmp %= 3600; |
---|
65 | long _m = tmp / 60; // sec_pr_min |
---|
66 | tmp %= 60; |
---|
67 | long _s = tmp; // seconds |
---|
68 | |
---|
69 | date_duration_type dd(_d); |
---|
70 | date_type d = date_type(1970, Jan, 01) + dd; |
---|
71 | return time_type(d, time_duration_type(_h, _m, _s, sub_sec)); |
---|
72 | } |
---|
73 | |
---|
74 | }} // boost::date_time |
---|
75 | |
---|
76 | #endif // BOOST_HAS_FTIME |
---|
77 | |
---|
78 | #endif // DATE_TIME_FILETIME_FUNCTIONS_HPP__ |
---|