Rails: How to parse date-time string into a specific time zone

前端 未结 7 1221
天命终不由人
天命终不由人 2020-12-01 10:23

I\'m using Rails 3.2 and ruby 1.9.3 on Debian. I have an app that collects a date, time, and timezone in the form of strings via an HTML form. Something like this:



        
相关标签:
7条回答
  • 2020-12-01 10:50

    Use String#in_time_zone (Rails 4+)

    I personally prefer using String#in_time_zone:

    >> '22.09.1986 10:30'.in_time_zone('Central Time (US & Canada)')
    # => Mon, 22 Sep 1986 10:30:00 CDT -05:00
    

    This parses the date and time in the String into the time zone provided.

    0 讨论(0)
  • 2020-12-01 10:53

    I've finally found the dirty, yet definitive way to do this.

    First, parse the string using plain Ruby Time.strptime like this:

    time = Time.strptime('12 : 00 : PM', '%I : %M : %p')
    

    This way you get the parsed Time, but not yet in correct timezone. To fix that, let's convert the time to string form and parse it with the standard ActiveSupport::TimeZone#parse

    Time.zone.parse(time.to_s)
    

    The result is the ActiveSupport::TimeWithZone with our time parsed into the correct timezone.

    The reason why we have to do it this way is that neither ActiveSupport::TimeZone nor ActiveSupport::TimeWithZone support the strptime method. So we have to parse the Time with core Ruby strptime that does not have timezone information, convert it to format acceptable in ActiveSupport objects and then parse it yet again.

    0 讨论(0)
  • 2020-12-01 10:55

    This is the method that I came up with. Not the prettiest, but it works. Allows parsing the string using a specified format, and then turning it into the format that I know Time.zone.parse requires.

    class ActiveSupport::TimeZone
      def strptime(time, format='%m/%d/%Y')
        formatted = Time.strptime(time, format).strftime('%Y-%m-%d %T')
        parse(formatted)
      end
    end
    

    Then you can do something like what was mentioned in another question, but with a specified format:

    zone = "Central Time (US & Canada)"  
    ActiveSupport::TimeZone[zone].strptime('2013-04-03', '%Y-%m-%d')
    

    Or if you already have a time zone set:

    Time.zone = "Central Time (US & Canada)" 
    Time.zone.strptime('01/13/2006')
    

    I used a default format of %m/%d/%Y because that's what my user input is most of the time. You can customize this to your needs, or use the default format DateTime uses which is believe is iso8601 (%FT%T%z)

    0 讨论(0)
  • 2020-12-01 11:00

    Convert specific date format in UTC.

    ActiveSupport::TimeZone['UTC'].parse(Time.strptime('01/24/2019T16:10:16', "%m/%d/%YT%H:%M:%S").asctime)
    
    0 讨论(0)
  • 2020-12-01 11:02

    Try this:

    zone = "Central Time (US & Canada)"  
    
    ActiveSupport::TimeZone[zone].parse("2013-04-03 17:47:00")
    
    0 讨论(0)
  • 2020-12-01 11:06

    To have DateTime take the date string and attach a timezone other than UTC without changing the values of the date string , use this, its easy , doesnt break on leap day :)

    xx = DateTime.strptime("9/1/15 #{object.time_zone}", "%m/%d/%Y %Z")
    
    0 讨论(0)
提交回复
热议问题