Does Ruby have an extensive library / module for ISO 8601?

Is there already an implementation of the entire date, time, duration and interval of using the ISO 8601 standard in ruby? I mean something like a class in which you can set and receive data such as year, month, day, day_o_o_ week, week, hour, minute, is_duration ?, has_recurrence? etc. which can also be set and exported to a string?

+4
source share
2 answers
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 

Take a look at Time . It has a lot of things.

Unfortunately, the built-in functions of Date-Time Ruby do not seem to be thought out (compared to .NET, for example), so for other functions you will need to use some gems.

It’s good that using these gems really looks like a Ruby built-in implementation.

Most useful may be Time Calculation from ActiveSupport (Rails 3).
You do not need to require 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 many things you can do, and I believe that you will find something that best suits your needs.

Therefore, instead of giving you abstract examples, I recommend that you experiment with irb , requiring irb from it.

Keep your time calculations handy.

+3
source

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

I don’t know the gem that exports other ISO 8601 formats. You can extend the Time class yourself to add them.

Often you use the strftime method to print certain formats. Example.

+2
source

All Articles