Categories We Write About

Build a timer that logs interruptions

To build a timer that logs interruptions, you can use a combination of a basic timer functionality and a logging system. Here’s how you could approach it:

Requirements:

  1. Timer Functionality: You need a countdown timer.

  2. Interrupt Logging: When the timer is interrupted (e.g., paused or stopped), log the interruption with the timestamp and any relevant details.

This can be implemented in various programming languages, but I’ll show you an example in Python.

Python Implementation

python
import time import logging # Setup logging to record interruptions logging.basicConfig(filename="interruptions.log", level=logging.INFO, format="%(asctime)s - %(message)s") class Timer: def __init__(self, duration): """Initialize the timer with the given duration (in seconds).""" self.duration = duration self.start_time = None self.paused_time = None self.is_running = False self.is_paused = False def start(self): """Start the timer.""" if not self.is_running: self.start_time = time.time() self.is_running = True self.is_paused = False print(f"Timer started for {self.duration} seconds.") else: print("Timer is already running.") def pause(self): """Pause the timer and log the interruption.""" if self.is_running and not self.is_paused: self.paused_time = time.time() self.is_paused = True elapsed_time = self.paused_time - self.start_time logging.info(f"Timer paused at {elapsed_time:.2f} seconds.") print(f"Timer paused at {elapsed_time:.2f} seconds.") else: print("Timer is not running or already paused.") def resume(self): """Resume the timer if it was paused.""" if self.is_paused: time_elapsed = time.time() - self.paused_time self.start_time += time_elapsed self.is_paused = False print(f"Timer resumed.") else: print("Timer is not paused.") def stop(self): """Stop the timer and log the interruption.""" if self.is_running: elapsed_time = time.time() - self.start_time logging.info(f"Timer stopped at {elapsed_time:.2f} seconds.") print(f"Timer stopped at {elapsed_time:.2f} seconds.") self.is_running = False else: print("Timer is not running.") def get_time_left(self): """Get the remaining time on the timer.""" if self.is_running and not self.is_paused: elapsed_time = time.time() - self.start_time remaining_time = self.duration - elapsed_time return remaining_time if remaining_time > 0 else 0 elif self.is_paused: elapsed_time = self.paused_time - self.start_time remaining_time = self.duration - elapsed_time return remaining_time if remaining_time > 0 else 0 else: return self.duration # Create a timer object for 10 seconds timer = Timer(10) # Start, pause, resume, and stop the timer to simulate interruptions timer.start() time.sleep(3) # Wait for 3 seconds timer.pause() time.sleep(2) # Wait for 2 seconds while paused timer.resume() time.sleep(4) # Wait for 4 seconds timer.stop() # After this, the "interruptions.log" file will contain the logged interruptions.

Explanation:

  • Timer Class: The Timer class has methods for starting, pausing, resuming, and stopping the timer. The class also keeps track of the start time, paused time, and running state.

  • Logging: The logging module records interruptions (pause and stop) to a file (interruptions.log). Each log entry includes a timestamp and the time at which the interruption occurred.

  • Start, Pause, Resume, and Stop:

    • Start: Starts the timer and records the start time.

    • Pause: Pauses the timer and logs the interruption.

    • Resume: Resumes the timer from where it was paused.

    • Stop: Stops the timer and logs the time at which it was stopped.

Sample interruptions.log File:

yaml
2025-05-18 12:34:56,789 - Timer paused at 3.00 seconds. 2025-05-18 12:35:00,123 - Timer stopped at 7.00 seconds.

This solution allows for tracking interruptions and resuming the timer without losing the time that has already passed.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About