Re: countdown display code

new topic     » goto parent     » topic index » view thread      » older message » newer message

Not exactly an error, more a misunderstanding of how truncate works, and Euphoria's printf() hiding what's really happening. trunc() returns the integer portion of a number, so even 9.9999999999 will come out as 9. That's not what's needed here.

--  
-- count_down.ex (revised) 
--  
include std/datetime.e  
include std/math.e  
include std/graphics.e  
include std/os.e  
include std/console.e 
atom s,t,r 
 
function DaysBetweenDates(datetime dt1,datetime dt2)  
atom d,h,m 
s=diff(dt1,dt2)  
d=s/86400  
    h=frac(d)*24 -- hours.xxx  
    d=trunc(d)   -- days  
        m=frac(h)*60    -- minutes.xxx  
        h=trunc(h)      -- hours  
            s=frac(m)*60   -- seconds.xxx  
            m=trunc(m)     -- minutes 
               t = trunc(s) 
               r = round(s,1) 
return {d,h,m,s}  
end function  
  
public procedure now_until(datetime dt2={2020,9,1,0,0,0})  
sequence bal, pos = get_position()  
object prevbal = 0 
loop do  
    bal=DaysBetweenDates(now(),dt2) 
        display("[]-[]:[]:[] ",bal,0) display("s=[] t=[] r=[]",{s,t,r}) 
    sleep(1) 
    until equal(bal,dt2)    
end loop  
end procedure  
  
now_until()  
 

The actual numbers are like this: s = raw seconds, t = trunc(s), r = round(s)

15-9:42:16 s=16 t=16 r=16 
15-9:42:15 s=15 t=14 r=15 
15-9:42:14 s=14 t=14 r=14 
15-9:42:13 s=13 t=12 r=13 
15-9:42:12 s=12 t=11 r=12 
15-9:42:11 s=11 t=11 r=11 
15-9:42:9.99999999999998 s=9.99999999999998 t=9 r=10 
15-9:42:9.00000000000002 s=9.00000000000002 t=9 r=9 
15-9:42:7.99999999999999 s=7.99999999999999 t=7 r=8 
15-9:42:7.00000000000004 s=7.00000000000004 t=7 r=7 
15-9:42:6 s=6 t=6 r=6 
15-9:42:4.99999999999997 s=4.99999999999997 t=4 r=5 
15-9:42:4.00000000000002 s=4.00000000000002 t=4 r=4 
15-9:42:2.99999999999998 s=2.99999999999998 t=2 r=3 
15-9:42:2.00000000000003 s=2.00000000000003 t=2 r=2 
15-9:42:0.999999999999996 s=0.999999999999996 t=0 r=1 
15-9:41:60 s=60 t=59 r=60 
15-9:41:59 s=59 t=59 r=59 
15-9:41:58 s=58 t=57 r=58 
 

Note: Will you have to use round instead of trunc for the minutes, hours and days as well?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu