Forum: Ruby on Rails distance_of_time_in_german_words

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martin G. (Guest)
on 2006-05-24 04:31
(Received via mailing list)
Hiall,

This could be useful for those of you producing websites in the german
language. Basically I copied the definition of the rails builtin
distance_of_time_in_words and extracted another method
german_words_for_distance_in_seconds. Just put these e.g. into module
ApplicationHelper and you can use it in all views.

  def distance_of_time_in_german_words(from_time, to_time = 0,
include_seconds = false)
    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_seconds = ((to_time - from_time).abs).round

    german_words_for_distance_in_seconds(distance_in_seconds)

  end

  def german_words_for_distance_in_seconds(distance_in_seconds)

    distance_in_minutes = ((distance_in_seconds.abs)/60).round

    case distance_in_minutes
      when 0..1
        return (distance_in_minutes==0) ? 'weniger als eine Minute' :
'1 Minute' unless include_seconds
        case distance_in_seconds
          when 0..5   then 'weniger als 5 Sekunden'
          when 6..10  then 'weniger als 10 Sekunden'
          when 11..20 then 'weniger als 20 Sekunden'
          when 21..40 then 'eine halbe Minute'
          when 41..59 then 'weniger als 1 Minute'
          else             '1 Minute'
        end

      when 2..45      then "#{distance_in_minutes} Minuten"
      when 46..90     then 'ca. 1 Stunde'
      when 90..1440   then "ca. #{(distance_in_minutes.to_f /
60.0).round} Stunden"
      when 1441..2880 then '1 Tag'
      else                 "#{(distance_in_minutes / 1440).round} Tage"
    end
  end

Maybe sometime this will be helpful for someone ...

cheers
Martin
This topic is locked and can not be replied to.