1 | // Copyright (C) 2001-2003 |
---|---|
2 | // William E. Kempf |
3 | // Copyright (C) 2007-8 Anthony Williams |
4 | // |
5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
7 | |
8 | #ifndef BOOST_XTIME_WEK070601_HPP |
9 | #define BOOST_XTIME_WEK070601_HPP |
10 | |
11 | #include <boost/thread/detail/config.hpp> |
12 | #if defined BOOST_THREAD_USES_DATETIME |
13 | |
14 | #include <boost/cstdint.hpp> |
15 | #include <boost/thread/thread_time.hpp> |
16 | #include <boost/date_time/posix_time/conversion.hpp> |
17 | |
18 | #include <boost/config/abi_prefix.hpp> |
19 | |
20 | namespace boost { |
21 | |
22 | enum xtime_clock_types |
23 | { |
24 | TIME_UTC_=1 |
25 | // TIME_TAI, |
26 | // TIME_MONOTONIC, |
27 | // TIME_PROCESS, |
28 | // TIME_THREAD, |
29 | // TIME_LOCAL, |
30 | // TIME_SYNC, |
31 | // TIME_RESOLUTION |
32 | }; |
33 | |
34 | struct xtime |
35 | { |
36 | #if defined(BOOST_NO_INT64_T) |
37 | typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX |
38 | #else |
39 | typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX |
40 | #endif |
41 | |
42 | typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND |
43 | |
44 | xtime_sec_t sec; |
45 | xtime_nsec_t nsec; |
46 | |
47 | operator system_time() const |
48 | { |
49 | return boost::posix_time::from_time_t(0)+ |
50 | boost::posix_time::seconds(static_cast<long>(sec))+ |
51 | #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS |
52 | boost::posix_time::nanoseconds(nsec); |
53 | #else |
54 | boost::posix_time::microseconds((nsec+500)/1000); |
55 | #endif |
56 | } |
57 | |
58 | }; |
59 | |
60 | inline xtime get_xtime(boost::system_time const& abs_time) |
61 | { |
62 | xtime res; |
63 | boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0); |
64 | |
65 | res.sec=static_cast<xtime::xtime_sec_t>(time_since_epoch.total_seconds()); |
66 | res.nsec=static_cast<xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second())); |
67 | return res; |
68 | } |
69 | |
70 | inline int xtime_get(struct xtime* xtp, int clock_type) |
71 | { |
72 | if (clock_type == TIME_UTC_) |
73 | { |
74 | *xtp=get_xtime(get_system_time()); |
75 | return clock_type; |
76 | } |
77 | return 0; |
78 | } |
79 | |
80 | |
81 | inline int xtime_cmp(const xtime& xt1, const xtime& xt2) |
82 | { |
83 | if (xt1.sec == xt2.sec) |
84 | return (int)(xt1.nsec - xt2.nsec); |
85 | else |
86 | return (xt1.sec > xt2.sec) ? 1 : -1; |
87 | } |
88 | |
89 | } // namespace boost |
90 | |
91 | #include <boost/config/abi_suffix.hpp> |
92 | #endif |
93 | #endif //BOOST_XTIME_WEK070601_HPP |
94 |