0

Trying to change my timer app made from anvil to display HH:MM:SS instead just seconds

"self.label_time_elapsed.text = "{}".format( (datetime.now() - self.start_time).strftime('%H:%M:%S')) )" - tried this from another question i asked, now i have a different bug

from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from datetime import datetime


class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    self.start_time = None

  def button_start_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.start_time = datetime.now()
    self.button_start.visible = False
    self.button_stop.visible = True

  def button_stop_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.stop_time = datetime.now()
    self.total_time = self.stop_time - self.start_time
    anvil.server.call('add_session', self.start_time, 
 self.total_time.seconds)

    self.start_time = None
    self.button_start.visible = True
    self.button_stop.visible = False

  def timer_progress_tick(self, **event_args):
     """This method is called Every [interval] seconds. Does not trigger
     if [interval] is 0."""
    if self.start_time is not None:
     self.label_time_elapsed.text = "{}".format(
         (datetime.now() - self.start_time).strptime('%H:%M:%S'))

AttributeError: 'timedelta' object has no attribute 'strptime' at Form1, line 38

pysolver33
  • 307
  • 1
  • 5
  • 13
  • https://docs.python.org/3/library/datetime.html#timedelta-objects – jpmc26 Jun 18 '19 at 18:16
  • Possible duplicate of [Python format timedelta to string](https://stackoverflow.com/questions/538666/python-format-timedelta-to-string) – jpmc26 Jun 18 '19 at 18:16

2 Answers2

2

timedelta objects contain the delta between two dates - 2 years, 2 milliseconds, etc.

You're trying to coerce this timedelta into a string representation of a date / time; a moment in time, which isn't possible.

now - self.start_time isn't a date or time, it's the amount of time elapsed between then and now. Express it as, say, seconds instead:

(datetime.now() - self.start_time).total_seconds()

edit: To convert those seconds into HH:MM:SS :

td = datetime.now() - self.start_time
minutes, seconds = divmod(td.seconds + td.days * 86400, 60)
hours, minutes = divmod(minutes, 60)
self.label_time_elapsed.text = '{:d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
Danielle M.
  • 3,607
  • 1
  • 14
  • 31
1

You can try using a custom function.

Ex:

import datetime

def cust(seconds):
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return "{hour}:{min}:{sec}".format(hour=int(h), min=int(m), sec=int(s))    

now = datetime.datetime.now()
d = (now - datetime.datetime.strptime("2016-01-01", "%Y-%m-%d")).total_seconds()
print(cust(d))

Output:

30349:52:12

In your case

cust(datetime.now() - self.start_time).total_seconds())
Rakesh
  • 81,458
  • 17
  • 76
  • 113