You could just take the remainder when dividing by 65535 with the modulo operator %:
In [114]: int(time.perf_counter()*1000) % 65535
Out[114]: 29548
If you also are interested to know how many times the timer has been resetted, you can use divmod:
In [115]: divmod(int(time.perf_counter()*1000),65535)
Out[115]: (31, 30036)
Answer from Niko Fohr on Stack OverflowBeen learning functions and how to add them to buttons and working with time in general. So wanted to create a little project that will test me on those areas.
import tkinter as tk
from tkinter import *
import time
root = Tk()
root.title(' COUNT-DOWN TIMER')
#root.geometry("800x400")
root['background']='#2F4F4F'
total_time = 0
rest = 0
pause = "No"
#####ADD/MINUS/RESET FUNCTIONS
def add_time_hour():
global total_time
total_time += 3600
countdown_label.config(text=f"{total_time//3600:02}:{(total_time//60)%60:02}:{total_time%60:02}",fg = '#00FF00',font=("Arial", 24))
def add_time_min():
global total_time
total_time += 60
countdown_label.config(text=f"{total_time//3600:02}:{(total_time//60)%60:02}:{total_time%60:02}",fg= "#00FF00",font=("Arial", 24))
def add_time_sec():
global total_time
total_time += 1
countdown_label.config(text=f"{total_time//3600:02}:{(total_time//60)%60:02}:{total_time%60:02}",fg= "#00FF00",font=("Arial", 24))
def add_rest_min():
global rest
rest += 60
rest_label.config(text=f"{(rest // 3600) % 3600:02}:{(rest // 60) % 60:02}:{rest % 60:02}",fg= "#00FF00",font=("Arial", 24))
def reset():
global total_time, rest,pause
pause = "No"
total_time = 0
rest = 0
countdown_label.config(text=f"{total_time//3600:02}:{(total_time//60)%60:02}:{total_time%60:02}",fg= "#00FF00",font=("Arial", 24))
rest_label.config(text=f"{(rest // 3600) % 3600:02}:{(rest // 60) % 60:02}:{rest % 60:02}",fg= "#00FF00",font=("Arial", 24))
############TIME FUNCTIONS#############################################
def countdown():
global total_time,rest, pause
if total_time > 0 and pause == "No":
total_time -= 1
countdown_label.config(text=f"{(total_time // 3600) % 3600:02}:{(total_time // 60) % 60:02}:{total_time % 60:02}",fg="#00FF00", font=("Arial", 24))
root.after(1000,countdown)
if total_time == 0:
countdown_label.config(
text=f"{(total_time // 3600) % 3600:02}:{(total_time // 60) % 60:02}:{total_time % 60:02}", fg="red",
font=("Arial", 24))
elif total_time == 0 and rest > 0 and pause == "No":
rest -= 1
rest_label.config(text=f"{(rest// 3600) % 3600:02}:{(rest // 60) % 60:02}:{rest % 60:02}", fg="#00FF00", font=("Arial", 24))
root.after(1000,countdown)
if rest == 0:
rest_label.config(text=f"{(rest // 3600) % 3600:02}:{(rest // 60) % 60:02}:{rest % 60:02}", fg="red",
font=("Arial", 24))
def countdown_pause():
global pause
pause = "Yes"
def countdown_unpause():
global pause
pause = "No"
countdown()
#############LABELS####################
countdown_label = tk.Label(text = "00:00:00",fg="#00FF00",bg='#2F4F4F', font=("Arial", 24))
countdown_label.grid(row= 0, column = 2, columnspan=2)
countdown_label_title = tk.Label(text = "RUNNING: ",fg="#00FF00",bg='#2F4F4F', font=("Arial", 24))
countdown_label_title.grid(row= 0, column = 0, columnspan=2)
rest_label = tk.Label(text = "00:00:00", fg= "#00FF00",bg='#2F4F4F',font=("Arial", 24))
rest_label.grid(row= 1, column = 2, columnspan=2)
rest_label_title = tk.Label(text = "RESTING: ",fg="#00FF00",bg='#2F4F4F', font=("Arial", 24))
rest_label_title.grid(row= 1, column = 0, columnspan= 2)
#BUTTONS###
add_button = tk.Button(root, text="START", command=countdown, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 3, column = 1)
add_button = tk.Button(root, text="+HOUR", command=add_time_hour, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 2, column = 0)
add_button = tk.Button(root, text="+MNTS", command=add_time_min, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 2, column = 1)
add_button = tk.Button(root, text="+SCND", command=add_time_sec, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 2, column = 2)
add_button = tk.Button(root, text="RESET ", command=reset, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 3, column = 0)
add_button = tk.Button(root, text="RST-MIN", command=add_rest_min, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 2, column = 3)
add_button = tk.Button(root, text="PAUSE", command=countdown_pause, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 3, column = 2)
add_button = tk.Button(root, text="RESUME", command=countdown_unpause, bg='#36648B', font=("Arial", 18))
add_button.grid(row= 3, column = 3)
root.mainloop()Resetting time.clock() in Python - Stack Overflow
python - how can I reset the counter? - Stack Overflow
Reset counter to zero if different day - Python - Stack Overflow
python - How to set the counter to 0 again after button click? - Stack Overflow
Videos
You have two options; your code doesn't work because you are trying to reset the clock but instead you reset elapsed, which does nothing.
Using modulo division.
start = time.clock()
while True:
elapsed = (time.clock() - start)
if int(elapsed) % 10:
print("MOTION")
Resetting the clock.
start = time.clock()
while True:
elapsed = (time.clock() - start)
if elapsed >= 10:
print("MOTION")
start = time.clock()
You neglected to reset your reference time: change the basis, not the interval. On each iteration, you reset elapsed to 0, but then immediately go back to the original start time. Change the last line of you loop:
start = time.clock()
while True:
elapsed = (time.clock() - start)
if elapsed > 10:
print("MOTION")
start = time.clock()
» pip install count-timer
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_counterorprocess_time, depending on your requirements. Before 3.3 it was recommended to usetime.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.
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
I would suggest using the time module for something like this:
from time import time, sleep
from datetime import datetime, timedelta
nowtime = time()
#Put your script here
x = 1
for k in range(1000):
x+=1
sleep(0.01)
sec = timedelta(seconds=int(time()-nowtime))
d = datetime(1,1,1)+sec
print("DAYS:HOURS:MIN:SEC")
print("%d:%d:%d:%d" % (d.day-1, d.hour, d.minute, d.second))
This assigns the time in seconds at the beginning to a variable, and after the main script has finished, it subtracts the previous time from the current time and formats it in days, hours, minutes, and seconds.
Here is it running:
bash-3.2$ python timing.py
DAYS:HOURS:MIN:SEC
0:0:0:10
bash-3.2$
You could also use the Threading module, which has a built-in cancel method:
>>> import threading
>>> def hello():
... print "This will print after a desired period of time"
...
>>> timer = threading.Timer(3.0, hello)
>>> timer.start() #After 3.0 seconds, "This will print after a desired period of time" will be printed
>>> This will print after a desired period of time
>>> timer.start()
>>> timer = threading.Timer(3.0, hello)
>>> timer.start()
>>> timer.cancel()
>>>
Python actually has a class for this, which includes a cancel method: threading.Timer. It seems to be close enough to the Java Timer class for your needs (The Java Timer also runs in background thread). Here's the example usage from the docs:
def hello():
print "hello, world"
t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
Edit:
The problem with your updated code is that you're trying to use the same Timer object more than once. That may be possible in the Java implementation, but in Python you can't reuse a Thread object (Timer is a Thread subclass). You'll need to create a new Timer object after you join() it. Like this:
t = threading.Timer(5.0, countdown)
def enter():
global t # You need this to tell Python that you're going to change the global t variable. If you don't do this, using 't = ..' will just create a local t variable.
if int(Variable.count == 1):
print("Entered")
t.start()
else:
print("Entered +1")
t.cancel()
t.join() # here you block the main thread until the timer is completely stopped
t = threading.Timer(5.0, countdown)
t.start()