Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/VM/src/loslib.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
3
#include "lualib.h"
4
5
#include "lcommon.h"
6
7
#include <string.h>
8
#include <time.h>
9
10
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%"
11
12
#if defined(_WIN32)
13
static tm* gmtime_r(const time_t* timep, tm* result)
14
{
15
return gmtime_s(result, timep) == 0 ? result : NULL;
16
}
17
18
static tm* localtime_r(const time_t* timep, tm* result)
19
{
20
return localtime_s(result, timep) == 0 ? result : NULL;
21
}
22
#endif
23
24
static time_t os_timegm(struct tm* timep)
25
{
26
// Julian day number calculation
27
int day = timep->tm_mday;
28
int month = timep->tm_mon + 1;
29
int year = timep->tm_year + 1900;
30
31
// year adjustment, pretend that it starts in March
32
int a = timep->tm_mon % 12 < 2 ? 1 : 0;
33
34
// also adjust for out-of-range month numbers in input
35
a -= timep->tm_mon / 12;
36
37
int y = year + 4800 - a;
38
int m = month + (12 * a) - 3;
39
40
int julianday = day + ((153 * m + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
41
42
const int utcstartasjulianday = 2440588; // Jan 1st 1970 offset in Julian calendar
43
const int64_t utcstartasjuliansecond = utcstartasjulianday * 86400ll; // same in seconds
44
45
// fail the dates before UTC start
46
if (julianday < utcstartasjulianday)
47
return time_t(-1);
48
49
int64_t daysecond = timep->tm_hour * 3600ll + timep->tm_min * 60ll + timep->tm_sec;
50
int64_t julianseconds = int64_t(julianday) * 86400ull + daysecond;
51
52
if (julianseconds < utcstartasjuliansecond)
53
return time_t(-1);
54
55
int64_t utc = julianseconds - utcstartasjuliansecond;
56
return time_t(utc);
57
}
58
59
static int os_clock(lua_State* L)
60
{
61
lua_pushnumber(L, lua_clock());
62
return 1;
63
}
64
65
/*
66
** {======================================================
67
** Time/Date operations
68
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
69
** wday=%w+1, yday=%j, isdst=? }
70
** =======================================================
71
*/
72
73
static void setfield(lua_State* L, const char* key, int value)
74
{
75
lua_pushinteger(L, value);
76
lua_setfield(L, -2, key);
77
}
78
79
static void setboolfield(lua_State* L, const char* key, int value)
80
{
81
if (value < 0) // undefined?
82
return; // does not set field
83
lua_pushboolean(L, value);
84
lua_setfield(L, -2, key);
85
}
86
87
static int getboolfield(lua_State* L, const char* key)
88
{
89
int res;
90
lua_rawgetfield(L, -1, key);
91
res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
92
lua_pop(L, 1);
93
return res;
94
}
95
96
static int getfield(lua_State* L, const char* key, int d)
97
{
98
int res;
99
lua_rawgetfield(L, -1, key);
100
if (lua_isnumber(L, -1))
101
res = (int)lua_tointeger(L, -1);
102
else
103
{
104
if (d < 0)
105
luaL_error(L, "field '%s' missing in date table", key);
106
res = d;
107
}
108
lua_pop(L, 1);
109
return res;
110
}
111
112
static int os_date(lua_State* L)
113
{
114
const char* s = luaL_optstring(L, 1, "%c");
115
time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
116
117
struct tm tm;
118
struct tm* stm;
119
if (*s == '!')
120
{ // UTC?
121
stm = gmtime_r(&t, &tm);
122
s++; // skip `!'
123
}
124
else
125
{
126
// on Windows, localtime() fails with dates before epoch start so we disallow that
127
stm = t < 0 ? NULL : localtime_r(&t, &tm);
128
}
129
130
if (stm == NULL) // invalid date?
131
{
132
lua_pushnil(L);
133
}
134
else if (strcmp(s, "*t") == 0)
135
{
136
lua_createtable(L, 0, 9); // 9 = number of fields
137
setfield(L, "sec", stm->tm_sec);
138
setfield(L, "min", stm->tm_min);
139
setfield(L, "hour", stm->tm_hour);
140
setfield(L, "day", stm->tm_mday);
141
setfield(L, "month", stm->tm_mon + 1);
142
setfield(L, "year", stm->tm_year + 1900);
143
setfield(L, "wday", stm->tm_wday + 1);
144
setfield(L, "yday", stm->tm_yday + 1);
145
setboolfield(L, "isdst", stm->tm_isdst);
146
}
147
else
148
{
149
char cc[3];
150
cc[0] = '%';
151
cc[2] = '\0';
152
153
luaL_Strbuf b;
154
luaL_buffinit(L, &b);
155
for (; *s; s++)
156
{
157
if (*s != '%' || *(s + 1) == '\0') // no conversion specifier?
158
{
159
luaL_addchar(&b, *s);
160
}
161
else if (strchr(LUA_STRFTIMEOPTIONS, *(s + 1)) == 0)
162
{
163
luaL_argerror(L, 1, "invalid conversion specifier");
164
}
165
else
166
{
167
size_t reslen;
168
char buff[200]; // should be big enough for any conversion result
169
cc[1] = *(++s);
170
reslen = strftime(buff, sizeof(buff), cc, stm);
171
luaL_addlstring(&b, buff, reslen);
172
}
173
}
174
luaL_pushresult(&b);
175
}
176
return 1;
177
}
178
179
static int os_time(lua_State* L)
180
{
181
time_t t;
182
if (lua_isnoneornil(L, 1)) // called without args?
183
t = time(NULL); // get current time
184
else
185
{
186
struct tm ts;
187
luaL_checktype(L, 1, LUA_TTABLE);
188
lua_settop(L, 1); // make sure table is at the top
189
ts.tm_sec = getfield(L, "sec", 0);
190
ts.tm_min = getfield(L, "min", 0);
191
ts.tm_hour = getfield(L, "hour", 12);
192
ts.tm_mday = getfield(L, "day", -1);
193
ts.tm_mon = getfield(L, "month", -1) - 1;
194
ts.tm_year = getfield(L, "year", -1) - 1900;
195
ts.tm_isdst = getboolfield(L, "isdst");
196
197
// Note: upstream Lua uses mktime() here which assumes input is local time, but we prefer UTC for consistency
198
t = os_timegm(&ts);
199
}
200
if (t == (time_t)(-1))
201
lua_pushnil(L);
202
else
203
lua_pushnumber(L, (double)t);
204
return 1;
205
}
206
207
static int os_difftime(lua_State* L)
208
{
209
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), (time_t)(luaL_optnumber(L, 2, 0))));
210
return 1;
211
}
212
213
static const luaL_Reg syslib[] = {
214
{"clock", os_clock},
215
{"date", os_date},
216
{"difftime", os_difftime},
217
{"time", os_time},
218
{NULL, NULL},
219
};
220
221
int luaopen_os(lua_State* L)
222
{
223
luaL_register(L, LUA_OSLIBNAME, syslib);
224
return 1;
225
}
226
227