Skip to content

Commit 33f8832

Browse files
committed
Start fixing date/time formatting in pattern data
1 parent 5f16903 commit 33f8832

File tree

4 files changed

+93
-69
lines changed

4 files changed

+93
-69
lines changed

lib/include/pl/pattern_language.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <wolv/io/fs.hpp>
2525
#include <wolv/container/interval_tree.hpp>
26+
#include <wolv/utils/date_time_format.hpp>
2627

2728
namespace pl {
2829

@@ -134,6 +135,16 @@ namespace pl {
134135
*/
135136
void abort();
136137

138+
/**
139+
* @brief Get locale (currently only used for date/time formatting)
140+
*/
141+
const wolv::util::Locale& getLocale() const;
142+
143+
/**
144+
* @brief Set locale (currently only used for date/time formatting)
145+
*/
146+
void setLocale(const wolv::util::Locale &lc);
147+
137148
/**
138149
* @brief Sets the data source for the pattern language
139150
* @param baseAddress Base address of the data source
@@ -416,6 +427,7 @@ namespace pl {
416427

417428
private:
418429
Internals m_internals;
430+
wolv::util::Locale m_locale;
419431
std::vector<core::err::CompileError> m_compileErrors;
420432
std::optional<core::err::PatternLanguageError> m_currError;
421433
std::map<std::string, std::string> m_defines;
Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
#pragma once
2-
3-
#include <pl/patterns/pattern.hpp>
4-
5-
namespace pl::ptrn {
6-
7-
class PatternUnsigned : public Pattern {
8-
public:
9-
PatternUnsigned(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
10-
: Pattern(evaluator, offset, size, line) { }
11-
12-
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
13-
return std::unique_ptr<Pattern>(new PatternUnsigned(*this));
14-
}
15-
16-
[[nodiscard]] core::Token::Literal getValue() const override {
17-
u128 data = 0;
18-
this->getEvaluator()->readData(this->getOffset(), &data, this->getSize(), this->getSection());
19-
return transformValue(hlp::changeEndianess(data, this->getSize(), this->getEndian()));
20-
}
21-
22-
[[nodiscard]] std::string getFormattedName() const override {
23-
return this->getTypeName();
24-
}
25-
26-
[[nodiscard]] bool operator==(const Pattern &other) const override { return compareCommonProperties<decltype(*this)>(other); }
27-
28-
void accept(PatternVisitor &v) override {
29-
v.visit(*this);
30-
}
31-
32-
std::string formatDisplayValue() override {
33-
auto data = this->getValue().toUnsigned();
34-
return Pattern::callUserFormatFunc(this->getValue()).value_or(fmt::format("{:d}", data));
35-
}
36-
37-
[[nodiscard]] std::string toString() override {
38-
auto value = this->getValue();
39-
auto result = fmt::format("{:d}", value.toUnsigned());
40-
41-
return Pattern::callUserFormatFunc(value, true).value_or(result);
42-
}
43-
44-
std::vector<u8> getRawBytes() override {
45-
std::vector<u8> result;
46-
result.resize(this->getSize());
47-
48-
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
49-
if (this->getEndian() != std::endian::native)
50-
std::reverse(result.begin(), result.end());
51-
52-
return result;
53-
}
54-
};
55-
1+
#pragma once
2+
3+
#include <pl/patterns/pattern.hpp>
4+
5+
namespace pl::ptrn {
6+
7+
class PatternUnsigned : public Pattern {
8+
public:
9+
PatternUnsigned(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
10+
: Pattern(evaluator, offset, size, line) { }
11+
12+
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
13+
return std::unique_ptr<Pattern>(new PatternUnsigned(*this));
14+
}
15+
16+
[[nodiscard]] core::Token::Literal getValue() const override {
17+
u128 data = 0;
18+
this->getEvaluator()->readData(this->getOffset(), &data, this->getSize(), this->getSection());
19+
return transformValue(hlp::changeEndianess(data, this->getSize(), this->getEndian()));
20+
}
21+
22+
[[nodiscard]] std::string getFormattedName() const override {
23+
return this->getTypeName();
24+
}
25+
26+
[[nodiscard]] bool operator==(const Pattern &other) const override { return compareCommonProperties<decltype(*this)>(other); }
27+
28+
void accept(PatternVisitor &v) override {
29+
v.visit(*this);
30+
}
31+
32+
std::string formatDisplayValue() override {
33+
auto data = this->getValue().toUnsigned();
34+
return Pattern::callUserFormatFunc(this->getValue()).value_or(fmt::format("{:d}", data));
35+
}
36+
37+
[[nodiscard]] std::string toString() override {
38+
auto value = this->getValue();
39+
auto result = fmt::format("{:d}", value.toUnsigned());
40+
41+
return Pattern::callUserFormatFunc(value, true).value_or(result);
42+
}
43+
44+
std::vector<u8> getRawBytes() override {
45+
std::vector<u8> result;
46+
result.resize(this->getSize());
47+
48+
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
49+
if (this->getEndian() != std::endian::native)
50+
std::reverse(result.begin(), result.end());
51+
52+
return result;
53+
}
54+
};
55+
5656
}

lib/source/pl/lib/std/time.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <pl/core/log_console.hpp>
55
#include <pl/core/evaluator.hpp>
66
#include <pl/patterns/pattern.hpp>
7+
#include <wolv/utils/date_time_format.hpp>
78

89
#include <ctime>
910
#include <fmt/format.h>
@@ -65,29 +66,21 @@ namespace pl::lib::libstd::time {
6566
/* to_local(time) */
6667
runtime.addFunction(nsStdTime, "to_local", FunctionParameterCount::exactly(1), [&runtime](Evaluator *, auto params) -> std::optional<Token::Literal> {
6768
auto time = time_t(params[0].toUnsigned());
69+
auto localTime = std::localtime(&time);
6870

69-
try {
70-
auto localTime = std::localtime(&time);
71-
if (localTime == nullptr) return u128(0);
71+
if (localTime == nullptr) return u128(0);
7272

73-
return { packTMValue(*localTime, runtime) };
74-
} catch (const fmt::format_error&) {
75-
return u128(0);
76-
}
73+
return { packTMValue(*localTime, runtime) };
7774
});
7875

7976
/* to_utc(time) */
8077
runtime.addFunction(nsStdTime, "to_utc", FunctionParameterCount::exactly(1), [&runtime](Evaluator *, auto params) -> std::optional<Token::Literal> {
8178
auto time = time_t(params[0].toUnsigned());
79+
auto gmTime = std::gmtime(&time);
8280

83-
try {
84-
auto gmTime = std::gmtime(&time);
85-
if (gmTime == nullptr) return u128(0);
81+
if (gmTime == nullptr) return u128(0);
8682

87-
return { packTMValue(*gmTime, runtime) };
88-
} catch (const fmt::format_error&) {
89-
return u128(0);
90-
}
83+
return { packTMValue(*gmTime, runtime) };
9184
});
9285

9386
/* to_epoch(structured_time) */
@@ -117,6 +110,17 @@ namespace pl::lib::libstd::time {
117110

118111
return { fmt::format(fmt::runtime(fmt::format("{{:{}}}", formatString)), time) };
119112
});
113+
114+
/* format_tt_locale(time_t) */
115+
runtime.addFunction(nsStdTime, "format_tt_locale", FunctionParameterCount::exactly(1), [&runtime](Evaluator *, auto params) -> std::optional<Token::Literal> {
116+
auto tt = params[0].toUnsigned();
117+
const wolv::util::Locale &lc = runtime.getLocale();
118+
119+
using wolv::util::DTOpts;
120+
auto optval = wolv::util::formatTT(lc, tt, DTOpts::TT32 | DTOpts::DandT | DTOpts::LongDate);
121+
122+
return optval;
123+
});
120124
}
121125
}
122126

lib/source/pl/pattern_language.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ namespace pl {
380380
this->m_aborted = true;
381381
}
382382

383+
const wolv::util::Locale& PatternLanguage::getLocale() const {
384+
return m_locale;
385+
}
386+
387+
void PatternLanguage::setLocale(const wolv::util::Locale &lc) {
388+
m_locale = lc;
389+
}
390+
383391
void PatternLanguage::setIncludePaths(const std::vector<std::fs::path>& paths) {
384392
this->m_fileResolver.setIncludePaths(paths);
385393
}

0 commit comments

Comments
 (0)