Page 1 of 1

Traffic ETA time calculating

Posted: Monday 11 May 2020 19:14
by dennis075
Goodevening,

I got a part of the google maps ETA script that i want to tweak, see below:

Code: Select all

("%02d:%02d"):format(time.hour, time.min+(traffic_Home2Work)
Traffic_Home2Work is a variable for the calculated travel time. I want to add it to the current time to know the ETA.
16:20 + 20 min works fine --> 16:40
16:50 + 20 min gives 16:70

On the forum I can't find the correct way of coding, hope someone can help me as this is probably not the hardest question here ;)

Re: Traffic ETA time calculating

Posted: Monday 11 May 2020 22:13
by waaren
dennis075 wrote: Monday 11 May 2020 19:14
I got a part of the google maps ETA script that i want to tweak, see below:

Traffic_Home2Work is a variable for the calculated travel time. I want to add it to the current time to know the ETA.
16:20 + 20 min works fine --> 16:40
16:50 + 20 min gives 16:70
Time / date are not the easiest part of Lua.

See below a solution.

Code: Select all

local traffic_Home2Work = 20 -- travel time in minutes

local now = os.date('*t') -- now is a time object based on current time ; os.date('*t') is equivalent to os.date('*t', os.time())
local atWork = os.date('*t', os.time() + traffic_Home2Work * 60) -- os.time() = number of seconds of current time since 1970/01/01 

print('Current time: ' .. string.format('%02d:%02d', now.hour, now.min))
print('Arrival at work after ' .. traffic_Home2Work .. ' minutes travel: ' ..  string.format('%02d:%02d', atWork.hour, atWork.min))

Re: Traffic ETA time calculating

Posted: Friday 15 May 2020 11:35
by dennis075
Thanks for taking time to help me out. A little late reaction, but I just implemented it in my script and works perfect! Thanks a lot.