Skip to content

Instantly share code, notes, and snippets.

@wlinds
Created July 2, 2024 21:27
Show Gist options
  • Save wlinds/2aabd579064e6ea138ad1d8d846428b5 to your computer and use it in GitHub Desktop.
Save wlinds/2aabd579064e6ea138ad1d8d846428b5 to your computer and use it in GitHub Desktop.
Easy timestamp creation
import os, keyboard, time, json, threading
def format(elapsed):
ms = int((elapsed * 1000) % 1000)
seconds = int(elapsed % 60)
minutes = int((elapsed // 60) % 60)
hours = int((elapsed // 3600) % 24)
return f"{hours:02}:{minutes:02}:{seconds:02}.{ms:03}"
def record():
elapsed = time.time() - start_time
timestamp = format(elapsed)
print(f"\nTimestamp recorded: {timestamp}")
timestamps.append(timestamp)
save()
def save():
with open(FILE_NAME, 'w') as f:
json.dump(timestamps, f, indent=4)
def display_time():
while not stop_event.is_set():
elapsed = time.time() - start_time
current_time = format(elapsed)
print(f"\rCurrent Time: {current_time} - Press 'space' to record timestamp, 'esc' to exit.", end='')
time.sleep(0.01)
if __name__ == "__main__":
FILE_NAME = 'timestamps.json'
start_time = time.time()
timestamps = []
if os.path.exists(FILE_NAME):
with open(FILE_NAME, 'r') as f:
timestamps = json.load(f)
stop_event = threading.Event()
keyboard.add_hotkey('space', record)
display_thread = threading.Thread(target=display_time)
display_thread.start()
keyboard.wait('esc')
stop_event.set()
display_thread.join()
save()
print("\nTimestamps saved.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment