configuration for Lua type corresponding to 'time_t'

This commit is contained in:
Roberto Ierusalimschy 2014-03-20 16:18:54 -03:00
parent 7707f3e7c3
commit 469daa16ee
1 changed files with 24 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: loslib.c,v 1.43 2014/02/26 15:55:58 roberto Exp roberto $
** $Id: loslib.c,v 1.44 2014/03/12 20:57:40 roberto Exp roberto $
** Standard Operating System library
** See Copyright Notice in lua.h
*/
@ -20,10 +20,10 @@
#include "lualib.h"
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
/*
** list of valid conversion specifiers for the 'strftime' function
*/
#if !defined(LUA_STRFTIMEOPTIONS)
#if !defined(LUA_USE_POSIX)
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
@ -34,15 +34,27 @@
"O", "deHImMSuUVwWy" }
#endif
#endif
#endif /* } */
#if !defined(l_time_t) /* { */
/*
** type to represent time_t in Lua
*/
#define l_timet lua_Integer
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
#define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a))
#endif /* } */
#if !defined(lua_tmpnam) /* { */
/*
** By default, Lua uses tmpnam except when POSIX is available, where it
** uses mkstemp.
*/
#if !defined(lua_tmpnam) /* { */
#if defined(LUA_USE_POSIX) /* { */
@ -65,11 +77,12 @@
#endif /* } */
#if !defined(l_gmtime) /* { */
/*
** By default, Lua uses gmtime/localtime, except when POSIX is available,
** where it uses gmtime_r/localtime_r
*/
#if !defined(l_gmtime) /* { */
#if defined(LUA_USE_POSIX) /* { */
@ -204,7 +217,7 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) {
static int os_date (lua_State *L) {
const char *s = luaL_optstring(L, 1, "%c");
time_t t = luaL_opt(L, (time_t)luaL_checkinteger, 2, time(NULL));
time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
struct tm tmr, *stm;
if (*s == '!') { /* UTC? */
stm = l_gmtime(&t, &tmr);
@ -265,17 +278,18 @@ static int os_time (lua_State *L) {
ts.tm_isdst = getboolfield(L, "isdst");
t = mktime(&ts);
}
if (t == (time_t)(-1))
if (t != (time_t)(l_timet)t)
luaL_error(L, "time result cannot be represented in this Lua instalation");
else if (t == (time_t)(-1))
lua_pushnil(L);
else
lua_pushinteger(L, t);
l_pushtime(L, t);
return 1;
}
static int os_difftime (lua_State *L) {
lua_pushnumber(L, difftime((time_t)(luaL_checkinteger(L, 1)),
(time_t)(luaL_optinteger(L, 2, 0))));
lua_pushnumber(L, difftime((l_checktime(L, 1)), (l_checktime(L, 2))));
return 1;
}