lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Lua experts,

take a look at the following Lua program which tries various timestamps around the unix epoch:

-- works on Linux and OsX
assert(os.time({year=1969, month=12, day=31, hour=23, min=59, sec=58}) == -2)

-- this fails, because in os_time() the call to mktime() will return -1,
-- which is treated as an error.
assert(os.time({year=1969, month=12, day=31, hour=23, min=59, sec=59}) == -1)

-- works on Linux and OsX
assert(os.time({year=1970, month=1, day=1, hour=0, min=0, sec=0}) == 0)

print "success"


and I'm running this program in the UTC time zone:

TZ=UTC lua date1969.lua


trying to get the integer timestamp value for 1969-12-31T23:59:59 fails, as the implementation in os_time() can not distinguish it from a different error of the mktime() function. Of course this is clearly an issue with the c library's function signature of "time_t mktime(struct tm*)" , but would it be possible to work around this in Lua?

As a work around I've added the following if condition to my os_time() function to detect this situation, but I'm fully aware that this work around does not consider any time zones. For me this works, because I know that my computers will always run in the UTC timezone.

t = mktime(&ts);
setallfields(L, &ts);  /* update fields with normalized values */
/* check if the last second in 1969 was requested, which is not an error. */
if (t == -1 &&
    ts.tm_year == 69 &&
    ts.tm_mday == 31 &&
    ts.tm_mon == 11 &&
    ts.tm_hour == 23 &&
    ts.tm_min == 59 &&
    ts.tm_sec == 59 )
{
      l_pushtime(L, t);
      return 1;
}


I'm not an expert with the C library's time functions, or many systems which simply wouldn't properly support negative values for time_t and thus can't support any timestamps before unix epoch anyway.


--
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org