I was in the office on Friday when I coworker of mine was discussing trouble he was having with restoring a DB backup. Normally I passively listen to those types of complaints, but something caught my ear about this one. He didn’t realize the company we outsource our work to runs UTC as their main way of describing time. So when he asked for a dump from 2 days ago, at 4pm he thought it was 2 days ago 4pm CST (my timezone) where the ticket for the company described 4pm UTC. Needless to say confusion started to hold.

Now this can start my rant (which up till now I haven’t actually written) about timezones, but I wanted to make his life easier so I figured out a pretty straight forward way to convert any date and time of a timezone to UTC via ruby. This is how you do it:

Note: this requires ruby 1.9.3+

[8] pry(main)> require 'time'
[8] pry(main)> t = Time.strptime("04/23/2014 02:00 AM","%m/%d/%Y %I:%M %p")
=> 2014-04-23 02:00:00 -0500
[9] pry(main)> t.utc
=> 2014-04-23 07:00:00 UTC
[10] pry(main)>

Wow, that’s pretty simple eh? :) In looking around for this answer, I came across a pretty neat trick with ruby also:

[13] pry(main)> Time.now.utc
=> 2014-04-25 16:21:27 UTC
[10] pry(main)> Time.now.to_i
=> 1398443660

That’s time now for UTC from your machines time, and the epoch time too!