c_time.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef DATE_TIME_C_TIME_HPP___
  2. #define DATE_TIME_C_TIME_HPP___
  3. /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date: 2008-11-12 14:37:53 -0500 (Wed, 12 Nov 2008) $
  9. */
  10. /*! @file c_time.hpp
  11. Provide workarounds related to the ctime header
  12. */
  13. #include <ctime>
  14. #include <string> // to be able to convert from string literals to exceptions
  15. #include <stdexcept>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/date_time/compiler_config.hpp>
  18. //Work around libraries that don't put time_t and time in namespace std
  19. #ifdef BOOST_NO_STDC_NAMESPACE
  20. namespace std { using ::time_t; using ::time; using ::localtime;
  21. using ::tm; using ::gmtime; }
  22. #endif // BOOST_NO_STDC_NAMESPACE
  23. //The following is used to support high precision time clocks
  24. #ifdef BOOST_HAS_GETTIMEOFDAY
  25. #include <sys/time.h>
  26. #endif
  27. #ifdef BOOST_HAS_FTIME
  28. #include <time.h>
  29. #endif
  30. namespace boost {
  31. namespace date_time {
  32. //! Provides a uniform interface to some 'ctime' functions
  33. /*! Provides a uniform interface to some ctime functions and
  34. * their '_r' counterparts. The '_r' functions require a pointer to a
  35. * user created std::tm struct whereas the regular functions use a
  36. * staticly created struct and return a pointer to that. These wrapper
  37. * functions require the user to create a std::tm struct and send in a
  38. * pointer to it. A pointer to the user created struct will be returned.
  39. * All functions do proper checking of the C function results and throw
  40. * exceptions on error. Therefore the functions will never return NULL.
  41. */
  42. struct c_time {
  43. public:
  44. #if defined(BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS)
  45. //! requires a pointer to a user created std::tm struct
  46. inline
  47. static std::tm* localtime(const std::time_t* t, std::tm* result)
  48. {
  49. // localtime_r() not in namespace std???
  50. result = localtime_r(t, result);
  51. if (!result)
  52. boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
  53. return result;
  54. }
  55. //! requires a pointer to a user created std::tm struct
  56. inline
  57. static std::tm* gmtime(const std::time_t* t, std::tm* result)
  58. {
  59. // gmtime_r() not in namespace std???
  60. result = gmtime_r(t, result);
  61. if (!result)
  62. boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
  63. return result;
  64. }
  65. #else // BOOST_HAS_THREADS
  66. #if (defined(_MSC_VER) && (_MSC_VER >= 1400))
  67. #pragma warning(push) // preserve warning settings
  68. #pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8
  69. #endif // _MSC_VER >= 1400
  70. //! requires a pointer to a user created std::tm struct
  71. inline
  72. static std::tm* localtime(const std::time_t* t, std::tm* result)
  73. {
  74. result = std::localtime(t);
  75. if (!result)
  76. boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
  77. return result;
  78. }
  79. //! requires a pointer to a user created std::tm struct
  80. inline
  81. static std::tm* gmtime(const std::time_t* t, std::tm* result)
  82. {
  83. result = std::gmtime(t);
  84. if (!result)
  85. boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
  86. return result;
  87. }
  88. #if (defined(_MSC_VER) && (_MSC_VER >= 1400))
  89. #pragma warning(pop) // restore warnings to previous state
  90. #endif // _MSC_VER >= 1400
  91. #endif // BOOST_HAS_THREADS
  92. };
  93. }} // namespaces
  94. #endif // DATE_TIME_C_TIME_HPP___