0

I am currently trying to output a time to flask, this needs to be displayed in minutes and seconds, an example being:

2:14

I tried using the datetime/timedelta module but it does not appear to work with flask, here is a snippet of my code

 if (len(red_circles[0, :]) == 7):
                start_time = time.time()


            else:
                if len(red_circles[0, :]) == 0:
                    end_time = time.time()
                    time_taken = end_time - start_time

                    times.append(time_taken)
                    average = sum(times) / len(times) / 60

Any help would be appreciated!

Ethan0619
  • 25
  • 1
  • 6
  • 1
    several options here: https://stackoverflow.com/questions/775049/how-do-i-convert-seconds-to-hours-minutes-and-seconds – ilias-sp Jul 26 '22 at 13:40
  • Hi, thanks for the reply but most of these answers use timedelta which i am unable to use with flask – Ethan0619 Jul 26 '22 at 14:01
  • What exactly do you mean by 'it does not appear to work with Flask'? – NoCommandLine Jul 26 '22 at 14:06
  • when i try using timedelta the output does not appear on the flask webpage – Ethan0619 Jul 26 '22 at 14:10
  • Why can't you use time delta with flask? Flask is just plain old python code, it will work fine with time delta. When you say "I'm am currently trying to output a time to flask" are you talking about a time from a database? Current time on the server? current time on the user's machine? A countdown time you want to update? How long some task took to run? – barryodev Jul 26 '22 at 14:11
  • I'm using opencv.. it's a program that detects objects... when a object is visible a timer starts and when no object is visible the time stops.. it then prints the time, i tried using str(datetime.timedelta(seconds=average))[2:7], this worked when printing to terminal but not with flask – Ethan0619 Jul 26 '22 at 14:30

1 Answers1

1

I'm not sure why you can't get it to work with flask, seems like it might be a variable misnamed but here's a solution I've tested on my machine printing to terminal and returning from a flask route. It uses datetime.now() because the formatting is a bit easier to split/slice.

from datetime import datetime
import time

start = datetime.now()
time.sleep(5)
end = datetime.now()
duration = end - start

duration_in_minutes_and_seconds = str(duration).split('.')[0][2:]

print(duration_in_minutes_and_seconds)
barryodev
  • 509
  • 3
  • 11
  • 1
    Thanks.... figured out what i did wrong.. thanks for ensuring me that datetime works! :) – Ethan0619 Jul 26 '22 at 15:04
  • Ah cool, yeah I was thinking something funky was happening with your import or variable casting in your flask code. Glad you got it working. – barryodev Jul 26 '22 at 15:14