Created
April 2, 2025 15:55
-
-
Save seven1m/631d9c9d223dceb93705484c7999220f to your computer and use it in GitHub Desktop.
A little Ruby script that prints converts the given time to the three timezones I care about.
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/env ruby | |
# Usage: timey [timestamp] | |
# | |
# Examples: | |
# | |
# → timey | |
# UTC: 2025-04-02 15:50:58 UTC | |
# CDT: 2025-04-02 10:50:58 -0500 | |
# PDT: 2025-04-02 08:50:58 -0700 | |
# | |
# → timey '2025-04-02 15:52:02 UTC' | |
# UTC: 2025-04-02 15:52:02 UTC | |
# CDT: 2025-04-02 10:52:02 -0500 | |
# PDT: 2025-04-02 08:52:02 -0700 | |
# | |
# → timey '2025-04-02T15:52:02Z' | |
# UTC: 2025-04-02 15:52:02 UTC | |
# CDT: 2025-04-02 10:52:02 -0500 | |
# PDT: 2025-04-02 08:52:02 -0700 | |
# | |
# → timey '10:52' | |
# UTC: 2025-04-02 15:52:00 UTC | |
# CDT: 2025-04-02 10:52:00 -0500 | |
# PDT: 2025-04-02 08:52:00 -0700 | |
# | |
require 'time' | |
timestamp = ARGV.first || Time.now.to_s | |
if timestamp.end_with?('Z') || timestamp.end_with?('UTC') | |
utc_time = Time.parse(timestamp) | |
local = utc_time.getlocal | |
dst = utc_time.getlocal.dst? | |
else | |
local = Time.parse(timestamp) | |
utc_time = local.dup.utc | |
dst = local.dst? | |
end | |
puts "UTC: #{utc_time}" | |
puts "#{local.zone}: #{local}" | |
ca_offset = dst ? -7 : -8 | |
ca_time = utc_time.getlocal("%+03d:00" % ca_offset) | |
puts "#{dst ? 'PDT' : 'PST'}: #{ca_time}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment