]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // posix/chrono.cpp --------------------------------------------------------------// |
2 | ||
3 | // Copyright Beman Dawes 2008 | |
4 | // Copyright Vicente J. Botet Escriba 2009 | |
5 | ||
6 | // Distributed under the Boost Software License, Version 1.0. | |
7 | // See http://www.boost.org/LICENSE_1_0.txt | |
8 | ||
9 | //----------------------------------------------------------------------------// | |
10 | // POSIX // | |
11 | //----------------------------------------------------------------------------// | |
12 | ||
13 | #include <time.h> // for clock_gettime | |
14 | #include <boost/assert.hpp> | |
15 | ||
16 | namespace boost | |
17 | { | |
18 | namespace chrono | |
19 | { | |
20 | ||
21 | system_clock::time_point system_clock::now() BOOST_NOEXCEPT | |
22 | { | |
23 | timespec ts; | |
24 | if ( ::clock_gettime( CLOCK_REALTIME, &ts ) ) | |
25 | { | |
26 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); | |
27 | } | |
28 | ||
29 | return time_point(duration( | |
30 | static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); | |
31 | } | |
32 | ||
33 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
34 | system_clock::time_point system_clock::now(system::error_code & ec) | |
35 | { | |
36 | timespec ts; | |
37 | if ( ::clock_gettime( CLOCK_REALTIME, &ts ) ) | |
38 | { | |
39 | if (BOOST_CHRONO_IS_THROWS(ec)) | |
40 | { | |
41 | boost::throw_exception( | |
42 | system::system_error( | |
43 | errno, | |
44 | BOOST_CHRONO_SYSTEM_CATEGORY, | |
45 | "chrono::system_clock" )); | |
46 | } | |
47 | else | |
48 | { | |
49 | ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); | |
50 | return time_point(); | |
51 | } | |
52 | } | |
53 | ||
54 | if (!BOOST_CHRONO_IS_THROWS(ec)) | |
55 | { | |
56 | ec.clear(); | |
57 | } | |
58 | return time_point(duration( | |
59 | static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); | |
60 | } | |
61 | #endif | |
62 | ||
63 | std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT | |
64 | { | |
65 | return static_cast<std::time_t>( t.time_since_epoch().count() / 1000000000 ); | |
66 | } | |
67 | ||
68 | system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT | |
69 | { | |
70 | return time_point(duration(static_cast<system_clock::rep>(t) * 1000000000)); | |
71 | } | |
72 | ||
73 | #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY | |
74 | ||
75 | steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT | |
76 | { | |
77 | timespec ts; | |
78 | if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) ) | |
79 | { | |
80 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); | |
81 | } | |
82 | ||
83 | return time_point(duration( | |
84 | static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); | |
85 | } | |
86 | ||
87 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
88 | steady_clock::time_point steady_clock::now(system::error_code & ec) | |
89 | { | |
90 | timespec ts; | |
91 | if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) ) | |
92 | { | |
93 | if (BOOST_CHRONO_IS_THROWS(ec)) | |
94 | { | |
95 | boost::throw_exception( | |
96 | system::system_error( | |
97 | errno, | |
98 | BOOST_CHRONO_SYSTEM_CATEGORY, | |
99 | "chrono::steady_clock" )); | |
100 | } | |
101 | else | |
102 | { | |
103 | ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); | |
104 | return time_point(); | |
105 | } | |
106 | } | |
107 | ||
108 | if (!BOOST_CHRONO_IS_THROWS(ec)) | |
109 | { | |
110 | ec.clear(); | |
111 | } | |
112 | return time_point(duration( | |
113 | static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); | |
114 | } | |
115 | #endif | |
116 | #endif | |
117 | ||
118 | } // namespace chrono | |
119 | } // namespace boost | |
120 | ||
121 |