Last Updated: February 25, 2016
·
417
· hopsoft

Parse a time string in a specific timezone

Dealing with timezones & string parsing can be a pain. Leveraging ActiveSupport makes things a bit more predictable. See if you can spot the difference.

require "active_support/all"

value = "2016-10-01"

Time.parse(value).utc
# 2016-10-01 06:00:00 UTC 

ActiveSupport::TimeZone["UTC"].parse(value).to_time.utc 
# 2016-10-01 00:00:00 UTC

This example was run on a machine using Mountain Time.