Answer the question
In order to leave comments, you need to log in
What is the best way to calculate time?
There is such a piece of code that displays the speed of the cursor and the time when the cursor moved. The task is to calculate the total time the cursor has moved since the code was launched
import sys
import math
import time
from datetime import datetime
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from toolz.itertoolz import second
# scriptStartAt = datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S")
# print('Script start at : ', scriptStartAt)
#time.sleep(3)
class Frame:
def __init__(self, position, time):
self.position = position
self.time = time
def speed(self, frame):
d = distance(*self.position, *frame.position)
time_delta = abs(frame.time - self.time)
return d / time_delta
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2 + (y2-y1)**2)
def get_current_cursor_position():
pos = QCursor.pos()
return pos.x(), pos.y()
def get_current_frame():
return Frame(get_current_cursor_position(), time.time())
#спрашиваем время каждую минуту
# def check_last_time():
# nowTime = datetime.now()
# print('Script still runing :', nowTime)
# time.sleep(60)
if __name__ == '__main__':
app = QApplication(sys.argv)
last_frame = get_current_frame()
while True:
#объясвляем время
nowTime = datetime.now()
mouseWasMoveAt = nowTime.strftime('%H:%M:%S')
new_frame = get_current_frame()
if new_frame.speed(last_frame) != 0:
print(mouseWasMoveAt)
last_frame = new_frame
time.sleep(0.07)
24.923661403031307 12:29:06
387.6390624573542 12:29:07
394.4926824122518 12:29:07
364.6319448618099 12:29:07
265.4365222073813 12:29:07
130.94569163049468 12:29:07
2.2565370492799803 12:29:09
2755.4110208927873 12:29:09
4424.753655893077 12:29:09
1333.456662360851 12:29:09
378.76671822675877 12:29:09
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question