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