add precise_utc_time function

This commit is contained in:
neveix 2026-08-18 21:57:51 +05:00
parent 4111da1230
commit dfcc3e913d
3 changed files with 25 additions and 10 deletions

View file

@ -1,11 +1,12 @@
#include "engine/Engine.hpp"
#include "api_lua.hpp"
#include <ctime>
#include "api_lua.hpp"
#include "engine/Engine.hpp"
using namespace scripting;
#if defined(_WIN32) || defined(_WIN64)
#define USE_MSVC_TIME_SAFE
#define USE_MSVC_TIME_SAFE
#endif
static int l_uptime(lua::State* L) {
@ -20,11 +21,18 @@ static int l_utc_time(lua::State* L) {
return lua::pushnumber(L, std::time(nullptr));
}
static int l_precise_utc_time(lua::State* L) {
auto now = std::chrono::system_clock::now();
return lua::pushnumber(
L, std::chrono::duration<double>(now.time_since_epoch()).count()
);
}
static int l_local_time(lua::State* L) {
std::time_t t = std::time(nullptr);
std::tm gmt_tm{};
std::tm local_tm{};
std::tm gmt_tm {};
std::tm local_tm {};
#if defined(USE_MSVC_TIME_SAFE)
gmtime_s(&gmt_tm, &t);
@ -44,8 +52,8 @@ static int l_local_time(lua::State* L) {
static int l_utc_offset(lua::State* L) {
std::time_t t = std::time(nullptr);
std::tm gmt_tm{};
std::tm local_tm{};
std::tm gmt_tm {};
std::tm local_tm {};
#if defined(USE_MSVC_TIME_SAFE)
gmtime_s(&gmt_tm, &t);
@ -74,6 +82,7 @@ const luaL_Reg timelib[] = {
{"uptime", lua::wrap<l_uptime>},
{"delta", lua::wrap<l_delta>},
{"utc_time", lua::wrap<l_utc_time>},
{"precise_utc_time", lua::wrap<l_precise_utc_time>},
{"utc_offset", lua::wrap<l_utc_offset>},
{"local_time", lua::wrap<l_local_time>},
{"precise_time", lua::wrap<l_precise_time>},