lua-users home
lua-l archive

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


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 sweet: using pure Lua, how can I compute the time difference between local time
and UCT (sometimes known as universal time or Greenwich Mean Time)?


Norman

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?

Cheers,
Geoff


-- 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 time.
function time(t)
local t_secs = os.time(t) -- get seconds if t was in local time. 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 converted to. t_UTC.isdst = t.isdst -- apply DST to this time if necessary. 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)
end