Question

I have a string that represents a date time. For example:

"2013-10-29T08:30:00"

that does not have time zone information, but I know that it represents a time in specific time zone, e.g. "Central Time (US & Canada)"

I want to create a correct DateTime object with correct time zone information, but I fail to do that, because I do not know whether I should use "-06:00" or "-05:00" as an offset (due to DST).

Any help?

Was it helpful?

Solution

No worries about DST at all :) ActiveSupport will take care of that for you.

Time.zone = "Central Time (US & Canada)"

t = Time.zone.parse "2013-10-29T08:30:00"
#=> Tue, 29 Oct 2013 08:30:00 CDT -05:00

t.dst?
#=> true

Ref about time zone setting and dst? http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

Update: The above block is for showing purpose. In app, you can set time zone at config/application.rb

config.time_zone = 'Central Time (US & Canada)'

This will set default Time.zone so you no longer need the first line of above code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top