function Initialize()
measure = SKIN:GetMeasure('MeasureLocalTime')
end
function Update()
-- setup timestamps
local timestamp = measure:GetValue()
local localTz = (getTimeOffset() / 3600)
local tzOffset = -6 -- insert the input's timezone offset here. in the case of a weather skin, this would be the
-- timezone offset of the location you are getting the data from
if tzOffset == localTz then
tDate = os.date("!*t", timestamp)
else
tDate = os.date("!*t", timestamp - getTimeOffset() + (tzOffset * 3600))
end
tDate.year = tDate.year - (1970 - 1601) -- convert Windows timestamp (0 = 1/1/1601) to Unix/Lua timestamp (0 = 1/1/1970)
timestamp = os.time(tDate) -- recreate timestamp with new parameters
return timestamp
end
function getTimeOffset() return (os.time() - os.time(os.date('!*t')) + (os.date('*t')['isdst'] and 3600 or 0)) end
This code is supposed to account for timezones and accurately translate the timestamp. However, no matter what I do, the resulting timestamp is ALWAYS one hour ahead. I am using this website to test the timestamps.
I feel like the solution is super easy and obvious, but I just can't wrap my head around it. Can someone help me?
I also made a simple test skin to ease the testing process (left-click the timestamp to copy it to the clipboard so you can paste it on the website):
Yeah, I can't believe I didn't know that was there.
I just tried to implement it and it caused all sorts of issues. I need to offset the timestamp by the timezone of the location, and I need to be able to manipulate it using the table that os.date() creates, so I can't just add or subtract it by a hardcoded number. I need to figure out why my existing formulas aren't doing the job.
I expanded the test to include all of the results that I need:
function Initialize()
measure = SKIN:GetMeasure('MeasureLocalTime')
timestamps = {}
end
function Update()
-- setup timestamps
local timestamp = measure:GetValue()
local localTz = (getTimeOffset() / 3600)
local tzOffset = -6 -- insert the input's timezone offset here. in the case of a weather skin, this would be the
-- timezone offset at the location you are getting the data from
if tzOffset == localTz then
tDate = os.date("!*t", timestamp)
else
tDate = os.date("!*t", timestamp - getTimeOffset() + (tzOffset * 3600))
end
tDate.year = tDate.year - (1970 - 1601) -- convert Windows timestamp (0 = 1/1/1601) to Unix/Lua timestamp (0 = 1/1/1970)
timestamp = os.time(tDate) -- recreate timestamp with new parameters
mDate = tonumber(tostring(timestamp) .. '000') -- millisecond date (timestamp with three extra zeroes)
zDate = tonumber(tostring(os.time{ year = tDate.year, month = tDate.month, day = tDate.day, hour = 0, min = 0, sec = 0 }) .. '000') -- timestamp at current day, 0:00:00 (12:00 AM)
ysDate = zDate - 86400000 -- timestamp at yesterday, 0:00:00 (12:00 AM)
tmDate = zDate + 86400000 -- timestamp at tomorrow, 0:00:00 (12:00 AM)
timestamps.timestamp = timestamp
timestamps.mDate = mDate
timestamps.zDate = zDate
timestamps.ysDate = ysDate
timestamps.tmDate = tmDate
end
function GetData(key) return timestamps[key] or '' end
function getTimeOffset() return (os.time() - os.time(os.date('!*t')) + (os.date('*t')['isdst'] and 3600 or 0)) end