PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/chrono/detail/inlined/posix/chrono.hpp

http://hadesmem.googlecode.com/
C++ Header | 124 lines | 97 code | 19 blank | 8 comment | 8 complexity | a0aaf6d1c42a1496bdfc53768c71e950 MD5 | raw file
  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
 15namespace boost
 16{
 17namespace chrono
 18{
 19
 20  system_clock::time_point system_clock::now()
 21  {
 22    timespec ts;
 23    if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
 24    {
 25        boost::throw_exception(
 26                system::system_error( 
 27                        errno, 
 28                        BOOST_CHRONO_SYSTEM_CATEGORY, 
 29                        "chrono::system_clock" ));
 30    }
 31
 32    return time_point(duration(
 33      static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
 34  }
 35
 36  system_clock::time_point system_clock::now(system::error_code & ec)
 37  {
 38    timespec ts;
 39    if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
 40    {
 41        if (BOOST_CHRONO_IS_THROWS(ec))
 42        {
 43            boost::throw_exception(
 44                    system::system_error( 
 45                            errno, 
 46                            BOOST_CHRONO_SYSTEM_CATEGORY, 
 47                            "chrono::system_clock" ));
 48        }
 49        else
 50        {
 51            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
 52            return time_point();
 53        }
 54    }
 55
 56    if (!BOOST_CHRONO_IS_THROWS(ec)) 
 57    {
 58        ec.clear();
 59    }
 60    return time_point(duration(
 61      static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
 62  }
 63
 64  std::time_t system_clock::to_time_t(const system_clock::time_point& t)
 65  {
 66      return static_cast<std::time_t>( t.time_since_epoch().count() / 1000000000 );
 67  }
 68
 69  system_clock::time_point system_clock::from_time_t(std::time_t t)
 70  {
 71      return time_point(duration(static_cast<system_clock::rep>(t) * 1000000000));
 72  }
 73
 74#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
 75
 76  steady_clock::time_point steady_clock::now()
 77  {
 78    timespec ts;
 79    if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
 80    {
 81        boost::throw_exception(
 82                system::system_error( 
 83                        errno, 
 84                        BOOST_CHRONO_SYSTEM_CATEGORY, 
 85                        "chrono::steady_clock" ));
 86    }
 87
 88    return time_point(duration(
 89      static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
 90  }
 91
 92  steady_clock::time_point steady_clock::now(system::error_code & ec)
 93  {
 94    timespec ts;
 95    if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
 96    {
 97        if (BOOST_CHRONO_IS_THROWS(ec))
 98        {
 99            boost::throw_exception(
100                    system::system_error( 
101                            errno, 
102                            BOOST_CHRONO_SYSTEM_CATEGORY, 
103                            "chrono::steady_clock" ));
104        }
105        else
106        {
107            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
108            return time_point();
109        }
110    }
111
112    if (!BOOST_CHRONO_IS_THROWS(ec)) 
113    {
114        ec.clear();
115    }
116    return time_point(duration(
117      static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
118  }
119#endif
120
121}  // namespace chrono
122}  // namespace boost
123
124