Created
March 25, 2024 22:24
-
-
Save a5ync/7de0055b7d541fbd02ab2d868a7c0cdd to your computer and use it in GitHub Desktop.
Python time converter
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
| #!/usr/bin/python3 | |
| from datetime import datetime, timezone | |
| from zoneinfo import ZoneInfo | |
| import sys | |
| def convert_timestamp(input_timestamp): | |
| # Determine if the timestamp is in milliseconds or seconds based on its length | |
| # Assuming it's in milliseconds if it's greater than 10 digits (a common convention) | |
| if len(input_timestamp) > 10: | |
| timestamp_seconds = int(input_timestamp) / 1000 | |
| else: | |
| timestamp_seconds = int(input_timestamp) | |
| # Create a UTC datetime object from the timestamp | |
| utc_datetime = datetime.fromtimestamp(timestamp_seconds, tz=timezone.utc) | |
| # Define the time zones to convert to | |
| time_zones = { | |
| "UTC": timezone.utc, | |
| "HST": ZoneInfo('Pacific/Honolulu'), # Hawaii Standard Time | |
| "Pacific": ZoneInfo('America/Los_Angeles'), # Pacific Time (supports DST) | |
| "EST": ZoneInfo('America/New_York'), # Eastern Standard Time (supports DST) | |
| } | |
| # Convert and print the datetime in each time zone | |
| for zone_name, zone in time_zones.items(): | |
| local_datetime = utc_datetime.astimezone(zone) | |
| print(f"{zone_name}: {local_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z')}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Usage: script.py <timestamp>") | |
| sys.exit(1) | |
| print("p's time converter") | |
| convert_timestamp(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment