blob: c7791682c4983f954723ce30380cc0898075a680 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
|