Reputation: 1837
puts "date --- #{date}"
@date = Time.parse(date.to_s).iso8601 unless date.nil?
puts "@date -- #{@date}"
Outputs
Date --- 2012-08-12T12:15:17-07:00
@Date -- 2012-08-12T19:15:17+00:00
Anyone know why?
Additionally, this happens with strptime
Time.strptime("2012-08-12T12:05:08-07:00", "%Y-%m-%dT%H:%M:%S%:z")
=> 2012-08-12 19:05:08 +0000
Upvotes: 0
Views: 418
Reputation: 34031
It appears that your system is set to UTC. Time.parse()
creates a new Time object, which uses the system timezone, and sets it to the time that is parsed. It doesn't change the timezone of the new Time to match the timezone of the parsed date. If you really want that behavior, you can use something like:
DateTime.parse(date.to_s).new_offset(date.iso8601[-6,6]).iso8601
Update: Regarding the strptime()
part of the question that was just added, it's the exact same concept. A new Time is being created with the default timezone, with a time that matches the date that you're parsing.
Upvotes: 1