Skip to content

Code examples in various languages which illustrate how to correctly calculate Swatch Internet Time beats

License

Notifications You must be signed in to change notification settings

swatchtime/sample-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swatch Time: Sample Code

This repository contains single-file examples that compute Swatch Internet Time ("beats") using the canonical definition used by the SwatchTime organization.

Canonical rules

  • Reference zone: Biel Mean Time (BMT) = UTC+1 (fixed offset, no DST)
  • One beat = 86.4 seconds (86400 / 1000)
  • Beats run from @000 at Biel midnight (00:00:00 UTC+1) through @999 just before the next Biel midnight

Computation (pseudocode)

// JavaScript-style pseudocode
now = new Date()
utcSeconds = now.getUTCHours() * 3600 + now.getUTCMinutes() * 60 + now.getUTCSeconds()
bielSeconds = (utcSeconds + 3600 + 24*3600) % (24*3600)
beat = Math.floor(bielSeconds / 86.4) % 1000
display = `@${String(beat).padStart(3,'0')}`

Quick test vectors

  • 2025-01-01T00:00:00Z → Biel 01:00:00 → @041
  • 2025-01-01T23:00:00Z → Biel 00:00:00 (next day) → @000
  • 2025-06-01T12:34:56Z → Biel 13:34:56 → @565

Files

  • bash/get_swatch_time.sh — Bash example. Run with bash bash/get_swatch_time.sh.
  • c/get_swatch_time.c — C single-file example. Build: gcc -std=c11 c/get_swatch_time.c -o get_swatch_time -lm.
  • cpp/get_swatch_time.cpp — C++ single-file example. Build: g++ -std=c++11 cpp/get_swatch_time.cpp -o get_swatch_time.
  • csharp/get_swatch_time.cs — C# example. Run with dotnet script or compile as a console app.
  • elixir/get_swatch_time.exs — Elixir example. Run with elixir elixir/get_swatch_time.exs.
  • go/get_swatch_time.go — Go single-file example. Build or run with go run go/get_swatch_time.go.
  • haskell/get_swatch_time.hs — Haskell example. Run with runhaskell haskell/get_swatch_time.hs or compile with ghc.
  • java/GetSwatchTime.java — Java example. Build/run with javac java/GetSwatchTime.java && java -cp java GetSwatchTime.
  • js/getSwatchTime.js — JavaScript single-file example. Run with node js/getSwatchTime.js or open in browser console.
  • kotlin/get_swatch_time.kt — Kotlin example. Run as script with kotlinc -script kotlin/get_swatch_time.kt or compile with kotlinc.
  • lua/get_swatch_time.lua — Lua example. Run with lua lua/get_swatch_time.lua.
  • php/get_swatch_time.php — PHP example. Run with php php/get_swatch_time.php.
  • powershell/get_swatch_time.ps1 — Windows PowerShell example. Run in PowerShell: powershell -File powershell/get_swatch_time.ps1.
  • python/get_swatch_time.py — Python 3 single-file example. Run with python3 python/get_swatch_time.py.
  • ruby/get_swatch_time.rb — Ruby example. Run with ruby ruby/get_swatch_time.rb.
  • rust/get_swatch_time.rs — Rust single-file example that uses SystemTime. Compile with rustc rust/get_swatch_time.rs && ./get_swatch_time or run with cargo if you prefer.
  • swift/get_swatch_time.swift — Swift example. Run with swift swift/get_swatch_time.swift.

Usage

Each file prints the current Swatch beat to stdout/console. Some files accept an ISO timestamp or include example test lines you can uncomment to test known vectors.

Note: Rounding and display (important)

When displaying centibeats (two decimal places), implementations that round the fractional beats to 2 decimals before wrapping can briefly produce a value of 1000.00 (for example when the raw value is 999.995). To avoid this transient display, round first then wrap values >= 1000 back into range (subtract 1000 or set to 0) before formatting for display.

Canonical JavaScript snippet each implementation should follow:

const rawBeats = bielSeconds / 86.4;
let rounded = Math.round(rawBeats * 100) / 100; // 2 decimals
if (rounded >= 1000) rounded = rounded - 1000;   // wrap 1000 -> 0
const display = rounded.toFixed(2);

This preserves human-friendly rounding while ensuring the UI never shows 1000.00 briefly.

License

MIT

About

Code examples in various languages which illustrate how to correctly calculate Swatch Internet Time beats

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published