better fix for use of put_time

Put time can’t be activated by _WEBSOCKETPP_CPP11_STL_ due to backwards
compatibility issues with GCC 4.7 and 4.8 which otherwise work with
that define but do not support put_time
This commit is contained in:
Peter Thorson
2014-11-03 09:48:43 -05:00
parent 82502a651a
commit 397f5c00bf
2 changed files with 34 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Peter Thorson. All rights reserved.
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -40,12 +40,29 @@
#define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
#endif
// The code below attempts to use information provided by the build system or
// user supplied defines to selectively enable C++11 language and library
// features. In most cases features that are targeted individually may also be
// selectively disabled via an associated _WEBSOCKETPP_NOXXX_ define.
#if defined(_WEBSOCKETPP_CPP11_STL_) || __cplusplus >= 201103L
// _WEBSOCKETPP_CPP11_STL_ is a flag from the build system that forces
// WebSocket++ into C++11 mode. __cplusplus is a define set by the compiler
// if it has full support for C++11 language features. If either are set use
// C++11 language features
#if defined(_WEBSOCKETPP_CPP11_STL_) || __cplusplus >= 201103L || defined(_WEBSOCKETPP_CPP11_STRICT_)
// This check tests for blanket c++11 coverage. It can be activated in one
// of three ways. Either the compiler itself reports that it is a full
// C++11 compiler via the __cplusplus macro or the user/build system
// supplies one of the two preprocessor defines below:
// _WEBSOCKETPP_CPP11_STRICT_
//
// This define reports to WebSocket++ that 100% of the language and library
// features of C++11 are available. Using this define on a non-C++11
// compiler will result in problems.
// _WEBSOCKETPP_CPP11_STL_
//
// This define enables *most* C++11 options that were implemented early on
// by compilers. It is typically used for compilers that have many, but not
// all C++11 features. It should be safe to use on GCC 4.7-4.8 and perhaps
// earlier.
#ifndef _WEBSOCKETPP_NOEXCEPT_TOKEN_
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_ noexcept
#endif
@@ -58,7 +75,17 @@
#ifndef _WEBSOCKETPP_NULLPTR_TOKEN_
#define _WEBSOCKETPP_NULLPTR_TOKEN_ nullptr
#endif
#ifndef _WEBSOCKETPP_CPP11_STL_
// a few extra things that real/full C++11 compilers have but the
// *almost* c++11 compilers like GCC 4.7/4.8 that otherwise work with
// _WEBSOCKETPP_CPP11_STL_ lack.
#define _WEBSOCKETPP_PUTTIME_
#endif
#else
// In the absence of a blanket define, try to use compiler versions or
// feature testing macros to selectively enable what we can.
// Test for noexcept
#ifndef _WEBSOCKETPP_NOEXCEPT_TOKEN_
#ifdef _WEBSOCKETPP_NOEXCEPT_

View File

@@ -133,8 +133,8 @@ private:
static std::ostream & timestamp(std::ostream & os) {
std::time_t t = std::time(NULL);
std::tm* lt = std::localtime(&t);
#ifdef _WEBSOCKETPP_CPP11_STL_
return os << std::put_time(lt,"%Y-%m-%d %H:%M:%S");
#ifdef _WEBSOCKETPP_PUTTIME_
#else // Falls back to strftime, which requires a temporary copy of the string.
char buffer[20];
std::strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",lt);