-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos.cpp
More file actions
190 lines (156 loc) · 4.07 KB
/
os.cpp
File metadata and controls
190 lines (156 loc) · 4.07 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "os.hpp"
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <format>
#include <time.h>
#include <windows.h>
#include "textmodule_lua.hpp"
#include "textmodule_string.hpp"
#include "textmodule_time.hpp"
int os_time(lua_State* L) {
try {
int tp = lua_type(L, 1);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TTABLE, 1, "table/none expected");
if (tp == LUA_TNONE) {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
__time64_t now_c = std::chrono::system_clock::to_time_t(now);
lua_pushnumber(L, (lua_Number)now_c);
return 1;
}
else if (tp == LUA_TTABLE) {
struct tm Time = tm{};
__time64_t res;
lua_totmstruct(L, 1, &Time);
res = _mktime64(&Time);
lua_pushnumber(L, (lua_Number)res);
return 1;
}
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int os_date(lua_State* L) {
try {
lua_Wstring format;
bool utc = false;
__time64_t time_t;
int tp = lua_type(L, 1);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, 1, "string/none expected");
if (tp == LUA_TNONE)
format = L"%c";
else if (tp== LUA_TSTRING)
format = lua_towstring(L, 1);
if (format[0] == L'!') { // is UTC?
utc = true;
format = format.substr(1);
}
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
tp = lua_type(L, 2);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TNUMBER || lua_isbignumber(L, 2), 2, "number/bignumber/none expected");
if (tp == LUA_TNUMBER)
time_t = static_cast<__time64_t>(lua_tonumber(L, 2));
else if (tp == LUA_TUSERDATA)
time_t = static_cast<__time64_t>(lua_tobignumber(L, 2));
else
time_t = std::chrono::system_clock::to_time_t(now);
if (format == L"*t") {
struct tm tm;
if (utc) //make tm struct
_gmtime64_s(&tm, &time_t);
else
_localtime64_s(&tm, &time_t);
lua_pushtmstruct(L, &tm);
return 1;
}
else {
std::wstring f = L"{:" + format + L"}";
std::chrono::sys_seconds now_sec = std::chrono::floor<std::chrono::seconds>(now); // •b’PˆÊ
std::chrono::zoned_seconds zone_time;
if (utc)
zone_time = { "UTC" , now_sec };
else
zone_time = { std::chrono::current_zone() , now_sec };
std::chrono::local_seconds local_time = zone_time.get_local_time();
std::wstring_view v = f;
f = std::vformat(f, std::make_wformat_args(local_time, 1));
lua_pushwstring(L, f);
return 1;
}
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int os_difftime(lua_State* L) {
try {
__time64_t time2_t = static_cast<__time64_t>(tm_tonumber(L, 1));
__time64_t time1_t = static_cast<__time64_t>(tm_tonumber(L, 2));
std::chrono::time_point time2 = std::chrono::system_clock::from_time_t(time2_t);
std::chrono::time_point time1 = std::chrono::system_clock::from_time_t(time1_t);
int sec = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(time2 - time1).count());
lua_pushnumber(L, sec);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int os_clock(lua_State* L) {
try {
clock_t clk = clock();
lua_pushnumber(L, (lua_Number)clk / (lua_Number)CLOCKS_PER_SEC);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int os_sleep(lua_State* L) {
try {
lua_Number time = tm_tonumber_s(L, 1, 0);
if (time <= 0)
return 0;
Sleep(static_cast<DWORD>(time * 1000.0));
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int os_exit(lua_State* L) {
try {
exit(luaL_optint(L, 1, EXIT_SUCCESS));
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
static luaL_Reg TEXTMODULE_OS_REG[] = {
{"time", os_time},
{"date", os_date},
{"difftime", os_difftime},
{"clock", os_clock},
{"sleep", os_sleep},
{"exit", os_exit},
{ nullptr, nullptr }
};
void luaReg_os(lua_State* L, lua_Option opt) {
if (opt["api"]["os"]) {
tm_debuglog_apiloaded(opt, "os");
lua_newtable(L);
luaL_register(L, NULL, TEXTMODULE_OS_REG);
lua_setfield(L, -2, "os");
}
else {
tm_debuglog_apinoloaded(opt, "os");
}
}