diff options
| -rw-r--r-- | loslib.c | 30 |
1 files changed, 20 insertions, 10 deletions
| @@ -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 | */ | ||
| 213 | static 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 | |||
| 209 | static int getboolfield (lua_State *L, const char *key) { | 226 | static 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"); |
