[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: slickest way to find out local time zone offset in pure lua?
- From: Geoff Leyland <geoff_leyland@...>
- Date: Wed, 5 Mar 2008 08:19:42 +1300
On 5/03/2008, at 7:49 AM, Norman Ramsey wrote:
I'm sure this question has come up many times, but the way I'm doing
it seems clunky, and I hope someone here has a solution that looks
using pure Lua, how can I compute the time difference between local
and UCT (sometimes known as universal time or Greenwich Mean Time)?
This isn't what you want, and it's also a bit clunky, but here's how
I handle date/times in UTC (Lua interprets them as local time, so you
have to offset them by the time zone, handling DST as you go). There
might be something in there helpful though?
-- Takes a time struct with a date and time in UTC and converts it into
-- seconds since Unix epoch (0:00 1 Jan 1970 UTC).
-- Trickier than you'd think because os.time assumes the struct is in
local t_secs = os.time(t) -- get seconds if t was in
t = os.date("*t", t_secs) -- find out if daylight
savings was applied.
local t_UTC = os.date("!*t", t_secs) -- find out what UTC t was
t_UTC.isdst = t.isdst -- apply DST to this time if
local UTC_secs = os.time(t_UTC) -- find out the converted
time in seconds.
-- The answer is our original answer plus the difference.
return t_secs + os.difftime(t_secs, UTC_secs)