Ruby:将秒转换为自然语言

Ruby:将秒转换为自然语言,ruby,datetime,format,Ruby,Datetime,Format,我们如何将以秒为单位的等待时间转换成一个字符串,自然地向人类表示等待时间 输入和输出示例 45=>“45秒”#如果少于一分钟,以秒表示 85=>“1分钟”#如果少于一小时,以四舍五入的分钟表示 3745=>“直到今天下午5:45”#如果少于一天,则在一天中的时间内进行快车,四舍五入到最接近的15分钟 9342=>“直到周二下午6:30”#如果少于一周,以一周中的某一天表示,时间四舍五入到最接近的15分钟 139342=>“截止到2017年12月13日下午2:00”#否则,以日期和时间表示,四舍

我们如何将以秒为单位的等待时间转换成一个字符串,自然地向人类表示等待时间

输入和输出示例

  • 45=>“45秒”#如果少于一分钟,以秒表示
  • 85=>“1分钟”#如果少于一小时,以四舍五入的分钟表示
  • 3745=>“直到今天下午5:45”#如果少于一天,则在一天中的时间内进行快车,四舍五入到最接近的15分钟
  • 9342=>“直到周二下午6:30”#如果少于一周,以一周中的某一天表示,时间四舍五入到最接近的15分钟
  • 139342=>“截止到2017年12月13日下午2:00”#否则,以日期和时间表示,四舍五入至最接近的15分钟
    • 代码

      require 'time'
      
      FMT = [[s =  60,         "%-S seconds",                   :NO_ROUND],
             [s *= 60,         "%-M minutes",                   :ROUND_MINS],
             [s *= 24,         "until %-l:%M %p today",         :ROUND_UP_MINS_15],
             [s *   7,         "until %A %-l:%M %p",            :ROUND_UP_MINS_15],
             [Float::INFINITY, "until %-_m/%d/%y at %-l:%M %p", :ROUND_UP_MINS_15]]
      
      def fmt_duration(start_time = Date.today.to_time, elapsed_seconds)
        secs = start_time.to_i + elapsed_seconds
        _, fmt, round = FMT.find { |max_secs_plus_1, *_| elapsed_seconds < max_secs_plus_1 }
        rnd_secs =
        case round
        when :NO_ROUND
          secs
        when :ROUND_MINS
          mins = (secs/60.0).round
          mins -= 1 if mins % 60 == 0
          60 * mins
        when :ROUND_UP_MINS_15
          900 * (secs/900.0).ceil
        end
        Time.at(rnd_secs).strftime(fmt)
      end
      
      请参阅
      FMT
      第二列中的
      Date
      Time
      格式代码列表

      示例

      def time_to_secs(days, hours, minutes, seconds)
        seconds + 60 * (minutes + 60 * (hours + 24 * days))
      end
      
      secs = time_to_secs( 0,  0,  0, 37) #=> 37
      fmt_duration(secs)                  #=> "37 seconds"
      
      secs = time_to_secs( 0,  0, 41, 37) #=> 2_497
      fmt_duration(secs)                  #=> "42 minutes"
      
      secs = time_to_secs( 0, 13, 41, 37) #=> 49_297
      fmt_duration(secs)                  #=> "until 1:45 PM today"
      
      secs = time_to_secs( 6, 23, 36, 37) #=> 603_397
      fmt_duration(secs)                  #=> "until Tuesday 11:45 PM"
      
      secs = time_to_secs(24, 13, 41, 37) #=> 2_122_897
      fmt_duration(secs)                  #=> "until 7/22/17 at 1:45 PM"
      

      你在使用Rails吗?@Saratibetts Rails助手没有介绍这个用例,因为它是针对“很久以前”而不是“直到”的。@ma111hew28-我认为没有一个宝石存在于此。但是您可以将其实现为一系列if语句,就像您在问题中所做的那样。您的需求非常具体。如果有一个内置的助手,我会感到惊讶。@eiko它可能会转换时间以便工作
      def time_to_secs(days, hours, minutes, seconds)
        seconds + 60 * (minutes + 60 * (hours + 24 * days))
      end
      
      secs = time_to_secs( 0,  0,  0, 37) #=> 37
      fmt_duration(secs)                  #=> "37 seconds"
      
      secs = time_to_secs( 0,  0, 41, 37) #=> 2_497
      fmt_duration(secs)                  #=> "42 minutes"
      
      secs = time_to_secs( 0, 13, 41, 37) #=> 49_297
      fmt_duration(secs)                  #=> "until 1:45 PM today"
      
      secs = time_to_secs( 6, 23, 36, 37) #=> 603_397
      fmt_duration(secs)                  #=> "until Tuesday 11:45 PM"
      
      secs = time_to_secs(24, 13, 41, 37) #=> 2_122_897
      fmt_duration(secs)                  #=> "until 7/22/17 at 1:45 PM"