Hey all, so I’ve been racking my brain over this for some time but can’t quite seem to figure it out.
What I need to do: Calculate how much time in hours and minutes is left until 23:59 Eastern time (UTC-4hrs).
I’ve read Rob’s “Working with Time and Dates” tutorial, as well as came across other code, but just can’t seem to put the pieces together.
Some time zone code I came across:
-- Compute the difference in seconds between local time and UTC. local function get\_timezone() local now = os.time() return os.difftime(now, os.time(os.date("!\*t", now))) end timezone = get\_timezone() -- Return a timezone string in ISO 8601:2000 standard form (+hhmm or -hhmm) local function get\_tzoffset(timezone) local h, m = math.modf(timezone / 3600) return string.format("%+.4d", 100 \* h + 60 \* m) end tzoffset = get\_tzoffset(timezone) --[[debugging for \_, tz in ipairs(arg) do if tz == '-' then tz = timezone else tz = 0 + tz end print(tz, get\_tzoffset(tz)) end --]] -- return the timezone offset in seconds, as it was on the time given by ts -- Eric Feliksik local function get\_timezone\_offset(ts) local utcdate = os.date("!\*t", ts) local localdate = os.date("\*t", ts) localdate.isdst = false -- this is the trick return os.difftime(os.time(localdate), os.time(utcdate)) end
And Rob’s timestamp code:
function makeTimeStamp(dateString) local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%p])(%d%d)%:?(%d%d)" local year, month, day, hour, minute, seconds, tzoffset, offsethour, offsetmin = dateString:match(pattern) local timestamp = os.time( {year=year, month=month, day=day, hour=hour, min=minute, sec=seconds} ) local offset = 0 if ( tzoffset ) then if ( tzoffset == "+" or tzoffset == "-" ) then -- we have a timezone! offset = offsethour \* 60 + offsetmin if ( tzoffset == "-" ) then offset = offset \* -1 end timestamp = timestamp + offset end end return timestamp end
So basically my app has a timer and every day the timer resets and shows the user the number of hours and minutes until 11:59 EDT. The trouble I’m having is calculating the difference of time between now and a future set time (i.e. 06:59 UTC). Any advice would be greatly appreciate