blob: 3f58de83ba3bc1104a0e1d02de7bff9879a8f38a (
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "BasicValues.hpp"
#include <charconv>
#include <limits>
bool NumericValue::IsInstance(const BaseValue* value)
{
return value->GetKind() == KD_Numeric;
}
NumericValue::NumericValue()
: BaseValue(BaseValue::KD_Numeric)
{
}
template <class T>
static std::string NumberToString(T value)
{
constexpr auto kSize = std::numeric_limits<T>::max_digits10;
char buf[kSize];
auto res = std::to_chars(buf, buf + kSize, value);
if (res.ec == std::errc()) {
return std::string(buf, res.ptr);
} else {
return "<err>";
}
}
std::string NumericValue::GetTruncatedString() const
{
return ::NumberToString((int64_t)mValue);
}
std::string NumericValue::GetRoundedString() const
{
return ::NumberToString((int64_t)std::round(mValue));
}
std::string NumericValue::GetString() const
{
return ::NumberToString(mValue);
}
int64_t NumericValue::GetInt() const
{
return static_cast<int64_t>(mValue);
}
double NumericValue::GetValue() const
{
return mValue;
}
void NumericValue::SetValue(double value)
{
mValue = value;
}
bool TextValue::IsInstance(const BaseValue* value)
{
return value->GetKind() == KD_Text;
}
TextValue::TextValue()
: BaseValue(BaseValue::KD_Text)
{
}
const std::string& TextValue::GetValue() const
{
return mValue;
}
void TextValue::SetValue(const std::string& value)
{
mValue = value;
}
bool DateTimeValue::IsInstance(const BaseValue* value)
{
return value->GetKind() == KD_DateTime;
}
DateTimeValue::DateTimeValue()
: BaseValue(BaseValue::KD_DateTime)
{
}
std::string DateTimeValue::GetString() const
{
namespace chrono = std::chrono;
auto t = chrono::system_clock::to_time_t(mValue);
char data[32];
std::strftime(data, sizeof(data), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
return std::string(data);
}
const std::chrono::time_point<std::chrono::system_clock>& DateTimeValue::GetValue() const
{
return mValue;
}
void DateTimeValue::SetValue(const std::chrono::time_point<std::chrono::system_clock>& value)
{
mValue = value;
}
|