繁体   English   中英

python和小时公式计算

[英]python and hours formula calculation

我有一些工作代码,我改变了,但我似乎无法得到小时变量的数学。 我想假期我需要很多食物,因为我想要一个空白。

## {{{ 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/ }}}

从另一端开始工作要容易得多。 您不需要像86400这样的大数字,这使得代码审核者可以使用他们的计算器应用程序。

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)

或者,避免函数调用:

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)

我从方程式推断出elap是以秒为单位测量的。 由于您将小时数提取到单独的变量中,因此需要从分钟计数中删除它们。 当然,由于minutes的含义与原始代码有所不同,因此您需要在计算的其余部分中遵循此操作。

hours = int(elap/3600)
minutes = int((elap-hours*3600)/60)
seconds = int(elap-hours*3600-minutes*60)
hseconds = int((elap-hours*3600-minutes*60-seconds)*100)

我想如果我写这篇文章,我会修改elap因为我一直在努力减少重复。

hours = int(elap/3600)
elap -= hours*3600
minutes = int(elap/60)
elap -= minutes*60
seconds = int(elap)
elap -= seconds
hseconds = int(elap*100)

这样做可以更容易地看到正在发生的事情,并且将来也更容易修改。 例如,如果您想在混合中添加天数,那么您需要做的就是将其移植到代码的开头:

days = int(elap/86400)
elap -= days*86400

现在,我在这里编写了代码,假设elap是一个float ,当然它在你的程序中。 如果你特别偏执,你会在执行算术之前编写elap = float(elap)

但我同意@soulcheck认为使用库函数更清晰。

hours = int(elap/ 3600)

minutes = int((elap % 3600) / 60)

seconds = int(elap % 60) 

hseconds = int((elap % 1) * 100)

也许你最好用datetime.fromtimestamp转换它并使用它。

编辑:添加所有缺少的公式

目前无法测试,但是通过代码elap是经过的秒数。 因此,您将它除以3600并使用int()其向下舍入。 对于前面的代码来说,这意味着它可以在例如90分钟之前,但现在应该是1小时30分钟。 因此,除了在计算小时,你也必须调整minutessecondshseconds相应。

def _setTime(self, elap):
    """ Set the time string to Hours:Minutes:Seconds:Hundreths """
    hours = int(elap/3600)
    minutes = int(elap/60 - hours*60.0)
    seconds = int(elap - hours*3600.0 - minutes*60.0)
    hseconds = int((elap - hours*3600.0 - 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM