在ruby中是否有针对ISO 8601的综合库/模块?

dSt*_*lle 6 ruby recurrence datetime duration iso8601

是否已经实现了红宝石中ISO 8601标准的所有日期,时间,持续时间和间隔使用?我的意思是类似于一个类,你可以在其中设置和获取详细信息,如年,月,日,day_of_the_week,周,小时,分钟,is_duration?,has_recurrence?等等也可以设置并输出到字符串?

Dmy*_*iak 3

require 'time'

time = Time.iso8601 Time.now.iso8601 # iso8601 <--> string
time.year    # => Year of the date 
time.month   # => Month of the date (1 to 12)
time.day     # => Day of the date (1 to 31 )
time.wday    # => 0: Day of week: 0 is Sunday
time.yday    # => 365: Day of year
time.hour    # => 23: 24-hour clock
time.min     # => 59
time.sec     # => 59
time.usec    # => 999999: microseconds
time.zone    # => "UTC": timezone name
Run Code Online (Sandbox Code Playgroud)

看看《时代》杂志。它里面有很多东西。

不幸的是,Ruby 的内置日期时间函数似乎没有经过深思熟虑(例如与 .NET 相比),因此对于其他功能,您将需要使用一些 gem。

好的一点是,使用这些 gem 确实感觉像是内置的 Ruby 实现。

最有用的可能是来自 ActiveSupport (Rails 3) 的时间计算。
您不需要 Rails,只需要这个小库:gem install activesupport.

然后你可以这样做

require 'active_support/all'
Time.now.advance(:hours => 1) - Time.now # ~ 3600
1.hour.from_now - Time.now # ~ 3600 - same as above
Time.now.at_beginning_of_day # ~ 2010-11-24 00:00:00 +1100
# also at_beginning_of_xxx: xx in [day, month, quarter, year, week]
# same applies to at_end_of_xxx
Run Code Online (Sandbox Code Playgroud)

您可以做的事情确实有很多,我相信您会找到非常适合您需求的事情。

因此,我不在这里给你抽象的例子,而是鼓励你尝试从中提出irb要求active_support

时间计算放在手边。