Ruby on rails 更精确的时间距离(大写)
用词来说,时间的距离很好,但有时不够精确 我需要一个函数,它可以用文字报告精确的时间距离。例如,上午7:50到10:10的距离应该是“2小时20分钟”,而不是“大约2小时”,或者用词来说是时间的距离 我的用例是报告火车时刻表,特别是给定的火车行程需要多长时间。Ruby on rails 更精确的时间距离(大写),ruby-on-rails,time,Ruby On Rails,Time,用词来说,时间的距离很好,但有时不够精确 我需要一个函数,它可以用文字报告精确的时间距离。例如,上午7:50到10:10的距离应该是“2小时20分钟”,而不是“大约2小时”,或者用词来说是时间的距离 我的用例是报告火车时刻表,特别是给定的火车行程需要多长时间。def distance\u of\u time\u以小时和分钟为单位(从时间到时间) def distance_of_time_in_hours_and_minutes(from_time, to_time) from_time =
def distance\u of\u time\u以小时和分钟为单位(从时间到时间)
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
from_time=from_time.to_time如果是from_time.response_to?(:to_time)
to_time=to_time.to_time如果to_time.response_to?(:to_time)
以小时为单位的距离=((到时间-从时间).abs)/3600)。楼层
以分钟为单位的距离=(((到时间-从时间).abs)%3600)/60)。整轮
单词中的差异=“”
单词0中的差异
上面的代码把我的Rubymine搞糊涂了。。。我不确定它是否正确。我将其重建如下:
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
(请注意,我在编码环境中使用helper
预先设置了每次调用distance\u of_time\u in_miles
,以避免出现命名错误——例如,请参阅以获取更多信息。我将更改行距离(in_hours=((to_time-from_time).abs)/3600)。四舍五入到distance\u in_hours=(((to_time-from_time).abs)/3600)。to_i这轮将取整,使1.5小时看起来像2小时30分钟。我建议使用((to_time-from_time.abs)/3600)。floor
而不是((to_time-from_time.abs)/3600)。取整
,因为有时它显示“2小时50分钟”当时间相差1小时和50分钟时,这行“words=dist-Updated-Link:实际上效果更好。
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