c_time.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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$
  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. This struct may be used to store the resulting time.
  39. * The returned pointer may or may not point to this struct, however,
  40. * it will point to the result of the corresponding function.
  41. * All functions do proper checking of the C function results and throw
  42. * exceptions on error. Therefore the functions will never return NULL.
  43. */
  44. struct c_time {
  45. public:
  46. #if defined(BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS)
  47. //! requires a pointer to a user created std::tm struct
  48. inline
  49. static std::tm* localtime(const std::time_t* t, std::tm* result)
  50. {
  51. // localtime_r() not in namespace std???
  52. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  53. std::tm tmp;
  54. if(!localtime_r(t,&tmp))
  55. result = 0;
  56. else
  57. *result = tmp;
  58. #else
  59. result = localtime_r(t, result);
  60. #endif
  61. if (!result)
  62. boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
  63. return result;
  64. }
  65. //! requires a pointer to a user created std::tm struct
  66. inline
  67. static std::tm* gmtime(const std::time_t* t, std::tm* result)
  68. {
  69. // gmtime_r() not in namespace std???
  70. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  71. std::tm tmp;
  72. if(!gmtime_r(t,&tmp))
  73. result = 0;
  74. else
  75. *result = tmp;
  76. #else
  77. result = gmtime_r(t, result);
  78. #endif
  79. if (!result)
  80. boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
  81. return result;
  82. }
  83. #else // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  84. #if defined(__clang__) // Clang has to be checked before MSVC
  85. #pragma clang diagnostic push
  86. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  87. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  88. #pragma warning(push) // preserve warning settings
  89. #pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8
  90. #endif
  91. //! requires a pointer to a user created std::tm struct
  92. inline
  93. static std::tm* localtime(const std::time_t* t, std::tm* result)
  94. {
  95. result = std::localtime(t);
  96. if (!result)
  97. boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
  98. return result;
  99. }
  100. //! requires a pointer to a user created std::tm struct
  101. inline
  102. static std::tm* gmtime(const std::time_t* t, std::tm* result)
  103. {
  104. result = std::gmtime(t);
  105. if (!result)
  106. boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
  107. return result;
  108. }
  109. #if defined(__clang__) // Clang has to be checked before MSVC
  110. #pragma clang diagnostic pop
  111. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400))
  112. #pragma warning(pop) // restore warnings to previous state
  113. #endif
  114. #endif // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  115. };
  116. }} // namespaces
  117. #endif // DATE_TIME_C_TIME_HPP___