fix doc example typo
[boost.git] / boost / thread / xtime.hpp
blob7cc6272d6a6c3da8e365f2ea59993f2b11e95132
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)
8 #ifndef BOOST_XTIME_WEK070601_HPP
9 #define BOOST_XTIME_WEK070601_HPP
11 #include <boost/thread/detail/config.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/thread/thread_time.hpp>
15 #include <boost/date_time/posix_time/conversion.hpp>
17 #include <boost/config/abi_prefix.hpp>
19 namespace boost {
21 enum xtime_clock_types
23 TIME_UTC=1
24 // TIME_TAI,
25 // TIME_MONOTONIC,
26 // TIME_PROCESS,
27 // TIME_THREAD,
28 // TIME_LOCAL,
29 // TIME_SYNC,
30 // TIME_RESOLUTION
33 struct xtime
35 #if defined(BOOST_NO_INT64_T)
36 typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX
37 #else
38 typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX
39 #endif
41 typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND
43 xtime_sec_t sec;
44 xtime_nsec_t nsec;
46 operator system_time() const
48 return boost::posix_time::from_time_t(0)+
49 boost::posix_time::seconds(static_cast<long>(sec))+
50 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
51 boost::posix_time::nanoseconds(nsec);
52 #else
53 boost::posix_time::microseconds((nsec+500)/1000);
54 #endif
59 inline xtime get_xtime(boost::system_time const& abs_time)
61 xtime res;
62 boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
64 res.sec=static_cast<xtime::xtime_sec_t>(time_since_epoch.total_seconds());
65 res.nsec=static_cast<xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()));
66 return res;
69 inline int xtime_get(struct xtime* xtp, int clock_type)
71 if (clock_type == TIME_UTC)
73 *xtp=get_xtime(get_system_time());
74 return clock_type;
76 return 0;
80 inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
82 if (xt1.sec == xt2.sec)
83 return (int)(xt1.nsec - xt2.nsec);
84 else
85 return (xt1.sec > xt2.sec) ? 1 : -1;
88 } // namespace boost
90 #include <boost/config/abi_suffix.hpp>
92 #endif //BOOST_XTIME_WEK070601_HPP