lua-users home
lua-l archive

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


Hi all,

in a project I'm working on I was testing some time related
functions with a loop similar to the following:

    -- This loop does not work in Lua 5.3.3
    local datetime = { year=2016, month=6, day=20, isdst=false }
    for n = 1, #tests do
        local test = tests[n]
        datetime.hour = test.hour
        datetime.min  = test.min
        local timestamp = os.time(datetime)
        ...
    end

After some research, I was surprised to discover that os.time()
modifies the datetime table passed in. Here is the proof:

$ lua
Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> a = { year=2016, month=6, day=20, isdst=false }
> for k,v in pairs(a) do print(k,v) end
isdst	false
month	6
day	20
year	2016
> os.time(a)
1466420400
> for k,v in pairs(a) do print(k,v) end
sec	0
year	2016
isdst	true
wday	2
yday	172
month	6
day	20
min	0
hour	13

Just to be sure I checked against Lua 5.2.4. That worked as expected:

$ lua52
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> a = { year=2016, month=6, day=20, isdst=false }
> for k,v in pairs(a) do print(k,v) end
day	20
year	2016
isdst	false
month	6
> os.time(a)
> for k,v in pairs(a) do print(k,v) end
day	20
year	2016
isdst	false
month	6

Is this expected behavior? I did not find anything mentioning it
in PiL or in the wiki.

Ciao.
-- 
Nicola