【问题标题】:Get random time objects between certain hours in Rails在 Rails 中的特定时间之间获取随机时间对象
【发布时间】:2013-01-01 03:54:01
【问题描述】:

我想要一种方法:

def time_between(from, to)
  ***
end

time_between 10.am.of_today, 3.pm.of_today # => 1pm Time object
time_between 10.am.of_today, 3.pm.of_today # => 3pm Time object
time_between 10.am.of_today, 3.pm.of_today # => 10:30am Time object
# I mean random

这里有两个问题:***如何实现?以及如何实现x.pm.of_today

【问题讨论】:

    标签: ruby-on-rails ruby datetime


    【解决方案1】:

    红宝石 1.9.3

    require 'rubygems'
    require 'active_support/all'
    
    def random_hour(from, to)
      (Date.today + rand(from..to).hour + rand(0..60).minutes).to_datetime
    end
    
    puts random_hour(10, 15)
    

    【讨论】:

      【解决方案2】:

      这是第一次尝试:

      def time_between(from, to)
        today = Date.today.beginning_of_day
        (today + from.hours)..(today + to.hours).to_a.sample
      end
      

      虽然它的工作原理是:

      time_between(10, 15) # => a random time between 10 am and 3 pm
      

      我认为已经足够了,但我愿意接受更好的解决方案。

      【讨论】:

        【解决方案3】:

        要获得随机时间段,您需要计算两次之间的距离。获取具有该距离跨度的随机值。最后将其添加到您的 from time 中。

        类似的东西:(但我不打算测试它)

        def time_between(from, to)
          if from > to
            time_between(to, from)
          else
            from + rand(to - from)
          end
        end
        

        至于创建 DSL 的时间。你可以看看 Rails 是如何做到的。但是要得到你想要的东西。只需创建一个代表一天中的小时数的类。使用 Fixnum 上的 am 或 pm 调用来实例化它。然后为of_today(以及您想要的任何其他方法)编写方法。

        class Fixnum
          def am
            TimeWriter.new(self)
          end
        
          def pm
            TimeWriter.new(self + 12)
          end
        end
        
        class TimeWriter
          MINUTES_IN_HOUR = 60
          SECONDS_IN_MINUTE = 60
          SECONDS_IN_HOUR = MINUTES_IN_HOUR * SECONDS_IN_MINUTE
        
          def initialize hours
            @hours = hours
          end
        
          def of_today
            start_of_today + (hours * SECONDS_IN_HOUR)
          end
        
          private
        
          attr_reader :hours
        
          def start_of_today
            now = Time.now
            Time.new(now.year, now.month, now.day, 0, 0)
          end
        end
        

        您应该添加一些超过 24 小时的错误处理。

        【讨论】:

        • MILLISECONDS_IN_SECOND,你的意思是SECONDS_IN_MINUTE吗?
        【解决方案4】:

        此代码将分钟和小时作为输入。

        require 'rubygems'
        require 'active_support/all'
        
        def random_time(from, to)
          from_arr = from.split(':')
          to_arr   = to.split(':')
          now      = Time.now
          rand(Time.new(now.year, now.month, now.day, from_arr[0], rom_arr[1])..Time.new(now.year, now.month, now.day, to_arr[0], to_arr[1]))
        end
        
        puts random_time('09:15', '18:45')
        

        另一种简单的方法:

        require 'rubygems'
        require 'active_support/all'
        
        def random_time(from, to)
          now      = Time.now
          rand(Time.parse(now.strftime("%Y-%m-%dT#{from}:00%z"))..Time.parse(now.strftime("%Y-%m-%dT#{to}:00%z")))
        end
        
        puts random_time('09:15', '18:45')
        

        【讨论】:

          猜你喜欢
          • 2018-10-16
          • 2016-12-09
          • 1970-01-01
          • 2014-05-22
          • 2011-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-04
          相关资源
          最近更新 更多