keepcalculator

06 Numbers and Powers

Quick and Dirty Stopwatch via the Mac Terminal?

Card No. 06·28218 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

Yes, you can run a quick and dirty stopwatch directly in the Mac Terminal using the built-in time command for measuring how long a command takes, or a simple one-liner with date for a manual count-up timer. You get elapsed time in seconds (or minutes/seconds) without installing anything extra.

Formula

The calculator converts a number of seconds into hours, minutes, and seconds. The formula is:

hours = floor(seconds / 3600)
minutes = floor((seconds % 3600) / 60)
seconds = seconds % 60

Where % is the modulo operator (remainder). The output is formatted as h:mm:ss with zero-padded minutes and seconds.

Worked Example

Using the default value of 65 seconds:

  • hours = floor(65 / 3600) = 0
  • minutes = floor((65 % 3600) / 60) = floor(65 / 60) = 1
  • seconds = 65 % 60 = 5

So 65 seconds is 0:01:05 (1 minute 5 seconds).

Why Use the Terminal for a Stopwatch?

The Terminal is already on every Mac. You don’t need to download a separate app or open a widget. For quick timing—like checking how long a build script takes, timing a short task, or measuring a recipe step—a one-line command is faster than launching a GUI stopwatch.

Also, the Terminal gives you precision to the millisecond with date +%s.%N (though macOS date doesn’t support %N by default; you’d use Python or gdate after installing coreutils). For most quick uses, time is enough.

The Classic: Using time

The simplest stopwatch is the time command. Run any command with time in front, and it prints the elapsed real time, CPU time, and system time. For example:

time sleep 3

Outputs something like:

real    0m3.005s
user    0m0.001s
sys     0m0.002s

The real line is what you want—the wall-clock time. This is perfect for timing a script or a command. You don’t need to start/stop anything; it’s automatic.

Manual Count-Up with date

If you need to time something that isn’t a single command (e.g., a phone call, a physical activity), you can use date with a start and end timestamp. The trick is to use date +%s to get the current time in seconds since the Unix epoch (January 1, 1970).

Here’s a manual stopwatch in two steps:

  1. Start: START=$(date +%s) (this stores the current seconds in a variable)
  2. Later, after your task, run: echo "Elapsed: $(( $(date +%s) - START )) seconds"

This prints the difference in seconds. For a more readable format, you can convert to minutes and seconds:

echo "Elapsed: $(( ($(date +%s) - START) / 60 ))m $(( ($(date +%s) - START) % 60 ))s"

Note that this requires the same shell session. If you close the Terminal, the variable is lost. For a persistent timer, you could write the start time to a file, but that’s beyond “quick and dirty.â€�

Using time with a Shell Built-in vs. External

In bash and zsh (the default shell on macOS), time can be a reserved word (built-in) or an external binary at /usr/bin/time. The built-in gives you the nice real, user, sys output. The external version gives similar but with different formatting. For most purposes, the built-in is fine.

If you want to capture the output to a file, you can redirect stderr (since time writes to stderr):

{ time sleep 2; } 2> time.txt

That saves the timing info to time.txt.

Common Problems and Fixes

  • “command not foundâ€� for time? In some shells, time is a reserved word, so it should work. If not, use /usr/bin/time explicitly.
  • Date command doesn’t support %N (nanoseconds)? macOS’s date doesn’t have %N. You can install coreutils via Homebrew and use gdate, or use Python: python3 -c 'import time; print(time.time())'.
  • Variable not persisting? If you close the Terminal, the START variable is gone. Use a file: date +%s > /tmp/start and later echo $(( $(date +%s) - $(cat /tmp/start) )).
  • Time shows negative? That happens if you start and stop across a system clock change (e.g., NTP sync). Rare, but possible.

Alternatives: Built-in Apps and Other Tools

If you need a graphical stopwatch, macOS has a built-in Clock app (since Monterey) with a Stopwatch tab. You can also use the Timer in the same app. For more advanced timing, you could use gnomon (a command-line tool that timestamps each line of output) or ts from moreutils. But for a quick one-off, the Terminal is unbeatable.

Safety and Accuracy Notes

There are no safety concerns with using the Terminal for timing. However, be aware that time measures wall-clock time, which includes any system load or sleep. If you need precise benchmarking, you might want to disable sleep and close other apps. Also, the date +%s method is accurate to the second, not milliseconds. For sub-second precision, use Python or a dedicated tool.

Frequently Asked Questions

How do I make a stopwatch that counts up continuously in the Terminal?

You can use a loop that prints the elapsed time every second. For example: START=$(date +%s); while true; do echo -ne "$(date -u -r $(( $(date +%s) - START )) +%H:%M:%S)\r"; sleep 1; done. Press Ctrl+C to stop. This uses date -r to convert seconds to a time format.

Can I time a command that takes longer than a day?

Yes, time handles that fine. The output will show hours, minutes, seconds (e.g., real 26m3.5s for 26 hours). The manual date method also works, but the output will be in seconds; you can convert to days as needed.

What’s the difference between time and date for timing?

time is a command that measures the execution time of another command. date is used to get the current time; you manually compute the difference. Use time when you want to time a single command, use date when you need to time something that isn’t a single command (like a manual task).

How do I get milliseconds in the Terminal?

macOS’s date doesn’t support milliseconds directly. Use Python: python3 -c 'import time; print(time.time())' gives seconds with fractions. Or install coreutils and use gdate +%s.%N for nanoseconds.

Is there a way to pause and resume the stopwatch?

Not with a simple one-liner. You’d need a script that saves the accumulated time and adds the current segment. That’s beyond “quick and dirty.â€� For that, use the Clock app or a dedicated stopwatch app.

Can I use the Terminal stopwatch for cooking or workouts?

Yes, but you’ll need to keep the Terminal window open and manually read the time. For a more convenient countdown, you can use a command like sleep 300 && say "Time's up!" to get a spoken alert after 5 minutes. That’s a quick and dirty timer, not a stopwatch.

Related

Formula used
in t | Elapsed seconds | s | 65 | 0 | 86400
out hms | Time (h:mm:ss) | - | floor(t / 3600) & ":" & pad(floor((t % 3600) / 60)) & ":" & pad(t % 60) | 0
out min | Minutes | min | t / 60 | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →