【问题标题】:Python and time / datetimePython和时间/日期时间
【发布时间】:2016-10-02 11:26:10
【问题描述】:

我最近开始编写一个 Python 程序,如下面的片段所示。

# General Variables
running = False
new = True
timeStart = 0.0
timeElapsed = 0.0

def endProg():
    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    curses.endwin()
    quit()

# Draw
def draw():
    stdscr.addstr(1, 1, ">", curses.color_pair(6))
    stdscr.border()
    if running:
        stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.ctime( timeStart - timeElapsed ) ) )

        stdscr.redrawwin()
    stdscr.refresh()

# Calculate
def calc():
    if running:
        timeElapsed = t.clock() - timeStart

stdscr.border()
stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( t.clock() - t.clock() ) ) )

# Main Loop
while True:
    # Get Input
    kInput = stdscr.getch()

    # Close the program
    if kInput == ord('q'):
        endProg()

    # Stop the current run
    elif kInput == ord('s'):
        stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( t.clock() - t.clock() ) ) )
        running = False
        new = True

    # Start a run
    elif kInput == ord(' ') and new:
        running = not running
        new = not new
        timeStart = dt.datetime.now()

    # Toggle the timer
    elif kInput == ord('p') and not new:
        timeStart = dt.datetime.now() - timeStart
        running = not running

    calc()
    draw()

我的程序目前有点介于解决方案之间,如果看起来不正确,请见谅。我很乐意解释。

过去几个小时我一直在网上阅读有关 python 的时间和日期时间模块的信息,试图弄清楚如何使用它们来实现我的目标,但是我尝试实现它们却没有用。

基本上,我需要我的程序来测量从按下按钮开始经过的时间,并能够以小时:分钟.秒的格式显示它。减法使其变得非常困难,必须实现诸如 timedelta 之类的东西。根据我在网上阅读的内容,如果没有 datetime 模块,就无法做我想做的事,但它给我的只是问题。

是否有更简单的解决方案,我的代码是否有任何未解决的错误,我有多愚蠢?

【问题讨论】:

  • 您可以使用time.time 来测量经过的时间。

标签: python datetime time timer


【解决方案1】:

使用ˋtime.time`:

import time

star = time.time()
# run long processing...
elapsed = time.time() - start

等等!

【讨论】:

    【解决方案2】:
    def draw():
        stdscr.border()
        if running:
            stdscr.addstr(1, 1, ">", curses.color_pair(8))
            stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( timeElapsed ) ) )
    
        if not running:
            stdscr.addstr(1, 1, ">", curses.color_pair(7))
    
        stdscr.redrawwin()
        stdscr.refresh()
    
    # Calculate
    def calc():
        if running:
            timeElapsed = t.time() - timeStart
    
    stdscr.border()
    stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( t.time() - t.time() ) ) )
    
    # Main Loop
    while True:
        # Get Input
        kInput = stdscr.getch()
    
        # Close the program
        if kInput == ord('q'):
            endProg()
    
        # Stop the current run
        elif kInput == ord('s'):
            running = False
            new = True
    
        # Start a run
        elif kInput == ord(' ') and new:
            running = not running
            new = not new
            timeStart = t.time()
    
        # Toggle the timer
        elif kInput == ord('p') and not new:
            running = not running
            timeStart = t.time() - timeStart
    
        calc()
        draw()
    

    好吧,我设法摆脱了我遇到的所有错误。但是显示的时间不正确。

    由于我们要查找经过的时间,它应该从 0 开始并增加。我的程序从 12:21.xx 开始,并且还在增加。

    查看我的代码,我没有看到任何错误,但很明显那里有一个错误。

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      相关资源
      最近更新 更多