c_time.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(_MSC_VER) && (_MSC_VER >= 1400))
  85. #pragma warning(push) // preserve warning settings
  86. #pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8
  87. #endif // _MSC_VER >= 1400
  88. //! requires a pointer to a user created std::tm struct
  89. inline
  90. static std::tm* localtime(const std::time_t* t, std::tm* result)
  91. {
  92. result = std::localtime(t);
  93. if (!result)
  94. boost::throw_exception(std::runtime_error("could not convert calendar time to local time"));
  95. return result;
  96. }
  97. //! requires a pointer to a user created std::tm struct
  98. inline
  99. static std::tm* gmtime(const std::time_t* t, std::tm* result)
  100. {
  101. result = std::gmtime(t);
  102. if (!result)
  103. boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time"));
  104. return result;
  105. }
  106. #if (defined(_MSC_VER) && (_MSC_VER >= 1400))
  107. #pragma warning(pop) // restore warnings to previous state
  108. #endif // _MSC_VER >= 1400
  109. #endif // BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS
  110. };
  111. }} // namespaces
  112. #endif // DATE_TIME_C_TIME_HPP___