Last active
May 17, 2024 04:28
-
-
Save smt/f3cdf15f9983a014427e08534740b111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Output the current time in Swatch Internet Time "beats" | |
# See: https://en.wikipedia.org/wiki/Swatch_Internet_Time | |
# Example output: | |
# @811 | |
# Helper conversion function | |
# @param naive time as HH:MM:SS string | |
# @param [-c] to include "centibeats" | |
# | |
# Reference command: | |
# TZ=UTC-1 date '+%H:%M:%S' | awk -F: '{printf("@%03d\n", ($3 + ($2 * 60) + ($1 * 3600)) / 86.4)}' | |
# | |
# Example usage: | |
# to_beat 18:45:38 | |
# @781 | |
# | |
# to_beat 18:45:38 -c | |
# @781.69 | |
# | |
to_beat(){ | |
local format='' | |
case "$2" in | |
-c) format="@%06.2f\n";; | |
*) format="@%03d\n";; | |
esac | |
echo "$1" | awk -F: \ | |
"{printf(\"$format\", (\$3 + (\$2 * 60) + (\$1 * 3600)) / 86.4)}" | |
} | |
# For backcompat, use UTC+1 with env var for current time, which does not | |
# observe daylight adjustments. This is unfortunately expressed as 'UTC-1'. | |
main(){ | |
to_beat $(TZ=UTC-1 date '+%H:%M:%S') | |
} | |
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Output the difference between two Unix timestamps in days | |
# | |
# Accepts the following arguments: | |
# - start timestamp (default: unix epoch) | |
# - end timestamp (default: current time) | |
# - output base (default: 10) | |
# - output precision (default: 0) | |
# | |
# Example output: | |
# $ days_since.sh | |
# 17444 | |
# | |
# $ days_since.sh 247881600 $(TZ=CET date '+%s') 16 3 | |
# 38EF.BBE | |
# | |
# TODO: convert arg usage to flags so they can be parsed by getopt | |
# TODO: generalise to convert to other units of time (years, months, weeks, H:M:S, etc) | |
days() { | |
local SECONDS=86400 | |
dc -e "10i $4 k $2 $1 - $SECONDS / $3o p" | |
} | |
main() { | |
local start=${1:-0} | |
local end=${2:-0} | |
local base=${3:-10} | |
local precision=${4:-0} | |
[ $end == 0 ] && end=`date '+%s'` | |
days $start $end $base $precision | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment