diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-11 00:31:53 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-11 00:31:53 +0500 |
| commit | 1bf68ba1680dbf40b1f44d91bd9a0abafa7cb3b6 (patch) | |
| tree | 0707cc00c529a63d21aa10f82bf556a430746105 | |
| parent | ead8d4e14f939eca0be580199de56a3d6039e446 (diff) | |
feat: a function for parsing datetimes to timestamps
| -rw-r--r-- | bot/src/commands/lua.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index 7b06082..d4d1781 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -101,6 +101,18 @@ namespace bot::command::lua { return o; }); + + state->set_function("time_parse", [](const std::string &datetime, + const std::string &format) { + std::tm tm = {}; + std::istringstream iss(datetime); + iss >> std::get_time(&tm, format.c_str()); + if (iss.fail()) { + throw std::runtime_error("datetime parse error"); + } + + return static_cast<long long>(std::mktime(&tm)); + }); } sol::object parse_json_object(std::shared_ptr<sol::state_view> state, |
