13

distance_of_time_in_words is great, but sometimes it's not granular enough.

I need a function that will report exact distances of time in words. E.g., 7:50AM to 10:10AM should be a distance of "2 hours and 20 minutes", not "about 2 hours" or whatever distance_of_time_in_words would do.

My use case is reporting train schedules, and in particular how long a given train ride will take.

1

2 Answers 2

3
def distance_of_time_in_hours_and_minutes(from_time, to_time)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_hours   = (((to_time - from_time).abs) / 3600).floor
  distance_in_minutes = ((((to_time - from_time).abs) % 3600) / 60).round

  difference_in_words = ''

  difference_in_words << "#{distance_in_hours} #{distance_in_hours > 1 ? 'hours' : 'hour' } and " if distance_in_hours > 0
  difference_in_words << "#{distance_in_minutes} #{distance_in_minutes == 1 ? 'minute' : 'minutes' }"
end
2
  • 1
    I would change the line distance_in_hours = (((to_time - from_time).abs) / 3600).round to distance_in_hours = (((to_time - from_time).abs) / 3600).to_i The round will round up and make 1.5 hours look like 2 hours 30 minutes Aug 6, 2010 at 20:35
  • I would suggest using (((to_time - from_time).abs) / 3600).floor instead of (((to_time - from_time).abs) / 3600).round since sometimes it showed "2 hours and 50 minutes" when there were 1h and 50 min difference between times, Oct 24, 2018 at 12:28
2

The above code confused my Rubymine ... and I'm not sure it's actually correct. I rebuilt it as follows:


def distance_of_time_in_hours_and_minutes(from_time, to_time)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  dist = to_time - from_time
  minutes = (dist.abs / 60).round
  hours = minutes / 60
  minutes = minutes - (hours * 60)

  words = dist <= 0 ? '' : '-'

  words << "#{hours} #{hours > 1 ? 'hours' : 'hour' } and " if hours > 0
  words << "#{minutes} #{minutes == 1 ? 'minute' : 'minutes' }"
end

Here's some Rspec for the above:


describe "Distance of Time in Hours and Minutes" do
  before do
    @time1 = Time.utc(2010,"sep",7,14,15,3)
    @time2 = @time1 + 28
    @time3 = @time1 + 30
    @time4 = @time1 + 60
    @time5 = @time1 + 60*60
    @time6 = @time1 + 60*60 + 60
    @time7 = @time1 + 60*60 + 5*60
  end

  it "calculates time differences properly" do
    distance_of_time_in_hours_and_minutes(@time1, @time1).should == "0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time2).should == "0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time3).should == "1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time4).should == "1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time5).should == "1 hour and 0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time6).should == "1 hour and 1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time7).should == "1 hour and 5 minutes"
    distance_of_time_in_hours_and_minutes(@time7, @time1).should == "-1 hour and 5 minutes"
    distance_of_time_in_hours_and_minutes(@time3, @time1).should == "-1 minute"
  end
end

(Note that I had prepend each invocation of distance_of_time_in_miles with helper in my coding environment to avoid a NoMethodError error -- see for instance http://old.nabble.com/How-to-spec-a-Rails-helper-method-td20660744.html for more on that.

1
  • 1
    Isn't this line "words = dist <= 0 ? '' : '-'" the wrong way round? You return the minus sign if dist is positive, whereas presumably you should return it if dist is negative. Jan 18, 2011 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.