aboutsummaryrefslogtreecommitdiff
path: root/loslib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-04-18 10:06:55 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-04-18 10:06:55 -0300
commit48baa5e89cfd7e7fbfe03966b50bfcf4f57cc6cd (patch)
tree7f13434d9b819672ed5d785f6024b4a3a7fc9ae9 /loslib.c
parentfdd7209688d9d96cbfc003db4f78701321acd3bf (diff)
downloadlua-48baa5e89cfd7e7fbfe03966b50bfcf4f57cc6cd.tar.gz
lua-48baa5e89cfd7e7fbfe03966b50bfcf4f57cc6cd.tar.bz2
lua-48baa5e89cfd7e7fbfe03966b50bfcf4f57cc6cd.zip
'os.time(t)' normalizes 't' fields
Diffstat (limited to 'loslib.c')
-rw-r--r--loslib.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/loslib.c b/loslib.c
index 1f893cc0..4563d2e0 100644
--- a/loslib.c
+++ b/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.62 2016/02/08 14:42:46 roberto Exp roberto $ 2** $Id: loslib.c,v 1.63 2016/02/09 12:16:11 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -206,6 +206,23 @@ static void setboolfield (lua_State *L, const char *key, int value) {
206 lua_setfield(L, -2, key); 206 lua_setfield(L, -2, key);
207} 207}
208 208
209
210/*
211** Set all fields from structure 'tm' in the table on top of the stack
212*/
213static void setallfields (lua_State *L, struct tm *stm) {
214 setfield(L, "sec", stm->tm_sec);
215 setfield(L, "min", stm->tm_min);
216 setfield(L, "hour", stm->tm_hour);
217 setfield(L, "day", stm->tm_mday);
218 setfield(L, "month", stm->tm_mon + 1);
219 setfield(L, "year", stm->tm_year + 1900);
220 setfield(L, "wday", stm->tm_wday + 1);
221 setfield(L, "yday", stm->tm_yday + 1);
222 setboolfield(L, "isdst", stm->tm_isdst);
223}
224
225
209static int getboolfield (lua_State *L, const char *key) { 226static int getboolfield (lua_State *L, const char *key) {
210 int res; 227 int res;
211 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); 228 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
@@ -276,15 +293,7 @@ static int os_date (lua_State *L) {
276 luaL_error(L, "time result cannot be represented in this installation"); 293 luaL_error(L, "time result cannot be represented in this installation");
277 if (strcmp(s, "*t") == 0) { 294 if (strcmp(s, "*t") == 0) {
278 lua_createtable(L, 0, 9); /* 9 = number of fields */ 295 lua_createtable(L, 0, 9); /* 9 = number of fields */
279 setfield(L, "sec", stm->tm_sec); 296 setallfields(L, stm);
280 setfield(L, "min", stm->tm_min);
281 setfield(L, "hour", stm->tm_hour);
282 setfield(L, "day", stm->tm_mday);
283 setfield(L, "month", stm->tm_mon+1);
284 setfield(L, "year", stm->tm_year+1900);
285 setfield(L, "wday", stm->tm_wday+1);
286 setfield(L, "yday", stm->tm_yday+1);
287 setboolfield(L, "isdst", stm->tm_isdst);
288 } 297 }
289 else { 298 else {
290 char cc[4]; /* buffer for individual conversion specifiers */ 299 char cc[4]; /* buffer for individual conversion specifiers */
@@ -324,6 +333,7 @@ static int os_time (lua_State *L) {
324 ts.tm_year = getfield(L, "year", -1, 1900); 333 ts.tm_year = getfield(L, "year", -1, 1900);
325 ts.tm_isdst = getboolfield(L, "isdst"); 334 ts.tm_isdst = getboolfield(L, "isdst");
326 t = mktime(&ts); 335 t = mktime(&ts);
336 setallfields(L, &ts); /* update fields with normalized values */
327 } 337 }
328 if (t != (time_t)(l_timet)t || t == (time_t)(-1)) 338 if (t != (time_t)(l_timet)t || t == (time_t)(-1))
329 luaL_error(L, "time result cannot be represented in this installation"); 339 luaL_error(L, "time result cannot be represented in this installation");