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