Skip to content

Commit

Permalink
Refactor std::put_time detection
Browse files Browse the repository at this point in the history
Hopefully this will allow both clang and GCC to work on Travis.
  • Loading branch information
CelticMinstrel committed Dec 16, 2016
1 parent f2f8808 commit 4d7b229
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/gettext_boost.cpp
Expand Up @@ -437,10 +437,10 @@ std::string strftime(const std::string& format, const std::tm* time)
std::basic_ostringstream<char> dummy;
dummy.imbue(get_manager().get_locale());
// See utils/io.hpp for explanation of this check
#if (defined(__clang__) && !__has_include(<experimental/any>)) || (defined(__GNUC__) && __GNUC__ < 5)
dummy << bl::as::ftime(format) << mktime(const_cast<std::tm*>(time));
#else
#if HAVE_PUT_TIME
dummy << std::put_time(time, format.c_str());
#else
dummy << bl::as::ftime(format) << mktime(const_cast<std::tm*>(time));
#endif

return dummy.str();
Expand Down
9 changes: 8 additions & 1 deletion src/global.hpp
Expand Up @@ -62,6 +62,7 @@

// Some C++11 features are not available on all supported platforms
#if defined(_MSC_VER)
#define HAVE_PUT_TIME 1
// MSVC supports these starting in MSVC 2015
#if _MSC_VER >= 1900
#define HAVE_REF_QUALIFIERS 1
Expand All @@ -77,6 +78,10 @@
#endif

#if defined(__clang__)
#include <ciso646> // To ensure standard library version macros are defined
// If it's libc++, no problem. Otherwise, attempt to detect libstdc++ version (needs GCC 5.1 or higher)
// by testing for the existence of a header added in that version.
#define HAVE_PUT_TIME (defined(_LIBCPP_VERSION) || __has_include(<experimental/any>))
// Clang has convenient feature detection macros \o/
#define HAVE_REF_QUALIFIERS __has_feature(cxx_reference_qualified_functions)
#define HAVE_INHERITING_CTORS __has_feature(cxx_inheriting_constructors)
Expand All @@ -97,7 +102,9 @@
#endif

#if defined(__GNUC__) && !defined(__clang__)
// GCC supports two of these from 4.6 up and the others from 4.8 up
// GCC 5 required for this
#define HAVE_PUT_TIME (__GNUC__ >= 5)
// GCC supports these from 4.8 up
#define CONSTEXPR constexpr
#define NOEXCEPT noexcept
#define NORETURN [[noreturn]]
Expand Down
4 changes: 3 additions & 1 deletion src/utils/io.hpp
Expand Up @@ -11,13 +11,15 @@ but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "global.hpp"

// The version of libstdc++ shipped with GCC 4.x does not have put_time in the <iomanip> header
// Thus if GCC is the compiler being used, we can simply check the compiler version.
// However, if clang is being used, this won't work.
// Instead, we check for the presence of the <experimental/any> header.
// This was introduced in GCC 5.1, so it's not a perfect check, but it appears to be the best available.
// (Boost also uses the same check internally.)
#if (defined(__clang__) && !__has_include(<experimental/any>)) || (defined(__GNUC__) && __GNUC__ < 5)
#if !HAVE_PUT_TIME

#include <ctime>

Expand Down

0 comments on commit 4d7b229

Please sign in to comment.