summaryrefslogtreecommitdiff
path: root/src/bookmouse_time.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bookmouse_time.cpp')
-rw-r--r--src/bookmouse_time.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/bookmouse_time.cpp b/src/bookmouse_time.cpp
new file mode 100644
index 0000000..c779168
--- /dev/null
+++ b/src/bookmouse_time.cpp
@@ -0,0 +1,70 @@
+#include "bookmouse_time.hpp"
+
+#include <vector>
+
+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<fud::String, fud::FudStatus> TimeFormat::format(const TimeInfo& timeInfo)
+{
+ using RetType = fud::Result<fud::String, fud::FudStatus>;
+
+ if (!m_utf8Valid || m_format.length() < 1) {
+ return RetType::error(FudStatus::ObjectInvalid);
+ }
+
+ std::vector<char> 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