Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active March 24, 2024 13:32
Show Gist options
  • Save ruario/9c7722b5d09abfd4b6e997384f12b31b to your computer and use it in GitHub Desktop.
Save ruario/9c7722b5d09abfd4b6e997384f12b31b to your computer and use it in GitHub Desktop.
Swatch .beats to centibeat level from the terminal

To print the time in Swatch .beats from the terminal, issue the following:

printf @;TZ=UTC-1 date '+scale=2;((%H*60+%M)*60+%S)/86.4'|bc

You can save this as a shell alias so that you can check the time in .beats whenever you like alias beat="printf @;TZ=UTC-1 date '+scale=2;((%H*60+%M)*60+%S)/86.4'|bc".

Notes:

  • POSIX time offsets are specificed in reverse. So this is actually "UTC+1", the same as "Biel Mean Time".
  • "centibeats" (.beats to two decimal places) are not official but commonly displayed on third party implementations.

There is some rounding going on above so the exact centibeat might be a fraction off. On Linux you could add .%N after %S to get better precision. On macOS and BSD this shell script that calls python could be better

#!/bin/sh
a=`TZ=UTC-1 python3 -c "from datetime import datetime; print(datetime.now().strftime('%H:%M:%S.%f'))"`;b=${a#*:}
printf @;echo "scale=2;((${a%%:*}*60+${b%:*})*60+${b#*:})/86.4"|bc

And yes… you could just do the entire thing in python. That be an exercise for the reader. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment