From 71423e0e94184a18d54b7df5b4c3aea00ea6af55 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 9 Aug 2021 00:25:20 -0400 Subject: [PATCH] Add string::formatTime/formatTimeISO(). --- adapters/standalone.cpp | 2 +- include/string.hpp | 3 +++ src/string.cpp | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/adapters/standalone.cpp b/adapters/standalone.cpp index f73bba66..af35f2c4 100644 --- a/adapters/standalone.cpp +++ b/adapters/standalone.cpp @@ -140,7 +140,7 @@ int main(int argc, char* argv[]) { #if defined ARCH_MAC INFO("Bundle path: %s", asset::bundlePath.c_str()); #endif - INFO("System time: %lf", system::getUnixTime()); + INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str()); // Load settings settings::init(); diff --git a/include/string.hpp b/include/string.hpp index 4d2342b6..ea05b326 100644 --- a/include/string.hpp +++ b/include/string.hpp @@ -79,6 +79,9 @@ Examples: */ std::vector split(const std::string& s, const std::string& seperator, size_t maxTokens = 0); +std::string formatTime(const char* format, double timestamp); +std::string formatTimeISO(double timestamp); + #if defined ARCH_WIN /** Performs a Unicode string conversion from UTF-16 to UTF-8. diff --git a/src/string.cpp b/src/string.cpp index eaf371a5..fe69a824 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1,3 +1,4 @@ +#include #include // for tolower and toupper #include // for transform and equal #include // for dirname and basename @@ -234,6 +235,18 @@ std::vector split(const std::string& s, const std::string& separato } +std::string formatTime(const char* format, double timestamp) { + time_t t = timestamp; + char str[1024]; + size_t s = std::strftime(str, sizeof(str), format, std::localtime(&t)); + return std::string(str, s); +} + +std::string formatTimeISO(double timestamp) { + return formatTime("%FT%T%z", timestamp); +} + + #if defined ARCH_WIN std::string UTF16toUTF8(const std::wstring& w) { if (w.empty())