Re: countdown display code
- Posted by irv Aug 16, 2020
- 1647 views
Here's a version that avoids nasty floating point math:
include std/os.e include std/console.e include std/datetime.e public procedure now_until(datetime dt={2021,12,25,12,0,0}) object dt1 -- (computer time and date) object dt2 = to_unix(dt) object target = datetime:format(dt,"%A, %b %d, %Y %l:%M:%S %p") object diff -- in seconds integer sec_now = 0, sec_prev = 0 loop do dt1 = to_unix(datetime:now()) diff = dt2-dt1 diff = from_unix(diff) diff -= {1970,1,1,0,0,0} -- adj for unix EPOCH sec_now = diff[6] if sec_now != sec_prev then display("Time remaining: [b]-[b]-[b] []:[]:[]",diff,0) display(" until []",{target}) sec_prev = diff[6] end if sleep(.1) -- note: must check more than once per second, or time will "jitter" until equal(dt1,dt2) end loop end procedure now_until()
Now you know how much time you have to buy me a Christmas gift for next year:
Time remaining: 1-4-10 19:37:9 until Saturday, Dec 25, 2021 12:00:00 AM Time remaining: 1-4-10 19:37:8 until Saturday, Dec 25, 2021 12:00:00 AM Time remaining: 1-4-10 19:37:7 until Saturday, Dec 25, 2021 12:00:00 AM