计算整个月的百分比时间

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

有时候一个月内的总时长是在多个时间段内。 这里是一个例子:

考虑用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)
    
    链接地址: http://www.djcxy.com/p/95415.html

    上一篇: Calculate time duration into percentages of a whole month

    下一篇: Find time occurence and duration in certain time range