# Can Ruby print out time difference (duration) readily?

J

#### Jian Lin

Can Ruby do something like this?

irb(main):001:0> start = Time.now
=> Thu Nov 05 01:02:54 -0800 2009

irb(main):002:0> Time.now - start
=> 25.239

irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
and similarly, report

23 minutes and 35 seconds
1 hour and 33 minutes
2 days and 3 hours
(either report the whole duration, up to how many seconds, or report up
to 2 numbers and units (if day and hour is reported, then no need to
tell how many minutes)

E

#### Ehsanul Hoque

Can Ruby do something like this?
=20
irb(main):001:0> start =3D Time.now
=3D> Thu Nov 05 01:02:54 -0800 2009
=20
irb(main):002:0> Time.now - start
=3D> 25.239
=20
irb(main):003:0> (Time.now - start).duration
=3D> "25 seconds"
and similarly=2C report
=20
23 minutes and 35 seconds
1 hour and 33 minutes
2 days and 3 hours
(either report the whole duration=2C up to how many seconds=2C or report = up
to 2 numbers and units (if day and hour is reported=2C then no need to
tell how many minutes)

You can try something like below (change with conditional statements to fit=
your requirements of upto 2 numbers):

class Time
def duration
Time.now - self
end
def duration_string
difference =3D duration
days =3D (difference/(3600*24)).to_i
hours =3D ((difference%(3600*24))/3600).to_i

mins =3D ((difference%(3600))/60).to_i

secs =3D (difference%60).to_i
"#{days} days=2C #{hours} hours=2C #{mins} minutes and #{secs} seconds"
end
end

start =3D Time.new
sleep(5)
puts start.duration_string

=20
_________________________________________________________________
Hotmail: Trusted email with Microsoft's powerful SPAM protection.
http://clk.atdmt.com/GBL/go/177141664/direct/01/
http://clk.atdmt.com/GBL/go/177141664/direct/01/

M

#### Mark Thomas

Can Ruby do something like this?

irb(main):001:0> start =Time.now
=> Thu Nov 05 01:02:54 -0800 2009

irb(main):002:0>Time.now - start
=> 25.239

irb(main):003:0> (Time.now - start).duration
=> "25 seconds"

require 'chronic_duration'
ChronicDuration.output(Time.now - start, :format => :long)

# => "2 minutes 23 seconds"

See http://github.com/hpoydar/chronic_duration

I

#### Intransition

Can Ruby do something like this?

irb(main):001:0> start =3D Time.now
=3D> Thu Nov 05 01:02:54 -0800 2009

irb(main):002:0> Time.now - start
=3D> 25.239

irb(main):003:0> (Time.now - start).duration
=3D> "25 seconds"
and similarly, report

23 minutes and 35 seconds
1 hour and 33 minutes
2 days and 3 hours
(either report the whole duration, up to how many seconds, or report up
to 2 numbers and units (if day and hour is reported, then no need to
tell how many minutes)

RichUnits also has a duration class. You can tell it what time
segments you want to use.

P

#### Phrogz

Can Ruby do something like this?

irb(main):001:0> start = Time.now
=> Thu Nov 05 01:02:54 -0800 2009

irb(main):002:0> Time.now - start
=> 25.239

irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
and similarly, report

23 minutes and 35 seconds
1 hour and 33 minutes
2 days and 3 hours

This old thread discusses a similar topic.
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/e51d23dadacc03fa

Note that durations expressed in any unit greater than weeks (months,
years, etc.) are going to give you problems.

## Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Threads
473,841
Messages
2,569,826
Members
45,894
Latest member
ConradBarn