Last active
May 19, 2025 00:32
-
-
Save yeiichi/1ce3fde68ebde4a7fb0344f41d59999b to your computer and use it in GitHub Desktop.
Echo how many calendar days have passed between the REFERENCE_DATE and today.
This file contains hidden or 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 sh | |
| # Echo how many calendar days have passed between the REFERENCE_DATE and today. | |
| readonly REFERENCE_DATE='2025-01-01' | |
| readonly SECONDS_PER_DAY=86400 | |
| error_exit() { | |
| echo "$1" >&2 | |
| exit "${2:-1}" | |
| } | |
| get_epoch() { | |
| date_str="$1" | |
| case "$(uname)" in | |
| Darwin) | |
| date -jf "%Y-%m-%d" "$date_str" +%s 2>/dev/null | |
| ;; | |
| Linux) | |
| date -d "$date_str" +%s 2>/dev/null | |
| ;; | |
| *) | |
| error_exit "Unsupported OS: $(uname)" | |
| ;; | |
| esac | |
| } | |
| today="$(date '+%Y-%m-%d')" | |
| sec_ref=$(get_epoch "$REFERENCE_DATE") || error_exit "Failed to parse reference date" | |
| sec_now=$(get_epoch "$today") || error_exit "Failed to parse current date" | |
| day_diff=$(((sec_now - sec_ref) / SECONDS_PER_DAY)) | |
| echo "$day_diff day(s) since $REFERENCE_DATE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment