Ruby - 将持续时间计算为整月的百分比

时间:2011-08-30 19:12:42

标签: ruby time sinatra

我想计算从特定开始时间和结束时间开始的持续时间,并以小时/分钟/秒为单位输出,以及持续时间与整月相比的百分比。

有时一个月内的总持续时间是多个持续时间块。这是一个例子:

用Ruby思考加载:

Start = Date.new(2011-09-01 00:00:00)  #24h clock - start of month
End = Date.new(2011-09-31 24:00:00) # end of month
Duration1 = Date.new(2011-09-03 12:30:00) Date.new(2011-09-11 12:30:00) #From - to
Duration2 = Date.new(2011-09-13 12:30:00) Date.new(2011-09-18 12:30:00) #From - to

Duration = Duration 1 + Duration2 #Might be even more durations during a month

Puts Duration.to_s
Total = Start + End
Percentage = (Duration / Total) * 100

知道我怎么能用上面的代码创建方法吗?如果我可以运行更简单的东西,那将是非常棒的:

Duration1 = Timedifference "2011-09-03 12:30:00", "2011-09-11 12:30:00" # Specify duration like this
Duration2 = Timedifference "2011-09-13 12:30:00", "2011-09-18 12:30:00" # Specify duration like this
Duration = Duration1 + Duration2
puts Duration
puts Percentage.Duration

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

有很多方法可以解决这个问题。假设有人想避免依赖标准库以外的任何东西,我就是这样做的:

require 'time'

# Calculate difference between two given Time objects
# and output as hours, minutes and seconds
def duration(time_start, time_end)
  secs  = (time_end - time_start).to_i
  hours = secs / 3600;
  secs -= hours * 3600
  mins  = secs / 60;
  secs -= mins * 60
  puts "#{hours}h #{mins}m #{secs}s"
end

# EDIT: Simpler and more elegant method (thanks to @knut)
def duration2(time_start, time_end)
  puts Time.at(time_end - time_start).gmtime.strftime('%dd %Hh %Mm %Ss')
end

# Calculate the difference between two given Time objects,
# and the duration of the month the latter is in, then
# output the ratio as a percentage with 2 decimal places
def percentage_of_month(time_start, time_end)
  time_secs  = time_end - time_start
  month_this = Date.civil(time_end.year, time_end.month, 1)
  month_next = month_this.next_month
  month_secs = (month_next - month_this) * 24 * 3600
  percentage = (time_secs * 100.0 / month_secs)
  puts '%2.2f%%' % percentage
end

time1a = Time.parse '2011-09-03 12:30:00'
time1b = Time.parse '2011-09-11 12:30:00'
duration time1a, time1b             # => 192h 0m 0s
percentage_of_month time1a, time1b  # => 26.67%

time2a = Time.parse '2011-09-13 12:30:00'
time2b = Time.parse '2011-09-18 12:30:00'
duration time2a, time2b             # => 120h 0m 0s
percentage_of_month time2a, time2b  # => 16.67%

time3a = Time.parse '2011-08-30 11:03:18'
time3b = Time.parse '2011-08-30 23:31:54'
duration time3a, time3b             # => 12h 28m 36s
percentage_of_month time3a, time3b  # => 1.68%

答案 1 :(得分:1)

以小时/分钟/秒为单位的输出非常简单:

Time.at(duration).gmtime.strftime("%R:%S")

详细信息请参阅http://www.ruby-forum.com/topic/48877

对于百分比,我需要更详细的规范。

  • 你一个月的基础是什么? 2月有28天(但不要忘记闰年),其他月份有30或31天。
  • 愿你的持续时间跨越不同月份吗?
  • 您能提供一些测试数据和预期结果吗?

下面有一些假设的解决方案。

require 'date'

=begin rdoc
Calculate seconds for a given month
=end
def seconds_of_month( year, month )  
  raise ArgumentError if month > 12
  month_start = DateTime.new(year, month, 1, 0)  #24h clock ,  start of month
  if month == 12
    month_end = DateTime.new(year + 1, 1, 1, 0) # jan 1st of following year
  else
    month_end = DateTime.new(year, month+1, 1) # start of next month
  end
  days = month_end -  month_start 
  #~ puts "%i-%i has %i days (%s-%s)" % [year, month, days, month_start, month_end ]
  days * 24 * 60 * 60 #convert month in second --- no check for leap seconds
end

=begin rdoc
Calculate percentage of duration in seconds per month.
=end
def percentage_of_month( duration, year, month )
  (duration / seconds_of_month(year,month)) * 100
end


duration = #=> 1123200s
  ( Time.local(2011, 9, 11, 12, 30, 00) - Time.local(2011, 9, 03, 12, 30, 00) ) +
  ( Time.local(2011, 9, 18, 12, 30, 00) - Time.local(2011, 9, 13, 12, 30, 00) )


1.upto(12){|mon|
  puts '%is is %.2f%% of 2011/%i (%is)' % [ duration, percentage_of_month( duration, 2011,mon), mon, seconds_of_month( 2011, mon )   ]
}

结果:

1123200s is 41.94% of 2011/1 (2678400s)
1123200s is 46.43% of 2011/2 (2419200s)
1123200s is 41.94% of 2011/3 (2678400s)
1123200s is 43.33% of 2011/4 (2592000s)
1123200s is 41.94% of 2011/5 (2678400s)
1123200s is 43.33% of 2011/6 (2592000s)
1123200s is 41.94% of 2011/7 (2678400s)
1123200s is 41.94% of 2011/8 (2678400s)
1123200s is 43.33% of 2011/9 (2592000s)
1123200s is 41.94% of 2011/10 (2678400s)
1123200s is 43.33% of 2011/11 (2592000s)
1123200s is 41.94% of 2011/12 (2678400s)

答案 2 :(得分:0)

或者非常简单的版本:

days_in_month = Date.today.end_of_month.day.to_f    
day_of_month = Date.today.strftime('%-d').to_f    
percentage_of_way_through_month = day_of_month/days_in_month    

,如果需要剩余月份,则相反:

days_in_month = Date.today.end_of_month.day.to_f    
day_of_month = Date.today.strftime('%-d').to_f    
percentage_of_month_to_go = 1-(day_of_month/days_in_month)