Skip to content

Commit 4bb662f

Browse files
committed
Added a UTC timestamp.
1 parent 4dda7da commit 4bb662f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/lpm.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,36 @@ static int lpm_setenv(lua_State* L) {
20472047
return 0;
20482048
}
20492049

2050+
static int lpm_utctime(lua_State* L) {
2051+
time_t now = time(NULL);
2052+
struct tm* t = NULL;
2053+
t = gmtime(&now);
2054+
lua_pushnil(L);
2055+
while (lua_next(L, 1) != 0) {
2056+
const char* key = luaL_checkstring(L, -2);
2057+
int value = luaL_checkinteger(L, -1);
2058+
if (strcmp(key, "year") == 0)
2059+
t->tm_year = value - 1900;
2060+
else if (strcmp(key, "month") == 0)
2061+
t->tm_mon = value - 1;
2062+
else if (strcmp(key, "day") == 0)
2063+
t->tm_mday = value;
2064+
else if (strcmp(key, "hour") == 0)
2065+
t->tm_hour = value;
2066+
else if (strcmp(key, "min") == 0)
2067+
t->tm_min = value;
2068+
else if (strcmp(key, "sec") == 0)
2069+
t->tm_sec = value;
2070+
lua_pop(L, 1);
2071+
}
2072+
#if _WIN32
2073+
lua_pushinteger(L, _mkgmtime(t));
2074+
#else
2075+
lua_pushinteger(L, timegm(t));
2076+
#endif
2077+
return 1;
2078+
}
2079+
20502080

20512081
static const luaL_Reg system_lib[] = {
20522082
{ "ls", lpm_ls }, // Returns an array of files.
@@ -2070,6 +2100,7 @@ static const luaL_Reg system_lib[] = {
20702100
{ "flock", lpm_flock }, // Locks a file.
20712101
{ "time", lpm_time }, // Get high-precision system time.
20722102
{ "setenv", lpm_setenv }, // Sets a system environment variable.
2103+
{ "utctime", lpm_utctime }, // Converts a local timestamp to a UTC timestamp.
20732104
{ NULL, NULL }
20742105
};
20752106

src/lpm.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ end
19091909

19101910
local function parseJSDate(date)
19111911
-- Mon, 10 Nov 2025 20:01:24 GMT
1912-
local dow, day, month, year, hour, minute, second, time_zone = date:match("(%w+),%s+(%d+)%s+(%w+)%s+(%d+)%s+(%d+):(%d+):(%d+)%s+(%w+)")
1912+
local dow, day, month, year, hour, minute, second, time_zone = date:match("(%w+),%s+(%d+)%s+(%w+)%s+(%d+)%s+(%d+):(%d+):(%d+)%s+GMT$")
19131913
if not dow then return nil end
19141914
local months = {
19151915
["Jan"] = 1,
@@ -1925,7 +1925,7 @@ local function parseJSDate(date)
19251925
["Nov"] = 11,
19261926
["Dec"] = 12
19271927
}
1928-
return os.time({ year = year, month = months[month], day = day, hour = hour, minute = minute, second = second })
1928+
return system.utctime({ year = year, month = months[month], day = day, hour = hour, min = minute, sec = second })
19291929
end
19301930

19311931
local function get_lite_xls()

0 commit comments

Comments
 (0)