#include "bookmouse_time.hpp" #include namespace bookmouse { using fud::FudStatus; using fud::Result; TimeFormat::TimeFormat(const char* format) : m_format{format}, m_sizeNeeded{m_format.length()} { if (m_format.utf8Valid()) { m_utf8Valid = true; } } TimeFormat::TimeFormat(const fud::String& format) : m_format{format}, m_sizeNeeded{m_format.length()} { if (m_format.utf8Valid()) { m_utf8Valid = true; } } fud::Result TimeFormat::format(const TimeInfo& timeInfo) { using RetType = fud::Result; if (!m_utf8Valid || m_format.length() < 1) { return RetType::error(FudStatus::ObjectInvalid); } std::vector output; size_t resultSize = 0; constexpr size_t maxSize = 1024; while (resultSize == 0 && m_sizeNeeded <= maxSize) { output.resize(m_sizeNeeded); #if defined __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" #elif defined __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif resultSize = std::strftime(output.data(), output.size(), m_format.c_str(), &timeInfo); #if defined __clang__ #pragma clang diagnostic pop #elif defined __GNUC__ #pragma GCC diagnostic pop #endif if (resultSize == 0) { if (m_sizeNeeded <= 512) { m_sizeNeeded *= 2; } else { m_sizeNeeded = maxSize; } } } if (resultSize == 0) { return RetType::error(FudStatus::Failure); } return RetType::okay(fud::String(output.data())); } } // namespace bookmouse