This gem is an extraction of
ActiveSupport::Duration from Rails, along with the
related core extensions.
Ruby 2.0 or greater is required.
If you're in a Rails project, then you should use
Otherwise there are several reason why you might prefer
active_support/core_ext/integer/timewill only require what you want, but in fact it will require a total of 5000 LOC (a lot of those are additional core extensions which you may not have wanted).
as-durationhas only under 200 LOC, and only gives you what you've asked for.
It sure is! I copied all the related tests from Rails, and modified them
so that they work standalone. So,
as-duration passes all of Rails' tests.
NOTE: In most cases
as-duration should work exactly like
ActiveSupport::Duration. However, there are a few modifications made, mostly
removing some of the magic, see Modifications.
The following methods are added on
# plural versions 2.seconds 3.minutes 4.hours 5.days 6.weeks 7.fortnights 8.months 9.years # singular versions 1.second 1.minute 1.hour 1.day 1.week 1.fortnight 1.month 1.year
The only exception is
#years which are only added to
(to maintain precision in calculations).
You can add and subtract durations from
Time.now + 2.hours Date.today + 1.year
When you add seconds/minutes/hours to a Date, the Date is automatically
converted to a
(Date.today + 1.minute).class #=> Time
As syntax sugar, you can also call time methods on the duration object:
# forward in time 1.year.from_now 2.months.since(Date.new(2015,4,27)) 2.months.after(Date.new(2015,4,27)) 2.months.from(Date.new(2015,4,27)) # back in time 2.hours.ago 20.minutes.until(Time.now) 20.minutes.before(Time.now) 20.minutes.to(Time.now)
You can add and subtract durations:
1.week + 1.day 2.minutes - 1.second
ActiveSupport::Duration, you can't add durations to integers and vice
versa. You either have to convert the integer to a duration, or
the duration to an integer with
AS::Duration#to_i. This is to help you
not to mix different time units.
# Bad 10 + 1.minute # TypeError 1.minute + 10 # TypeError # Good 10.seconds + 1.minute # AS::Duration 1.minute.to_i + 10 # Integer
The behaviour of
ActiveSupport::Duration has been slightly modified, mostly
to remove some magic:
#agocannot take any arguments, they always use the current time (passing an argument doesn't read well, better to use
DateTimewas first introduced in Ruby so that you can represent time that the
Timeclass at the moment wasn't able to. However, the
Timeclass improved over time and removed those limitations, so there is no more need to use
AS::Durationdoesn't act like an Integer