I wish to find the timezone offset using Lua, however I'm facing what looks odd behaviour, so I must be missing something.
I'm using the code:
local t1 = os.time();
local t2 = os.time( os.date( "!*t" ) );
print( t1, t2, t1 - t2 );
local t1 = os.time( os.date( "*t" ) );
local t2 = os.time( os.date( "!*t" ) );
print( t1, t2, t1 - t2 );
local t1 = os.date( "%c" );
local t2 = os.date( "!%c" );
print( t1, t2 );
local t1 = os.time( os.date( "*t", 86400 ) );
local t2 = os.time( os.date( "!*t", 86400 ) );
print( t1, t2, t1 - t2 );
local t1 = os.date( "*t" );
local t2 = os.date( "!*t" );
print( t1.hour, t1.isdst, t2.hour, t2.isdst );
print( ((t1.hour - t2.hour) * 60 + (t1.min - t2.min)) * 60 );
Which produces the output:
1496733916 1496730316 3600
1496733916 1496730316 3600
06/06/17 09:25:16 06/06/17 07:25:16
86400 82800 3600
9 true 7 false
7200
Now I'm located in UTC+2 (currently due to summer time, in winter is UTC+1, CEST) so I expected to see an offset of 7200 seconds. However the first two attempts, which should be equivalent, and they are; give me a difference of only one hour. When printing the time in human readable format it can clearly be seen that the offset between both are 2 hours. The fourth attempt is using a fixed point in time (86400 number of seconds in a day, technique from this question) the offset is also 1 hour). Finally when directly subtracting directly the hours (and minutes just in case of not whole hours offset) I get the 2 hours offset.
I suspect this is due to the daylight saving time or dst. What I'm trying to accomplish is get the time from a timestamp (already in UTC), and since os.time
is in local time I need to convert that timestamp so it matches localtime.
Or am I completely missing something?
zone_diff
(in seconds) in that answer was indeed incorrect. It is fixed now. – Egor Skriptunoff