Skip to content

Instantly share code, notes, and snippets.

@maptv
Last active July 20, 2023 19:50
Show Gist options
  • Save maptv/4e056422b490c38140c4259421527cef to your computer and use it in GitHub Desktop.
Save maptv/4e056422b490c38140c4259421527cef to your computer and use it in GitHub Desktop.
Display deciday time with deciday offset and 24-hour time with UTC offset, e.g. 5.50-2 and 13:00-05
# test by running the following in a UNIX shell:
# for tz in 'Pacific/Samoa' 'America/Los_Angeles' 'Etc/UTC' 'Asia/Shanghai' 'Pacific/Kiritimati'; do; echo \\n$tz; env TZ=$tz python deciday.py; done;
from datetime import datetime, timezone
import time
utc = datetime.now(timezone.utc)
mid = utc.replace(hour=0, minute=0, second=0, microsecond=0)
seconds_since_midnight = (utc - mid).total_seconds()
deciday = seconds_since_midnight / 8640
offset = round(time.timezone / 8640)
sign = "-" if offset > 0 else "+"
offset_deciday = deciday - offset
normalized_deciday = offset_deciday - 10 * (offset_deciday > 10) + 10 * (offset_deciday < 0)
print(f"{datetime.now().strftime('%H:%M')}{sign}{str(int(abs(time.timezone) / 3600)).zfill(2)}")
print(f" {normalized_deciday:.2f}{sign}{abs(offset)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment