1 /*************************************************************************
6 * [2011] - [2016] Realm Inc
9 * NOTICE: All information contained herein is, and remains
10 * the property of Realm Incorporated and its suppliers,
11 * if any. The intellectual and technical concepts contained
12 * herein are proprietary to Realm Incorporated
13 * and its suppliers and may be covered by U.S. and Foreign Patents,
14 * patents in process, and are protected by trade secret or copyright law.
15 * Dissemination of this information or reproduction of this material
16 * is strictly forbidden unless prior written permission is obtained
17 * from Realm Incorporated.
19 **************************************************************************/
21 #ifndef REALM_UTIL_TIME_HPP
22 #define REALM_UTIL_TIME_HPP
35 /// Thread safe version of std::localtime(). Uses localtime_r() on POSIX.
36 std::tm localtime(std::time_t);
38 /// Thread safe version of std::gmtime(). Uses gmtime_r() on POSIX.
39 std::tm gmtime(std::time_t);
41 /// Similar to std::put_time() from <iomanip>. See std::put_time() for
42 /// information about the format string. This function is provided because
43 /// std::put_time() is unavailable in GCC 4. This function is thread safe.
45 /// The default format is ISO 8601 date and time.
46 template<class C, class T>
47 void put_time(std::basic_ostream<C,T>&, const std::tm&, const C* format = "%FT%T%z");
49 /// @{ These functions combine localtime() or gmtime() with put_time() and
50 /// std::ostringstream. For detals on the format string, see
51 /// std::put_time(). These function are thread safe.
52 std::string format_local_time(std::time_t, const char* format = "%FT%T%z");
53 std::string format_utc_time(std::time_t, const char* format = "%FT%T%z");
61 template<class C, class T>
62 inline void put_time(std::basic_ostream<C,T>& out, const std::tm& tm, const C* format)
64 const auto& facet = std::use_facet<std::time_put<C>>(out.getloc()); // Throws
65 facet.put(std::ostreambuf_iterator<C>(out), out, ' ', &tm,
66 format, format + T::length(format)); // Throws
69 inline std::string format_local_time(std::time_t time, const char* format)
71 std::tm tm = util::localtime(time);
72 std::ostringstream out;
73 util::put_time(out, tm, format); // Throws
74 return out.str(); // Throws
77 inline std::string format_utc_time(std::time_t time, const char* format)
79 std::tm tm = util::gmtime(time);
80 std::ostringstream out;
81 util::put_time(out, tm, format); // Throws
82 return out.str(); // Throws
88 #endif // REALM_UTIL_TIME_HPP