计算时间持续时间成整个月的百分比



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

有时一个月的总持续时间分为多个持续时间块。下面是一个例子:

用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

提前感谢您的帮助。

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

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%

以小时/分/秒为单位的输出很容易:

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)

或者更简单的版本:

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)    

最新更新