Ruby on Rails
Duration (Version #7)

Synopsis

Duration is a package for creating timespans in which you can create those nice strings for when you want to show things like “20 days and 7 minutes”.

Duration contains 2 classes: Duration, BigDuration. The Duration class is used for creating yearless/monthless durations, so the highest unit is weeks. This was decided to be done this way because years and months are not consistent on a yearly basis, where in a given year a given month may not have a desired number of days, which ultimately creates an inconsistency in the total number of days in a year

Installing Duration

Installing Duration is fairly simple. You can choose to install it from a tar.gz archive or from RubyGems.

We’ll use RubyGems to explain the install process, as it is the recommended and easiest way of installing Duration.

On nix distributions, you may need to be root to do this (on Ubuntu just prepend sudo)

$ gem install duration --remote

Usage Duration

So, you’ve installed Duration? Let’s see if you can use it.

$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'duration'
=> true
irb(main):003:0>

Hey, if you got this far, you’re doing good! So, now we’ve confirmed that Duration is properly installed, let’s try to use it. There’s a few ways you can create a duration object.

irb(main):003:0> d = Duration.new(30)
=> #<Duration: 30 seconds>
irb(main):004:0>

Well, that was easy. It’s pretty obvious what we did here. We created a new Duration object with the timespan of 30 seconds. Another cool thing about Duration is:

irb(main):004:0> d = Duration.new(130)
=> #<Duration: 2 minutes and 10 seconds>
irb(main):005:0>

Oh my freakin’ god! Duration knows how to automatically determine minutes from the seconds! Yes, well this works for days and weeks as well.

But what if we want to specify how many weeks, days or even minutes we want the timespan to be, rather than giving a number in seconds? Well that’s easy, we’ll just pass a hash to Duration.new:

irb(main):005:0> d = Duration.new(:weeks => 2, :minutes => 5)
=> #<Duration: 2 weeks and 5 minutes>
irb(main):006:0>

Holy crap, that was so easy I almost farted. Well, all this is cool, but let’s get more into the Ruby Way of doing things:

irb(main):006:0> d = 2.days
=> #<Duration: 2 days>
irb(main):007:0> d += 2.days - 10.minutes
=> #<Duration: 3 days, 23 hours and 50 minutes>

Yeah, Duration is just that cool. There are methods added to the Numeric class which enhance the Rubyism of creating and managing Duration objects. Such methods are #seconds, #minutes, #days, #weeks, #months, and #years.

BigDuration

Shortly explained, the BigDuration class is identical to the Duration class, except that it supports years and months. It’s usually not recommended, because of the problems with accuracy in the number of days in a given year, but it’s useful when you only want a general representation. BigDuration will assume 1 year as 12 months and 1 month as 30 days. So keep this in mind when deciding to use BigDuration.

Other Information

For any other information, such as API documentation, check the RubyForge project page at http://www.rubyforge.org/projects/duration.

Synopsis

Duration is a package for creating timespans in which you can create those nice strings for when you want to show things like “20 days and 7 minutes”.

Duration contains 2 classes: Duration, BigDuration. The Duration class is used for creating yearless/monthless durations, so the highest unit is weeks. This was decided to be done this way because years and months are not consistent on a yearly basis, where in a given year a given month may not have a desired number of days, which ultimately creates an inconsistency in the total number of days in a year

Installing Duration

Installing Duration is fairly simple. You can choose to install it from a tar.gz archive or from RubyGems.

We’ll use RubyGems to explain the install process, as it is the recommended and easiest way of installing Duration.

On nix distributions, you may need to be root to do this (on Ubuntu just prepend sudo)

$ gem install duration --remote

Usage Duration

So, you’ve installed Duration? Let’s see if you can use it.

$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'duration'
=> true
irb(main):003:0>

Hey, if you got this far, you’re doing good! So, now we’ve confirmed that Duration is properly installed, let’s try to use it. There’s a few ways you can create a duration object.

irb(main):003:0> d = Duration.new(30)
=> #<Duration: 30 seconds>
irb(main):004:0>

Well, that was easy. It’s pretty obvious what we did here. We created a new Duration object with the timespan of 30 seconds. Another cool thing about Duration is:

irb(main):004:0> d = Duration.new(130)
=> #<Duration: 2 minutes and 10 seconds>
irb(main):005:0>

Oh my freakin’ god! Duration knows how to automatically determine minutes from the seconds! Yes, well this works for days and weeks as well.

But what if we want to specify how many weeks, days or even minutes we want the timespan to be, rather than giving a number in seconds? Well that’s easy, we’ll just pass a hash to Duration.new:

irb(main):005:0> d = Duration.new(:weeks => 2, :minutes => 5)
=> #<Duration: 2 weeks and 5 minutes>
irb(main):006:0>

Holy crap, that was so easy I almost farted. Well, all this is cool, but let’s get more into the Ruby Way of doing things:

irb(main):006:0> d = 2.days
=> #<Duration: 2 days>
irb(main):007:0> d += 2.days - 10.minutes
=> #<Duration: 3 days, 23 hours and 50 minutes>

Yeah, Duration is just that cool. There are methods added to the Numeric class which enhance the Rubyism of creating and managing Duration objects. Such methods are #seconds, #minutes, #days, #weeks, #months, and #years.

BigDuration

Shortly explained, the BigDuration class is identical to the Duration class, except that it supports years and months. It’s usually not recommended, because of the problems with accuracy in the number of days in a given year, but it’s useful when you only want a general representation. BigDuration will assume 1 year as 12 months and 1 month as 30 days. So keep this in mind when deciding to use BigDuration.

Other Information

For any other information, such as API documentation, check the RubyForge project page at http://www.rubyforge.org/projects/duration.