Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用于转换时间单位的自定义函数_Ruby On Rails_Ruby_Time - Fatal编程技术网

Ruby on rails 用于转换时间单位的自定义函数

Ruby on rails 用于转换时间单位的自定义函数,ruby-on-rails,ruby,time,Ruby On Rails,Ruby,Time,我有以下用于将小时转换为时间单位的自定义函数: 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_

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

  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
我有两个问题:

  • 有时候我并不总能得到我想要的结果。对于ex,我的返回时间是-2700小时

  • 当时间单位为月时,我还想返回剩余的天数。例如,2个月零13天


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


    如果您需要对月份更严格一些,可以使用此代码

    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
    
    需要“日期”
    类MyDateCalculator
    def time_expiration_text(小时)#时间单位为小时
    时间\到期\文本=“”
    如果(小时<24小时)
    时间\到期\文本=格式(小时,'小时')
    其他的
    天=小时/24
    now=DateTime.now
    now_month=从现在起的月份
    到期日=现在。截止日期+天
    到期月份=自到期日起的月份
    月份=到期月份-现在月份
    如果月份==0或(月份==1且到期日=现在到下个月(月)+1
    额外天数=到期日-现在到下个月(月)
    到期时间文本+=''+到期时间文本(额外天数至i*24)
    结束
    结束
    结束
    返回时间\u到期\u文本
    结束
    私有的
    def格式(数字、单位)
    text=“#{number}#{unit}”
    数字=1?文本:文本+'s'
    结束
    def自_纪元起的月份(日期)
    返回日期.year*12+日期.month
    结束
    结束
    

    正如你所看到的,这比凯尔所建议的要复杂得多,这是正确计算月份的唯一好处。

    keruilin,逻辑有点错误,因为你假设每个月都有30天。我会玩一下代码,然后发布一个答案
    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