#!/usr/bin/env python3 # Print world clock in terminal # (c) Copyright 2022 by Joseph Reagle # Licensed under the GPLv3, see # # 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()