]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/chrono/detail/inlined/posix/chrono.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / chrono / detail / inlined / posix / chrono.hpp
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
15 namespace boost
16 {
17 namespace chrono
18 {
19
20   system_clock::time_point system_clock::now() BOOST_CHRONO_NOEXCEPT
21   {
22     timespec ts;
23     if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
24     {
25       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
26     }
27
28     return time_point(duration(
29       static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
30   }
31
32 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
33   system_clock::time_point system_clock::now(system::error_code & ec)
34   {
35     timespec ts;
36     if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
37     {
38         if (BOOST_CHRONO_IS_THROWS(ec))
39         {
40             boost::throw_exception(
41                     system::system_error( 
42                             errno, 
43                             BOOST_CHRONO_SYSTEM_CATEGORY, 
44                             "chrono::system_clock" ));
45         }
46         else
47         {
48             ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
49             return time_point();
50         }
51     }
52
53     if (!BOOST_CHRONO_IS_THROWS(ec)) 
54     {
55         ec.clear();
56     }
57     return time_point(duration(
58       static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
59   }
60 #endif
61
62   std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_CHRONO_NOEXCEPT
63   {
64       return static_cast<std::time_t>( t.time_since_epoch().count() / 1000000000 );
65   }
66
67   system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_CHRONO_NOEXCEPT
68   {
69       return time_point(duration(static_cast<system_clock::rep>(t) * 1000000000));
70   }
71
72 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
73
74   steady_clock::time_point steady_clock::now() BOOST_CHRONO_NOEXCEPT
75   {
76     timespec ts;
77     if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
78     {
79       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
80     }
81
82     return time_point(duration(
83       static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
84   }
85
86 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
87   steady_clock::time_point steady_clock::now(system::error_code & ec)
88   {
89     timespec ts;
90     if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
91     {
92         if (BOOST_CHRONO_IS_THROWS(ec))
93         {
94             boost::throw_exception(
95                     system::system_error( 
96                             errno, 
97                             BOOST_CHRONO_SYSTEM_CATEGORY, 
98                             "chrono::steady_clock" ));
99         }
100         else
101         {
102             ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
103             return time_point();
104         }
105     }
106
107     if (!BOOST_CHRONO_IS_THROWS(ec)) 
108     {
109         ec.clear();
110     }
111     return time_point(duration(
112       static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
113   }
114 #endif
115 #endif
116
117 }  // namespace chrono
118 }  // namespace boost
119
120