Last active
July 20, 2023 19:50
-
-
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
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
# 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