【问题标题】:Ruby DateTime: Get next 5:15pm (or similar)Ruby DateTime:获取下一个下午 5:15(或类似)
【发布时间】:2016-09-10 19:39:27
【问题描述】:

因此,给定一个 DateTime 对象和一个固定时间,我想在给定 DateTime 对象之后获取下一次出现的固定时间。

  • 例如,给定 2016 年 3 月 14 日下午 4:00 的日期和下午 5:15 的时间,我想返回 2016 年 3 月 14 日 下午 5:15

  • 但是,鉴于日期为 2016 年 3 月 14 日下午 6:00,时间为下午 5:15,我想返回 2016 年 3 月 15 日,下午 5:15,因为那是下一次发生的事情。

到目前为止,我已经编写了以下代码:

# Given fixed_time and date_time

new_time = date_time
if fixed_time.utc.strftime("%H%M%S%N") >= date_time.utc.strftime("%H%M%S%N")
  new_time = DateTime.new(
    date_time.year,
    date_time.month,
    date_time.day,
    fixed_time.hour,
    fixed_time.min,
    fixed_time.sec
  )
else
  next_day = date_time.beginning_of_day + 1.day
  new_time = DateTime.new(
    next_day.year,
    next_day.month,
    next_day.day,
    fixed_time.hour,
    fixed_time.min,
    fixed_time.sec
  )
end

# Return new_time

有效,但有更好的方法吗?

【问题讨论】:

    标签: ruby-on-rails ruby datetime ruby-on-rails-4 time


    【解决方案1】:

    我将只构建一次新的日期时间,并在需要时添加 1 天:

    # Given fixed_time and date_time
    new_date_time = DateTime.new(
      date_time.year,
      date_time.month,
      date_time.day,
      fixed_time.hour,
      fixed_time.min,
      fixed_time.sec
    )
    
    # add 1 day if new date prior to the given date
    new_date_time += 1.day if new_date_time < date_time
    

    【讨论】:

      【解决方案2】:

      以下是重构它以消除一些冗余的一点尝试:

      # Given fixed_time and date_time
      
      base_date = date_time.to_date
      if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N")
        base_date = base_date.next_day
      end
      
      new_time = DateTime.new(
        base_date.year,
        base_date.month,
        base_date.day,
        fixed_time.hour,
        fixed_time.min,
        fixed_time.sec
      )
      
      # Return new_time
      

      最大的变化是base_date是在new_time创建之前确定的,这样就可以在那里使用了。

      我还在 DateTime 上使用了next_day 方法来获取第二天,并使用“%T”格式说明符作为“%H:%M:%S”的快捷方式

      这里有一个小测试程序来证明它可以工作:

      require "date"
      
      def next_schedule(fixed_time, date_time)
        # Given fixed_time and date_time
      
        base_date = date_time.to_date
        if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N")
          base_date = base_date.next_day
        end
      
        new_time = DateTime.new(
          base_date.year,
          base_date.month,
          base_date.day,
          fixed_time.hour,
          fixed_time.min,
          fixed_time.sec
        )
      
        # Return new_time
      end
      
      StartTime = DateTime.strptime("2016-02-14 17:15:00", "%F %T")
      Dates = [
        "2016-03-14 16:00:00",
        "2016-03-14 18:00:00"
      ]
      
      Dates.each do |current_date|
        scheduled = next_schedule(StartTime, DateTime.strptime(current_date, "%F %T"))
        puts "Scheduled: #{scheduled.strftime('%F %T')}"
      end
      

      这个的输出是:

      Scheduled: 2016-03-14 17:15:00
      Scheduled: 2016-03-15 17:15:00
      

      它使用了问题中描述的测试用例,并且得到了预期的答案。

      【讨论】:

      • 谢谢!这确实使函数看起来更好更小,尽管方法是相同的。如果我没有以更好的方法得到答案,我肯定会将您的答案标记为已接受的答案。
      • @shashwat 只是回头看看您是否能够找到更好的方法,或者您正在使用的方法是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2013-12-23
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多