Skip to content

Instantly share code, notes, and snippets.

@seven1m
Created April 2, 2025 15:55
Show Gist options
  • Save seven1m/631d9c9d223dceb93705484c7999220f to your computer and use it in GitHub Desktop.
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.
#!/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