mirror of
https://github.com/catchorg/Catch2.git
synced 2025-10-09 04:05:39 +02:00
Outline part of formatting system_clock's time_point into cpp file
This commit is contained in:

committed by
Martin Hořeňovský

parent
a9223b2bb3
commit
a58df2d7c5
@@ -63,6 +63,30 @@ namespace Detail {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string formatTimeT(std::time_t time) {
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
std::tm timeInfo = {};
|
||||||
|
const auto err = gmtime_s( &timeInfo, &time );
|
||||||
|
if ( err ) {
|
||||||
|
return "gmtime from provided timepoint has failed. This "
|
||||||
|
"happens e.g. with pre-1970 dates using Microsoft libc";
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
std::tm* timeInfo = std::gmtime( &time );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto const timeStampSize = sizeof( "2017-01-16T17:06:45Z" );
|
||||||
|
char timeStamp[timeStampSize];
|
||||||
|
const char* const fmt = "%Y-%m-%dT%H:%M:%SZ";
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
std::strftime( timeStamp, timeStampSize, fmt, &timeInfo );
|
||||||
|
#else
|
||||||
|
std::strftime( timeStamp, timeStampSize, fmt, timeInfo );
|
||||||
|
#endif
|
||||||
|
return std::string( timeStamp, timeStampSize - 1 );
|
||||||
|
}
|
||||||
|
|
||||||
std::string convertIntoString(StringRef string, bool escapeInvisibles) {
|
std::string convertIntoString(StringRef string, bool escapeInvisibles) {
|
||||||
std::string ret;
|
std::string ret;
|
||||||
// This is enough for the "don't escape invisibles" case, and a good
|
// This is enough for the "don't escape invisibles" case, and a good
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
#ifndef CATCH_TOSTRING_HPP_INCLUDED
|
#ifndef CATCH_TOSTRING_HPP_INCLUDED
|
||||||
#define CATCH_TOSTRING_HPP_INCLUDED
|
#define CATCH_TOSTRING_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@@ -42,6 +42,8 @@ namespace Catch {
|
|||||||
|
|
||||||
std::size_t catch_strnlen(const char *str, std::size_t n);
|
std::size_t catch_strnlen(const char *str, std::size_t n);
|
||||||
|
|
||||||
|
std::string formatTimeT( std::time_t time );
|
||||||
|
|
||||||
constexpr StringRef unprintableString = "{?}"_sr;
|
constexpr StringRef unprintableString = "{?}"_sr;
|
||||||
|
|
||||||
//! Encases `string in quotes, and optionally escapes invisibles
|
//! Encases `string in quotes, and optionally escapes invisibles
|
||||||
@@ -629,28 +631,7 @@ struct ratio_string<std::milli> {
|
|||||||
const auto systemish = std::chrono::time_point_cast<
|
const auto systemish = std::chrono::time_point_cast<
|
||||||
std::chrono::system_clock::duration>( time_point );
|
std::chrono::system_clock::duration>( time_point );
|
||||||
const auto as_time_t = std::chrono::system_clock::to_time_t( systemish );
|
const auto as_time_t = std::chrono::system_clock::to_time_t( systemish );
|
||||||
|
return ::Catch::Detail::formatTimeT( as_time_t );
|
||||||
#ifdef _MSC_VER
|
|
||||||
std::tm timeInfo = {};
|
|
||||||
const auto err = gmtime_s( &timeInfo, &as_time_t );
|
|
||||||
if ( err ) {
|
|
||||||
return "gmtime from provided timepoint has failed. This "
|
|
||||||
"happens e.g. with pre-1970 dates using Microsoft libc";
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
std::tm* timeInfo = std::gmtime( &as_time_t );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");
|
|
||||||
char timeStamp[timeStampSize];
|
|
||||||
const char * const fmt = "%Y-%m-%dT%H:%M:%SZ";
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
|
|
||||||
#else
|
|
||||||
std::strftime(timeStamp, timeStampSize, fmt, timeInfo);
|
|
||||||
#endif
|
|
||||||
return std::string(timeStamp, timeStampSize - 1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user