Time Difference & Time Zones

I see.  Yes, it would return hours/mins to 23:59 localtime. Maybe someone can come up with an answer for you faster than I can.  I’m going to have to think about it.

You should be able to find your offset and then add/subtract from -4:00 from it.

Yup, that’s where I was going in my solution.  Subtracting the -14400 from the UTC time gave NY time.  However it should have actually been -10800 because of DST.  Please try this code and tell me if it works for you:

local function timeToMidnight() local function get\_timezone() local now = os.time() return os.difftime(now, os.time(os.date("!\*t", now))) end offset = get\_timezone() local myTimeNow = os.date("\*t", os.time()) local midnight = {year=myTimeNow.year, month=myTimeNow.month, day=myTimeNow.day, hour=23, min=59, isdst = true} UTCMidnight = os.date("\*t", os.time(midnight)) UTCMidnight.isdst = true UTCMidnight = os.time(UTCMidnight) print("UTCMidnight: " .. UTCMidnight) currentUTC = os.date("!\*t", os.time()) currentUTCsecs = (os.time(currentUTC)) + offset print("CurrentUTC: " .. currentUTCsecs) NYMidnight = UTCMidnight - 10800 print("NYMidnight: " .. NYMidnight) timeDifference = ((os.difftime(NYMidnight, currentUTCsecs))/60)/60 hoursRemaining = math.floor(timeDifference) minsRemaining = math.round((timeDifference%1)\*60) print(hoursRemaining .. " hours and " .. minsRemaining .. " mins") end timeToMidnight()

UTCMidnight: 1364788740
CurrentUTC: 1364748036
NYMidnight: 1364777940
8 hours and 18 mins

However, that would put me at 9pm ET.

What time zone are you in?

Eastern EDT

Can you run this for me:

local function timeToMidnight() local function get\_timezone() local now = os.time() return os.difftime(now, os.time(os.date("!\*t", now))) end offset = get\_timezone() print("Offset: " .. offset) local myTimeNow = os.date("\*t", os.time()) local midnight = {year=myTimeNow.year, month=myTimeNow.month, day=myTimeNow.day, hour=23, min=59, isdst = true} UTCMidnight = os.date("\*t", os.time(midnight)) UTCMidnight.isdst = true UTCMidnight = os.time(UTCMidnight) print("UTCMidnight: " .. UTCMidnight) currentUTC = os.date("!\*t", os.time()) currentUTCsecs = (os.time(currentUTC)) + offset print("CurrentUTC: " .. currentUTCsecs) NYMidnight = UTCMidnight - 10800 print("NYMidnight: " .. NYMidnight) timeDifference = ((os.difftime(NYMidnight, currentUTCsecs))/60)/60 hoursRemaining = math.floor(timeDifference) minsRemaining = math.round((timeDifference%1)\*60) print(hoursRemaining .. " hours and " .. minsRemaining .. " mins") end timeToMidnight()

2013-03-31 15:19:32.783 Corona Simulator[5389:707] Offset: -18000
2013-03-31 15:19:32.783 Corona Simulator[5389:707] UTCMidnight: 1364788740
2013-03-31 15:19:32.784 Corona Simulator[5389:707] CurrentUTC: 1364757572
2013-03-31 15:19:32.784 Corona Simulator[5389:707] NYMidnight: 1364777940
2013-03-31 15:19:32.784 Corona Simulator[5389:707] 5 hours and 39 mins

Still 9pm ET

Ok, now I think I got it.  Please try this one:

local function timeToMidnight() 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 local offset = get\_timezone\_offset(os.time()) local UTCNow = os.time() local UTCTable = os.date("\*t", UTCNow) local dstValue if UTCTable.isdst == true then dstValue = -14400 else dstValue = -18000 end local midnight = {year=UTCTable.year, month=UTCTable.month, day=UTCTable.day, hour=23, min=59} local timeDifference = (((os.time(midnight) - (UTCNow + dstValue)) + offset)/60)/60 hoursRemaining = math.floor(timeDifference) minsRemaining = math.round((timeDifference%1)\*60) print(hoursRemaining .. " hours and " .. minsRemaining .. " mins") end timeToMidnight()

That worked!

Cool, thanks for all your input. Maybe some one else would be able to test the above code as well? I’m interested to see the results from some one east of UTC 0.

You should be able to change your timezone on your computer.