Skip to content

Time Calculation Inaccuracy (Math) #53

@annrose2277-glitch

Description

@annrose2277-glitch

Describe the bug
f the timer hits 0, the update_timer function calls check_result. However, check_result recalculates the time using time.time(). Due to CPU latency and the 1-second intervals of after(), the "elapsed time" might show as 60.1 or 61 seconds instead of exactly 60.

To Reproduce
Steps to reproduce the behavior:
In your init method, ensure you have a variable for the max time:

self.test_duration = 60 # Default to 60

Update the test_duration variable whenever the user clicks Start.

Inside start_test(self):

duration = 60 if self.mode == "60s" else 15
self.test_duration = duration # Store this for the final math
self.time_left = duration

Modify your check_result function like this:

def check_result(self, event=None):
if not self.start_time:
return

self.timer_running = False
self.pause_button.configure(state="disabled")

# --- THE FIX ---
if self.time_left <= 0:
    # If time actually ran out, use the PERFECT duration (e.g., 60.0)
    elapsed_time = self.test_duration
else:
    # If the user stopped early, calculate the actual time they spent
    elapsed_time = (time.time() - self.start_time) - self.total_paused_time

# Ensure we don't divide by zero if someone stops at 0.001 seconds
elapsed_time = max(elapsed_time, 1) 
# --- END FIX ---

typed_text = self.input_textbox.get("1.0", "end-1c")
chars_typed = self.total_chars_accumulated + len(typed_text)

# WPM Math: (chars/5) / (seconds/60)
wpm = (chars_typed / 5) / (elapsed_time / 60)

self.result_label.configure(text=f"Test Finished! Speed: {wpm:.2f} WPM")

Expected behavior
By "hard-coding" the elapsed_time to test_duration (e.g., 60) when the clock hits zero, eliminate the CPU jitter.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions