Comparison

util/datetime.lua @ 9698:e616c37756b3 0.11

util.datetime: Make sure timezone difference is calculated correctly (fixes #1262) If the two os.date() calls happen at either side of a second ticking over there would be a one second error in the calculation.
author Kim Alvefur <zash@zash.se>
date Sun, 09 Dec 2018 23:01:47 +0100
parent 8555:4f0f5b49bb03
child 12629:4c1d3f817063
comparison
equal deleted inserted replaced
9674:6f97acc4583b 9698:e616c37756b3
36 local function parse(s) 36 local function parse(s)
37 if s then 37 if s then
38 local year, month, day, hour, min, sec, tzd; 38 local year, month, day, hour, min, sec, tzd;
39 year, month, day, hour, min, sec, tzd = s:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$"); 39 year, month, day, hour, min, sec, tzd = s:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$");
40 if year then 40 if year then
41 local time_offset = os_difftime(os_time(os_date("*t")), os_time(os_date("!*t"))); -- to deal with local timezone 41 local now = os_time();
42 local time_offset = os_difftime(os_time(os_date("*t", now)), os_time(os_date("!*t", now))); -- to deal with local timezone
42 local tzd_offset = 0; 43 local tzd_offset = 0;
43 if tzd ~= "" and tzd ~= "Z" then 44 if tzd ~= "" and tzd ~= "Z" then
44 local sign, h, m = tzd:match("([+%-])(%d%d):?(%d*)"); 45 local sign, h, m = tzd:match("([+%-])(%d%d):?(%d*)");
45 if not sign then return; end 46 if not sign then return; end
46 if #m ~= 2 then m = "0"; end 47 if #m ~= 2 then m = "0"; end