Implement a Stopwatch

Python 2, 167 129 bytes

-36 bytes mostly* from using Maltysen's idea of catching ctrl-c using an exception - go give credit!
-4 bytes thanks to DLosc (init n and b to 0 rather than f())
-1 byte thanks to FlipTack (use p^=1 rather than p=1-p)
-2 bytes thanks to Felipe Nardi Batista (remove precision specifiers)

import time
f=time.time
n=b=p=0
while 1:
 try:n=[n,f()][p];t=n-b;print'\r%02d:%02d'%(t/60%60,t%60),
 except:b=[b-n+f(),b][p];p^=1

Works the same as my original, below, but with the control key sequence of ctrl+c.
(Tested by me with Python 2.7.8 on Windows 7, 64bit;
Tested by Brian Minton with Python 2.7.13 on linux, 64bit)

* also collapsed if statement to a list lookup in order to get the try as a one-liner.

My original:

import time,msvcrt as m
f=time.time
n=b=p=0
while 1:
 if m.kbhit()and m.getch()==b'p':b=[b-n+f(),b][p];p^=1
 if p:n=f()
 t=n-b;print'\r%0.2d:%0.2d'%(t/60%60,t%60),

(Tested by me with Python 2.7.8 on Windows 7, 64bit - this code, however, is Windows specific due to the use of the msvcrt library)

The control key is 'p'.

n and b are initialised to the same value at start-up, giving an "offset" of 0; p is initialised to 0, indicating a paused state.

Whenever the control key is pressed the value of p is switched. When switching from a paused state to an active state b is updated to a new value keeping any current offset from the previous active state(s) with b-n.

During an active state n is repeatedly updated to the current time by calling time.time().

The difference between n and b, t, is then the total number of seconds (including a fractional part) elapsed during active state(s).

The minutes elapsed are then t/60 and each of the minutes and seconds are displayed mod 60 with (t/60%60,t%60). Leading zeros are prepended for each using string formatting of the integer part with '...%0.2d...'. Printing a tuple (the trailing ,) where the first item has a leading carriage return (the \r) causes the previously printed text to be overwritten.


SmileBASIC, 86 77 71 bytes

@L
N=N!=DIALOG(FORMAT$("%02D:%02D",F/60MOD 60,F MOD 60),,,N)F=F+1GOTO@L

DIALOG displays a textbox on the touch screen. N is the number of seconds the text box will stay on screen before it disappears. If N is 0, it stays until the user presses the button on the touch screen.

DIALOG Returns 1 if the user pressed the button, and 0 if it closed automatically. So when the user pushes the pause button, it returns 1, and the display time is set to 0, pausing the stopwatch. After the user presses the button again, we set the display time back to 1, resuming the timer. Basically, every time DIALOG returns 1, the display time is switched between 1 and 0 using !=, which is eqivilant to a logical XOR as long as both inputs are 1 or 0.


Python - 160 159 143 bytes

Thanks to @JonathanAllan for saving me 18 bytes!

Only uses builtin libraries, so the control key is ctrl-c, catching it with an except keyboardInterrupt.

import time
Z=0
print'00:00'
while 1:exec"try:\n while 1:\n  %s\nexcept:1\n"*2%(1,"print'\033c%02d:%02d'%divmod(Z%3600,60);Z+=1;time.sleep(1)")