Chris@16: #ifndef DATE_TIME_C_TIME_HPP___ Chris@16: #define DATE_TIME_C_TIME_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: Chris@16: /*! @file c_time.hpp Chris@16: Provide workarounds related to the ctime header Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include // to be able to convert from string literals to exceptions Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: //Work around libraries that don't put time_t and time in namespace std Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std { using ::time_t; using ::time; using ::localtime; Chris@16: using ::tm; using ::gmtime; } Chris@16: #endif // BOOST_NO_STDC_NAMESPACE Chris@16: Chris@16: //The following is used to support high precision time clocks Chris@16: #ifdef BOOST_HAS_GETTIMEOFDAY Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #ifdef BOOST_HAS_FTIME Chris@16: #include Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: //! Provides a uniform interface to some 'ctime' functions Chris@16: /*! Provides a uniform interface to some ctime functions and Chris@16: * their '_r' counterparts. The '_r' functions require a pointer to a Chris@16: * user created std::tm struct whereas the regular functions use a Chris@16: * staticly created struct and return a pointer to that. These wrapper Chris@16: * functions require the user to create a std::tm struct and send in a Chris@16: * pointer to it. This struct may be used to store the resulting time. Chris@16: * The returned pointer may or may not point to this struct, however, Chris@16: * it will point to the result of the corresponding function. Chris@16: * All functions do proper checking of the C function results and throw Chris@16: * exceptions on error. Therefore the functions will never return NULL. Chris@16: */ Chris@16: struct c_time { Chris@16: public: Chris@16: #if defined(BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS) Chris@16: //! requires a pointer to a user created std::tm struct Chris@16: inline Chris@16: static std::tm* localtime(const std::time_t* t, std::tm* result) Chris@16: { Chris@16: // localtime_r() not in namespace std??? Chris@16: #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64 Chris@16: std::tm tmp; Chris@16: if(!localtime_r(t,&tmp)) Chris@16: result = 0; Chris@16: else Chris@16: *result = tmp; Chris@16: #else Chris@16: result = localtime_r(t, result); Chris@16: #endif Chris@16: if (!result) Chris@16: boost::throw_exception(std::runtime_error("could not convert calendar time to local time")); Chris@16: return result; Chris@16: } Chris@16: //! requires a pointer to a user created std::tm struct Chris@16: inline Chris@16: static std::tm* gmtime(const std::time_t* t, std::tm* result) Chris@16: { Chris@16: // gmtime_r() not in namespace std??? Chris@16: #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64 Chris@16: std::tm tmp; Chris@16: if(!gmtime_r(t,&tmp)) Chris@16: result = 0; Chris@16: else Chris@16: *result = tmp; Chris@16: #else Chris@16: result = gmtime_r(t, result); Chris@16: #endif Chris@16: if (!result) Chris@16: boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time")); Chris@16: return result; Chris@16: } Chris@101: #else // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS Chris@16: Chris@16: #if (defined(_MSC_VER) && (_MSC_VER >= 1400)) Chris@16: #pragma warning(push) // preserve warning settings Chris@16: #pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8 Chris@16: #endif // _MSC_VER >= 1400 Chris@16: //! requires a pointer to a user created std::tm struct Chris@16: inline Chris@16: static std::tm* localtime(const std::time_t* t, std::tm* result) Chris@16: { Chris@16: result = std::localtime(t); Chris@16: if (!result) Chris@16: boost::throw_exception(std::runtime_error("could not convert calendar time to local time")); Chris@16: return result; Chris@16: } Chris@16: //! requires a pointer to a user created std::tm struct Chris@16: inline Chris@16: static std::tm* gmtime(const std::time_t* t, std::tm* result) Chris@16: { Chris@16: result = std::gmtime(t); Chris@16: if (!result) Chris@16: boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time")); Chris@16: return result; Chris@16: } Chris@16: #if (defined(_MSC_VER) && (_MSC_VER >= 1400)) Chris@16: #pragma warning(pop) // restore warnings to previous state Chris@16: #endif // _MSC_VER >= 1400 Chris@16: Chris@101: #endif // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS Chris@16: }; Chris@16: }} // namespaces Chris@16: Chris@16: #endif // DATE_TIME_C_TIME_HPP___