diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-03-20 16:18:54 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-03-20 16:18:54 -0300 |
commit | 469daa16eeb2b894781ab6d6364a6d1d31db558e (patch) | |
tree | 4348a1d4d5feec3d15015594e8f036a5b0eced11 /loslib.c | |
parent | 7707f3e7c3eea95551255f7dafa6ff918d3cf23a (diff) | |
download | lua-469daa16eeb2b894781ab6d6364a6d1d31db558e.tar.gz lua-469daa16eeb2b894781ab6d6364a6d1d31db558e.tar.bz2 lua-469daa16eeb2b894781ab6d6364a6d1d31db558e.zip |
configuration for Lua type corresponding to 'time_t'
Diffstat (limited to 'loslib.c')
-rw-r--r-- | loslib.c | 34 |
1 files changed, 24 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loslib.c,v 1.43 2014/02/26 15:55:58 roberto Exp roberto $ | 2 | ** $Id: loslib.c,v 1.44 2014/03/12 20:57:40 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 | */ |
@@ -20,10 +20,10 @@ | |||
20 | #include "lualib.h" | 20 | #include "lualib.h" |
21 | 21 | ||
22 | 22 | ||
23 | #if !defined(LUA_STRFTIMEOPTIONS) /* { */ | ||
23 | /* | 24 | /* |
24 | ** list of valid conversion specifiers for the 'strftime' function | 25 | ** list of valid conversion specifiers for the 'strftime' function |
25 | */ | 26 | */ |
26 | #if !defined(LUA_STRFTIMEOPTIONS) | ||
27 | 27 | ||
28 | #if !defined(LUA_USE_POSIX) | 28 | #if !defined(LUA_USE_POSIX) |
29 | #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } | 29 | #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } |
@@ -34,15 +34,27 @@ | |||
34 | "O", "deHImMSuUVwWy" } | 34 | "O", "deHImMSuUVwWy" } |
35 | #endif | 35 | #endif |
36 | 36 | ||
37 | #endif | 37 | #endif /* } */ |
38 | 38 | ||
39 | 39 | ||
40 | 40 | ||
41 | #if !defined(l_time_t) /* { */ | ||
42 | /* | ||
43 | ** type to represent time_t in Lua | ||
44 | */ | ||
45 | #define l_timet lua_Integer | ||
46 | #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) | ||
47 | #define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a)) | ||
48 | |||
49 | #endif /* } */ | ||
50 | |||
51 | |||
52 | |||
53 | #if !defined(lua_tmpnam) /* { */ | ||
41 | /* | 54 | /* |
42 | ** By default, Lua uses tmpnam except when POSIX is available, where it | 55 | ** By default, Lua uses tmpnam except when POSIX is available, where it |
43 | ** uses mkstemp. | 56 | ** uses mkstemp. |
44 | */ | 57 | */ |
45 | #if !defined(lua_tmpnam) /* { */ | ||
46 | 58 | ||
47 | #if defined(LUA_USE_POSIX) /* { */ | 59 | #if defined(LUA_USE_POSIX) /* { */ |
48 | 60 | ||
@@ -65,11 +77,12 @@ | |||
65 | #endif /* } */ | 77 | #endif /* } */ |
66 | 78 | ||
67 | 79 | ||
80 | |||
81 | #if !defined(l_gmtime) /* { */ | ||
68 | /* | 82 | /* |
69 | ** By default, Lua uses gmtime/localtime, except when POSIX is available, | 83 | ** By default, Lua uses gmtime/localtime, except when POSIX is available, |
70 | ** where it uses gmtime_r/localtime_r | 84 | ** where it uses gmtime_r/localtime_r |
71 | */ | 85 | */ |
72 | #if !defined(l_gmtime) /* { */ | ||
73 | 86 | ||
74 | #if defined(LUA_USE_POSIX) /* { */ | 87 | #if defined(LUA_USE_POSIX) /* { */ |
75 | 88 | ||
@@ -204,7 +217,7 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { | |||
204 | 217 | ||
205 | static int os_date (lua_State *L) { | 218 | static int os_date (lua_State *L) { |
206 | const char *s = luaL_optstring(L, 1, "%c"); | 219 | const char *s = luaL_optstring(L, 1, "%c"); |
207 | time_t t = luaL_opt(L, (time_t)luaL_checkinteger, 2, time(NULL)); | 220 | time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); |
208 | struct tm tmr, *stm; | 221 | struct tm tmr, *stm; |
209 | if (*s == '!') { /* UTC? */ | 222 | if (*s == '!') { /* UTC? */ |
210 | stm = l_gmtime(&t, &tmr); | 223 | stm = l_gmtime(&t, &tmr); |
@@ -265,17 +278,18 @@ static int os_time (lua_State *L) { | |||
265 | ts.tm_isdst = getboolfield(L, "isdst"); | 278 | ts.tm_isdst = getboolfield(L, "isdst"); |
266 | t = mktime(&ts); | 279 | t = mktime(&ts); |
267 | } | 280 | } |
268 | if (t == (time_t)(-1)) | 281 | if (t != (time_t)(l_timet)t) |
282 | luaL_error(L, "time result cannot be represented in this Lua instalation"); | ||
283 | else if (t == (time_t)(-1)) | ||
269 | lua_pushnil(L); | 284 | lua_pushnil(L); |
270 | else | 285 | else |
271 | lua_pushinteger(L, t); | 286 | l_pushtime(L, t); |
272 | return 1; | 287 | return 1; |
273 | } | 288 | } |
274 | 289 | ||
275 | 290 | ||
276 | static int os_difftime (lua_State *L) { | 291 | static int os_difftime (lua_State *L) { |
277 | lua_pushnumber(L, difftime((time_t)(luaL_checkinteger(L, 1)), | 292 | lua_pushnumber(L, difftime((l_checktime(L, 1)), (l_checktime(L, 2)))); |
278 | (time_t)(luaL_optinteger(L, 2, 0)))); | ||
279 | return 1; | 293 | return 1; |
280 | } | 294 | } |
281 | 295 | ||