🗃️ Christmas countdown source code
📆 2025-12-20 18:15
Here's a tiny Bash script that outputs a festive Christmas countdown in Gemtext.
It figures out how much time is left until Christmas and formats it nicely in months, weeks, and days - perfect for a Gemini capsule or a simple CGI endpoint.
Comments in the script should help you customize it.
#!/usr/bin/env bash
# Gemini CGI script that outputs a festive Gemtext countdown to Christmas 🎄
# --- Determine today's date and the target Christmas year ---
today_ymd=$(date +%Y%m%d)
year=$(date +%Y)
christmas_ymd="${year}1225"
# If today's date is after Dec 25, switch to next year's Christmas
if [ "$today_ymd" -gt "$christmas_ymd" ]; then
year=$((year + 1))
christmas_ymd="${year}1225"
fi
# --- Convert dates to Unix timestamps for arithmetic ---
today_sec=$(date +%s)
christmas_sec=$(date -d "${year}-12-25" +%s)
# --- Calculate remaining time in days ---
total_days=$(( (christmas_sec - today_sec) / 86400 ))
# --- Break remaining days into approximate months, weeks, and days ---
# Months are approximated as 30 days
months=$(( total_days / 30 ))
rem_days=$(( total_days % 30 ))
weeks=$(( rem_days / 7 ))
days=$(( rem_days % 7 ))
# --- Simple pluralization helper (1 day vs 2 days) ---
plural() {
[ "$1" -eq 1 ] && printf "%s" "$2" || printf "%ss" "$2"
}
# --- Build a list of non-zero time units ---
parts=()
[ "$months" -gt 0 ] && parts+=("${months} $(plural "$months" month)")
[ "$weeks" -gt 0 ] && parts+=("${weeks} $(plural "$weeks" week)")
[ "$days" -gt 0 ] && parts+=("${days} $(plural "$days" day)")
# --- Join parts into a natural-language string ---
# Examples:
# "3 months"
# "2 weeks and 1 day"
# "1 month, 2 weeks and 3 days"
if [ "${#parts[@]}" -eq 1 ]; then
time_str="${parts[0]}"
elif [ "${#parts[@]}" -eq 2 ]; then
time_str="${parts[0]} and ${parts[1]}"
else
last=${parts[-1]}
unset 'parts[-1]'
time_str="$(IFS=", "; printf "%s" "${parts[*]}") and ${last}"
fi
# --- Output Gemini response headers and Gemtext body ---
printf "20 text/gemini\r\n"
printf "# ⏳ Days Until Christmas %s 🎄\r\n" "$year"
printf "A lightweight Gemtext Christmas countdown. Months, weeks, and days until December 25th.\r\n\r\n"
printf "Today is %s.\r\n" "$(date +%Y-%m-%d)"
printf "Time remaining until Christmas %s 🎅: %s\r\n" "$year" "$time_str"
View the script in action at:
🎅 Christmas countdown
🚶 Back to Fun Stuff