Is there a comprehensive library/module for ISO 8601 in ruby? - ruby

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?

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.

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.

Related

Time.new and Date.new .strftime("%A") returns different day for the same parameters

Solving Unlucky Days, I encountered, that:
require 'date'
Time.new(1001,1,1).strftime("%A") # => Thursday
Date.new(1001,1,1).strftime("%A") # => Wednesday
Is not the same day. The correct one (iGoogled) is the Time one.
Why is that?
Date by default uses the Julian calendar. When you're dealing with dates that far in the past you're going to get weird behaviour prior to calendar reform.
irb(main):013:0> Time.new(1001,1,1).strftime("%A")
=> "Thursday"
irb(main):014:0> Date.new(1001,1,1, Date::GREGORIAN).strftime("%A")
=> "Thursday"
More detail here:
https://gist.github.com/pixeltrix/e2298822dd89d854444b

Get start and end epoch times for today

I'm using an API that requires a start_time and an end_time in epoch that will give me data between those times. The question is I want all the data from the start of UTC today to the end of UTC today.
What's the most effective way to do this in ruby?
You can use the ActiveSupport Date functions #beginning_of_day and #end_of_day. And use to_i to convert the time to seconds since Epoch.
require 'active_support/core_ext'
Date.today.beginning_of_day.to_i
# => 1395532800
Date.today.end_of_day.to_i
# => 1395619199
Date.today.to_time.to_i
should get you the start, and
(Date.today + 1).to_time.to_i
should get you the end.
If you are not using rails or do not wish to import require 'active_support/core_ext' for some reason you can do it using ruby as follows
Date.today.to_time.to_i # beginning_of_day
=> 1481221800
Date.today.to_time.change(hour: 23, min: 59, sec: 59).to_i # end_of_day
=> 1481308199

How to get the current month with Sequel

I would like recover a list of entries for the current month with Sequel.
I tried:
Entry.where(:date >= Date.month).sum(:duration)
or
Entry.where(:date.like('%/06/2013')).sum(:duration)
and other ways, but none of them seemed to work.
If you want all entries the current month and the current year, it's probably easiest to use a range:
d = Date.today
Entry.where(:date=> Date.new(d.year, d.month)...(Date.new(d.year, d.month) >> 1)).sum(:duration)
If you want the current month in any year, Sequel has built in support for this:
Entry.where(Sequel.extract(:month, :date) => Date.today.month).sum(:duration)
You'll need to think in terms of how a database thinks, and how Sequel turns Ruby ranges into SQL:
require 'date'
today = Date.today # => #<Date: 2013-07-03 ((2456477j,0s,0n),+0s,2299161j)>
first_of_month = (today - today.day) + 1
first_of_month # => #<Date: 2013-07-01 ((2456475j,0s,0n),+0s,2299161j)>
next_month = today + 31 # => #<Date: 2013-08-03 ((2456508j,0s,0n),+0s,2299161j)>
last_of_month = next_month - next_month.day # => #<Date: 2013-07-31 ((2456505j,0s,0n),+0s,2299161j)>
last_of_month # => #<Date: 2013-07-31 ((2456505j,0s,0n),+0s,2299161j)>
Entry.where(:date => [first_of_month .. last_of_month]).sum(:duration)
I'd show you the SQL output, but I don't know the database type you're using, and, well, I'm lazy.
Often, you can play tricks inside the DB by truncating "now" to remove the day, then finding all timestamps whose truncated date matches it. That's a lot more specific to the DBM than using Sequel, which already knows how to deal with ranges when converting them to a "between"-type statement.

looking for a method that will show tomorrow's date in ruby

Though, I already got the answer to my question, I decided to edit it.
I was looking for any method in Ruby that can show tomorrow's date.
It's ok if it will show the time as well, I will format its output.
The Time.now gives current date, time and timezone:
Time.now
=> 2013-06-11 13:09:02 +0900
How can I use this method to get a date for tomorrow?
It's ok if there are other methods that can do it.
require 'date'
tomorrow = Date.today + 1
tomorrow is a date object. You can print it in the format you want.
Try:
Time.now + 24*60*60
(Edited: xaxxon is right. My earlier version used Rails' functionality)
ruby 2.6+ helper method
Date.tomorrow
Similar to Stu Gla:
today = Time.now
tomorrow = today + (60 * 60 * 24)
you can then use the .strftime method to format... example:
puts tomorrow.strftime("%F")
# => 2014-08-07

Ruby DateTime.Parse to local time

I have a date string 20101129220021, so I will use
require 'date'
d = DateTime.parse('20101129220021')
This part works fine, and I get a date, which is in UTC.
My question is, how can I convert this into my local time? I tried many methods like extracting the time part using d.to_time and manipulate the result, but it didn't work. As far as I know, DateTime object is immutable. Can I please get some help?
irb(main):001:0> require "date"
=> true
irb(main):002:0> d = DateTime.parse('20101129220021')
=> #<DateTime: 2010-11-29T22:00:21+00:00 (70719276007/28800,0/1,2299161)>
irb(main):003:0> d.to_time
=> 2010-11-30 00:00:21 +0200
ruby 1.9.2p180 (2011-02-18)
You can add a rational fraction based on the timezone to get the local time.
require 'date'
# Make this whatever your zone is. Using UTC +0300 here.
ZONE = 3
d = DateTime.parse('20101129220021') + Rational(ZONE,24)
d.to_s # => "2010-11-30T01:00:21+00:00"

Resources