#Discord.py: if I press the start button and then the stop button, how can I calculate the time elaps

103 messages · Page 1 of 1 (latest)

wispy torrent
#

I have to calculate the time elapsed between the start and stop button, how do I do it?

tranquil lion
#

personally, i would subclass your view, then in the callback of the start button, store the current time as start_time, then in the stop button callback, store stop_time as the current time, then subtract start time from the end time

#

@wispy torrent ^^

tranquil lion
#

i would use time.now()

open flame
#
import time
start = time.time()
elapsed = time.time() - start

Would this work?

wispy torrent
#

wait, I'll try

open flame
#

There are more accurate, higher resolution methods, for debugging but if you want to measure things in 100ms or seconds then this is perfect

wispy torrent
#

In hour ?

#

Maybe doing the conversion from seconds to minutes and minutes to hours would work?

open flame
#

Yeah sure will work perfectly. You're basically getting two separate times from the clock and subtracting them to find the elapsed time

#

Yes youll have to convert the result if you want to know the value in
H M S format

wispy torrent
#

Now I'll try, can you wait 2 seconds here?

open flame
#

yeah im here

#

?

wispy torrent
#

Wait pls

#

Im slow

#

It doesn't work, I'll send you the error

open flame
#

show me the code for stop_time and start_time

wispy torrent
open flame
#

the start_time variable is declared inside one function, so it remains in that functions local scope table meaning it cannot be accessed outside that function. You can either make it a global variable, or try to pass it into fine()

wispy torrent
#

How can I create a global variable?

open flame
#

just declare it outside inizio() then assign it a value inside inizio. Ofc you cannot simply declare variables in python, so you need to initialise it with whatever value

#

So like:

start_time = "nothing"

# Code here

async def inizio(...):
  global start_time
  start_time = time.time()

# Code here
#

Don't forget to tell the interpreter that it's a global variable, when you're inside inizio()

wispy torrent
#

Okok

#

i'll try now

#

It keeps giving the same error over and over again

wispy torrent
#

:<

open flame
#

let me see

wispy torrent
#

The error is the same as before

wispy torrent
open flame
#

Revert code back to what it was in screenshot.

In inizio() change self.start_time to SimpleView2.start_time and the same for stop_time

#

If this doesnt work 💀

#

@wispy torrent

tranquil lion
#

stop time would be the current time, then the elapsed time would be the stop_time - start_time

#

and do not use global here

open flame
#

yeah no global. didnt see it was a class

wispy torrent
wispy torrent
open flame
#

basically remove the global lines
keep the self.start/stop_time in init, no other declarations
inside inizio() and fine(), change self to class name

wispy torrent
#

wait

#

Im confused

#

and stupid

#

What should I do here?

#

@open flame @tranquil lion ?

open flame
#

Above channel = bot.get_token..., add SimpleView2.start_time = time.time()

#

@wispy torrent

tranquil lion
#

you've gone back about 3 steps

#

just implement what i said originally, its easy

tranquil lion
#

your editing things that dont need to be edited

#

do you know OOP?

wispy torrent
#

Always the same mistake

tranquil lion
#

things in your fine callback are still wrong

#

you dont use SimpleView2, you should use self

#

you have done it multiple times aswell

wispy torrent
#

I changed SimpleView2.stop/start_time

In Init, is it ok now?

tranquil lion
#

show me ur code

#

and why dont you test it

#

and tell me if it gives you an error

untold crescentBOT
#

@wispy torrent

File Attachments Not Allowed

For safety reasons we do not allow files with certain file extensions.

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use Hastebin or GitHub Gists and share the link here

Ignored these files due to them having disallowed file extensions
  • ada.txt
wispy torrent
open flame
wispy torrent
#

There is no error, write the number in the message, are they seconds and milliseconds?

open flame
#

seconds

wispy torrent
open flame
#

10 is 10 seconds

wispy torrent
#

Okok

#

I counted the seconds and they are correct, it works

#

Now I have to convert the elapsed seconds into minutes and hours too

open flame
#

thats great

#

yeah

wispy torrent
#

How can I do ?

#

I promise this is the last thing I ask of you

open flame
#

its ok dw about it

#

you can calculate the hours and minutes separately using the % operator, or use timedelta, which i'll post in a minute

#
from datetime import timedelta

time_d = timedelta(seconds=int(elapsed_time))
#

Did you get the result you wanted?

wispy torrent
#

I didn't understand how to do it, sorry

open flame
#

Add

from datetime import timedelta

at the top where you import things.
Then, under the elapsed_time initialisation, add

time_d = timedelta(seconds=int(elapsed_time))
#

actually wait

#

dont name it time

#

This should return HH:MM:SS

wispy torrent
#

Its ok ?

open flame
#

yes

wispy torrent
#

IT WORKS!!!!!

#

Thank you so much, I couldn't have done it without you!

#

I love you

#

@open flame@tranquil lion