Why would you need a command line time zone conversion?
Imagine that you’re troubleshooting a problem. Add that your team is distributed across multiple timezones. Now add that database times are generally recorded in UTC, but your alerting all displays in local time.
If you’re trying to pinpoint when an event happened and comparing notes across systems and observations, you’re going to be doing some time zone conversion to validate that all the observations are related to the same time period. That can be a bit tedious, especially if you’re already juggling a lot of details about the problem. For me, I wrote a quick script to be a time converter calculator to as a guide to help me align time stamps from different sources.
Parsing the time
Once require 'time'
is added to the script, Time.parse(time_string)
will work. But I also wanted to support if you had a date or time offset, such as 2022-04-20 11:09 -0500
without having to put the argument in quotes, so I joined the arguments:
# support calling as "convert_time 2022-04-20 11:09 -0500"
arg_time=ARGV.join(' ')
the_time=Time.parse(arg_time)
The resultant parsed time will be interpreted as a local time unless a time zone offset was specified.
Making things pretty
I’m using the colorize
gem for coloring the output. Note: Windows Terminal or similar is required for full color output.
Converting timezones
(tzinfo-data
gem is required for Windows or else you’ll end up with a TZInfo::DataSourceNotFound
)
I’m using the tzinfo
gem to convert times to specified times by grabbing a TZInfo::Timezone
and calling to_local
tz=TZInfo::Timezone.get(v)
string="#{k}: => #{tz.to_local(the_time)}"
Presentation
I look through a hash of all of my “locations” (keys) and respective time zone representations (values) in time_zone_hash
and present times for each as a time zone chart:

The Time Zone Conversion Code
Next Steps
I just had this in a script to call when comparing timestamps, but it could be bundled and the hash itself could be in a configuration or yaml file to clean up the time zone conversion.