2009-08-03 6 views
11

distance_of_time_in_words es genial, pero a veces no es lo suficientemente granular.Más precisa distance_of_time_in_words

Necesito una función que informe las distancias exactas de tiempo en palabras. Por ejemplo, 7:50 AM a 10:10 AM debe ser una distancia de "2 horas y 20 minutos", no "alrededor de 2 horas" o lo que distance_of_time_in_words haría.

Mi caso de uso informa los horarios del tren y, en particular, cuánto durará un viaje en tren.

+0

Enlace Actualizado:.. https://github.com/radar/dotiw funciona incluso mejor, en realidad –

Respuesta

2
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).round 
    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 
+1

que cambiaría la línea distance_in_hours = (((to_time - FROM_TIME) .abs)/3600) .round a distance_in_hours = (((to_time - FROM_TIME) .abs)/3600) .to_i La ronda redondear y hacer 1,5 horas parece que 2 horas 30 minutos – theschmitzer

2

El código anterior confundió mi Rubymine ... y no estoy seguro de que sea realmente correcto. Lo reconstruí la siguiente manera:


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 

He aquí algunos Rspec de lo anterior:


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 

(Tenga en cuenta que tuve anteponer cada invocación de distance_of_time_in_miles con helper en mi entorno de codificación para evitar un error NoMethodError - véase, por ejemplo http://old.nabble.com/How-to-spec-a-Rails-helper-method-td20660744.html para más información sobre que

+0

¿No es esta línea "words = dist <= 0? '': '-' "al revés? Usted devuelve el signo menos si dist es positivo, mientras que presumiblemente usted * debería * devolverlo si dist es negativo. –

Cuestiones relacionadas