Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count time in Python 3?

I want a code that shows the complete time someone has been in a voice channel, but I don't know how to start and stop a counter.

@bot.event
async def on_voice_state_update(before, after):

    if after.voice.voice_channel:
        timestrr = time.strftime("%d.%m.%Y-%H:%M:%S")
        voicezeit(after.id, timestrr)
#here should a timer start
    else:
         #and here should the timer stop
like image 423
Deniz Ascherl Avatar asked Jun 21 '19 23:06

Deniz Ascherl


People also ask

How do you count time in Python?

To measure time elapsed during program's execution, either use time. clock() or time. time() functions. The python docs state that this function should be used for benchmarking purposes.

Is time included in Python 3?

Python 3 - time time() MethodThe method time() returns the time as a floating point number expressed in seconds since the epoch, in UTC. Note − Even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.


2 Answers

If you just want to measure the elapsed wall-clock time between two points, you could use time.time():

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

This gives the execution time in seconds.

Another option since 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock . However, it is currently deprecated:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

like image 150
Kais Tounsi Avatar answered Oct 07 '22 22:10

Kais Tounsi


Why do you need a counter? Just initialise start_time variable to None in the beginning, then in your if-block you check whether it is set, if it isn't you set it to time.time(). In the else-block set end_time to time.time() again and calculate the difference.

EDIT

I don't know what the layout of the rest of your application, you have to initialise start_time = None somewhere outside of this update function. You would need to set it for each user, I will assume it is stored as user.start_time, but again, this is dependent on the structure of your app. Then your function could become this:

    @bot.event
    async def on_voice_state_update(before, after):

        if after.voice.voice_channel:
            if not user.start_time:  # this was set to None somewhere else
                user.start_time = time.time()
            # import datetime from datetime at the top of your file
            timestrr = datetime.from_timestamp(user.start_time).strftime("%d.%m.%Y-%H:%M:%S")
            voicezeit(after.id, timestrr)
        else:
             end_time = time.time()
             voice_chat_time = end_time - after.user.start_time
like image 44
iuvbio Avatar answered Oct 07 '22 23:10

iuvbio