ruby 中有 ISO 8601 的综合库/模块吗?

发布于 2024-10-02 05:47:26 字数 212 浏览 5 评论 0原文

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

Is there already an implementation of all the date, time, duration and interval usage of the ISO 8601 standard in ruby? I mean something like a Class where you can set and get the details like, year, month, day, day_of_the_week, week, hour, minutes, is_duration?, has_recurrence? and so on which also can be set by and exported to a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜司空 2024-10-09 05:47:26
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

查看时间。它里面有很多东西。

不幸的是,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

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

因此,我不在这里为您提供抽象示例,而是鼓励您尝试使用需要 active_supportirb

时间计算放在手边。

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

Have a look at the Time. It has a lot of stuff in it.

Unfortunately Ruby's built-in Date-Time functions do not seem to be well thought through (comparing to .NET for example), so for other functionality you will need to use some gems.

Good thing is that using those gems does feel like it's a built-into Ruby implementation.

Most useful probably is Time Calculations from ActiveSupport (Rails 3).
You don't need to require the rails but only this small library: gem install activesupport.

Then you can do:

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

There are really a lot of things that you can do and I believe you will find what suites your needs very well.

So instead of giving you abstract examples here I encourage you to experiment with irb requiring active_support from it.

Keep the Time Calculations at hand.

岁月染过的梦 2024-10-09 05:47:26

Ruby Time 库向 Time 类添加了 iso8601 方法。请参阅此处。

我不知道导出其他 ISO 8601 格式的 gem。您可以自己扩展 Time 类来添加它们。

通常,您会使用 strftime 方法来打印特定格式。 示例。

Ruby Time library adds an iso8601 method to the Time class. See here.

I don't know of a gem that exports the other ISO 8601 formats. You could extend the Time class yourself to add them.

Often you'll use the strftime method to print out specific formats. Example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文