Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and hours formula calculation

Tags:

python

math

I have some working code that I altered but I can't seem to get the math right for the hours variable. I think I had to much food for the holidays because I'm coming up with a blank.

## {{{ http://code.activestate.com/recipes/124894/ (r2)
from Tkinter import *
import time
import pygtk
import gtk
import time

class StopWatch(Frame):  
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()      

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=2, padx=2)                      

    def _update(self): 
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(50, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        hours = int(elap) #cant remember formula 
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)
        sn = time.strftime('%m/%d/%Y-%H:%M:%S')                
        self.timestr.set('%02s\n\n%02dh:%02dm:%02ds:%02d' % (sn,hours,minutes, seconds, hseconds))

    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1        

    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0

    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)



def main():
    root = Tk()
    root.title( "Stop Watch" )
    sw = StopWatch(root)
    sw.pack(side=TOP)

    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)

    root.mainloop()

if __name__ == '__main__':
    main()
## end of http://code.activestate.com/recipes/124894/ }}}
like image 647
Rick T Avatar asked Sep 28 '26 23:09

Rick T


1 Answers

It is much easier to work from the other end. You don't need big numbers like 86400 which make code reviewers reach for their calculator app.

c = int(elap * 100) # centiseconds
s, c = divmod(c, 100)
m, s = divmod(s, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
print(d, h, m, s, c)

or, avoiding the function calls:

c = int(elap * 100) # centiseconds
s = c // 100; c %= 100
m = s //  60; s %=  60
h = m //  60; m %=  60
d = h //  24; h %=  24
print(d, h, m, s, c)
like image 187
John Machin Avatar answered Sep 30 '26 11:09

John Machin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!