【问题标题】:Custom function for converting unit of time转换时间单位的自定义函数
【发布时间】:2011-03-18 10:46:13
【问题描述】:

我有以下自定义函数将小时转换为时间单位:

  def time_expiry_text(time_expiry) # unit of time is hours
    time_expiry_text = ''

    if ( (time_expiry / 24) > 30 ) then # if more than 30 days use months as unit
      months = ( (time_expiry / 24) / 30 )
      time_expiry_text = months.to_s
      time_expiry_text += months == 1 ? ' month' : ' months'
    elsif time_expiry >= 24 then # if greater than or equal to 1 day or 24 hours use days as unit
      days = time_expiry / 24
      time_expiry_text = days.to_s
      time_expiry_text += days == 1 ? ' day' : ' days'
    else
      hours = time_expiry
      time_expiry_text = hours.to_s
      time_expiry_text += hours == 1 ? ' hour' : ' hours'
    end

    return time_expiry_text
  end

我有两个问题:

  1. 有时我并不总能得到我想要的结果。例如,我的返回时间为 -2700 小时。

  2. 当时间单位是月时,我也想返回剩余的天数。例如,2 个月零 13 天。

【问题讨论】:

  • keruilin,逻辑有点错误,因为您假设每个月都是 30 天。我会玩一下代码并发布答案

标签: ruby-on-rails ruby time


【解决方案1】:

假设您使用广义的月份定义(并不总是 30 天,但您的逻辑似乎与日期无关)

hours = 16530
=> 16530    
days = hours / 24 
=> 688 #days
extra_hours = hours % 24
=> 18 #hours (unallocated)
months = days/30 
=> 22 #months
extra_days = days % 30
=> 28 #days (unallocated)

string = ""

string += months.to_s + " months " if months 
string += extra_days.to_s + " days " if extra_days
string += extra_hours.to_S + " hours " if extra_hours
string.strip! #or string = string.strip

string
=> "22 months 28 days 18 hours"

【讨论】:

    【解决方案2】:

    如果你需要更严格的月份,你可以使用这个代码

    require 'date'
    
    class MyDateCalculator
      def time_expiry_text(hours) # unit of time is hours
        time_expiry_text = ''
    
        if( hours < 24)
          time_expiry_text = format(hours, 'hour')
        else
          days = hours/24
          now = DateTime.now
          now_month = months_since_epoch now
          expiry_date = now.to_date + days
          expiry_month = months_since_epoch expiry_date
          months = expiry_month - now_month
    
          if months == 0 or (months == 1 and expiry_date.day < now.day)
            time_expiry_text = format(days, 'day')
          else
            time_expiry_text = format(months, 'month')
            if expiry_date >= now.to_date.next_month( months ) + 1
              extra_days = expiry_date - now.to_date.next_month( months ) 
              time_expiry_text += ' ' +time_expiry_text( extra_days.to_i * 24)
            end
          end
        end
    
        return time_expiry_text
      end
    
      private
      def format(number, unit)
        text = "#{number} #{unit}"
        number == 1 ? text : text+'s'
      end
    
      def months_since_epoch(date)
        return date.year * 12 + date.month
      end
    
    end
    

    如您所见,这比 Kyle 所建议的要复杂得多,并且正确计算月份的唯一好处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多