Rails ActiveSupport 时间解析? [英] Rails ActiveSupport Time Parsing?

查看:15
本文介绍了Rails ActiveSupport 时间解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 的 ActiveSupport 模块扩展了内置的 ruby​​ Time 类方法.

值得注意的是,有 to_formatted_s 方法,它可以让您编写 Time.now.to_formatted_s(:db) 以获取数据库格式的字符串,而不必到处写丑陋的 strftime 格式字符串.

Notably, there is the to_formatted_s method, which lets you write Time.now.to_formatted_s(:db) to get a string in Database format, rather than having to write ugly strftime format-strings everywhere.

我的问题是,有没有办法倒退?

My question is, is there a way to go backwards?

类似于 Time.parse_formatted_s(:db) 的东西,它会解析数据库格式的字符串,返回一个新的 Time 对象.这似乎是 Rails 应该提供的东西,但如果是,我找不到.

Something like Time.parse_formatted_s(:db) which would parse a string in Database format, returning a new Time object. This seems like something that rails should be providing, but if it is, I can't find it.

是我找不到,还是需要自己写?

Am I just not able to find it, or do I need to write it myself?

谢谢

推荐答案

看起来 ActiveSupport 确实提供了您正在寻找的解析方法(我也一直在寻找),毕竟!—至少如果您尝试解析的字符串是标准的 ISO-8601 格式(:db 格式)日期.

It looks like ActiveSupport does provide the parsing methods you are looking for (and I was looking for too), after all! — at least if the string you are trying to parse is a standard, ISO-8601-formatted (:db format) date.

如果您尝试解析的日期已经在您的本地时区,那真的很简单!

If the date you're trying to parse is already in your local time zone, it's really easy!

 > Time.zone.parse('2009-09-24 08:28:43')
=> Thu, 24 Sep 2009 08:28:43 PDT -07:00

 > Time.zone.parse('2009-09-24 08:28:43').class
=> ActiveSupport::TimeWithZone

然后可以轻松地将时区感知时间转换为 UTC

and that time-zone-aware time can then easily be converted to UTC

 > Time.zone.parse('2009-09-24 08:28:43').utc
=> 2009-09-24 15:28:43 UTC

或其他时区:

 > ActiveSupport::TimeZone.us_zones.map(&:name)
=> ["Hawaii", "Alaska", "Pacific Time (US & Canada)", "Arizona", "Mountain Time (US & Canada)", "Central Time (US & Canada)", "Eastern Time (US & Canada)", "Indiana (East)"]

 > Time.zone.parse('2009-09-24 08:28:43').utc.in_time_zone('Eastern Time (US & Canada)')
=> Thu, 24 Sep 2009 11:28:43 EDT -04:00

另一方面,如果您尝试解析的日期字符串是 UTC,则看起来没有任何方法可以将它直接解析为 TimeWithZone,但我能够要解决这个问题,首先使用 DateTime.strptime...

If the date string you're trying to parse is in UTC, on the other hand, it doesn't look like there's any method to parse it directly into a TimeWithZone, but I was able to work around that be first using DateTime.strptime...

如果您尝试解析的日期是 UTC 并且您希望它保持为 UTC,您可以使用:

If the date you're trying to parse is in UTC and you want it to stay as UTC, you can use:

 > DateTime.strptime('2009-09-24 08:28:43', '%Y-%m-%d %H:%M:%S').to_time
=> 2009-09-24 08:28:43 UTC

如果您尝试解析的日期采用 UTC 格式,并且您希望将其转换为默认时区,则可以使用:

If the date you're trying to parse is in UTC and you want it converted to your default time zone, you can use:

 > DateTime.strptime('2009-09-24 08:28:43', '%Y-%m-%d %H:%M:%S').to_time.in_time_zone
=> Thu, 24 Sep 2009 01:28:43 PDT -07:00

看起来它甚至可以解析其他格式,比如 Time#to_s 产生的奇怪格式:

It looks like it can even parse other formats, such as the strange format that Time#to_s produces:

irb -> Time.zone.parse('Wed, 23 Sep 2009 02:18:08').to_s(:db)
    => "2009-09-23 09:18:08"

irb -> Time.zone.parse('Wed, 23 Sep 2009 02:18:08 EDT').to_s(:db)
    => "2009-09-23 06:18:08"

我印象非常深刻.

这里有更多来自 [http://api.rubyonrails 的示例.org/classes/ActiveSupport/TimeWithZone.html][1]:

  Time.zone = 'Eastern Time (US & Canada)'        # => 'Eastern Time (US & Canada)'
  Time.zone.local(2007, 2, 10, 15, 30, 45)        # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.parse('2007-02-10 15:30:45')          # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.at(1170361845)                        # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.now                                   # => Sun, 18 May 2008 13:07:55 EDT -04:00
  Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone  # => Sat, 10 Feb 2007 15:30:45 EST -05:00

更多文档链接供参考:

这篇关于Rails ActiveSupport 时间解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆