rubytimezone

How do I get Ruby to parse time as if it were in a different time zone?


I'm parsing something like this:

11/23/10 23:29:57

which has no time zone associated with it, but I know it's in the UTC time zone (while I'm not). How can I get Ruby to parse this as if it were in the UTC timezone?


Solution

  • You could just append the UTC timezone name to the string before parsing it:

    require 'time'
    s = "11/23/10 23:29:57"
    Time.parse(s) # => Tue Nov 23 23:29:57 -0800 2010
    s += " UTC"
    Time.parse(s) # => Tue Nov 23 23:29:57 UTC 2010