Skip to content

Instantly share code, notes, and snippets.

@reagle
Created October 12, 2022 12:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reagle/499355b362bb43cf73285c2fc0f78f5a to your computer and use it in GitHub Desktop.
Save reagle/499355b362bb43cf73285c2fc0f78f5a to your computer and use it in GitHub Desktop.
Print world clock in terminal
#!/usr/bin/env python3
# Print world clock in terminal
# (c) Copyright 2022 by Joseph Reagle
# Licensed under the GPLv3, see <http://www.gnu.org/licenses/gpl-3.0.html>
#
# replacing: https://superuser.com/questions/164339/timezone-conversion-by-command-line
import argparse # http://docs.python.org/dev/library/argparse.html
import sys
import pendulum # https://pendulum.eustace.io/docs/
my_tz = "America/New_York"
my_start = pendulum.parse("8:00", tz=my_tz) # start of day in 24 HH
my_end = pendulum.parse("18:00", tz=my_tz) # end of day in 24 HH
arg_parser = argparse.ArgumentParser(description="Print world clock in terminal")
arg_parser.add_argument("time", nargs="?", metavar="TIME", help="target time to show")
args = arg_parser.parse_args(sys.argv[1:])
if args.time:
args.time = pendulum.parse(args.time, tz=my_tz)
else:
args.time = pendulum.now(my_tz)
# zones of interest
target_zones = [
"Asia/Calcutta",
"Etc/UTC",
"Europe/London",
"America/New_York",
"America/Chicago",
"America/Denver",
"America/Los_Angeles",
]
for zone in target_zones:
zone_time = args.time.in_timezone(zone)
highlight = "\033[4m" if zone == my_tz else "\033[0m"
print(
f"{highlight}{zone:<20} {zone_time.strftime('%b-%d')}"
+ f" {zone_time.strftime('%H:%M%p | %z')}",
end=" || ",
)
counter = my_start
while counter <= my_end:
counter_in_tz = counter.in_timezone(zone)
print(f"{counter_in_tz.format('HH')} ", end="")
counter = counter.add(hours=1)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment