【问题标题】:Ruby method that takes todays date and if it falls in between DST (daylight savings time) dates, then return the date of that next change采用今天日期的 Ruby 方法,如果它在 DST(夏令时)日期之间,则返回下一次更改的日期
【发布时间】:2019-02-28 04:18:39
【问题描述】:

我正在尝试编写一个采用今天日期的方法,如果提供的日期介于 2018 年 DST 日期之间(如果日期是 03/11/18 2:00am 和 11/04/18 2:00am,则 DST 为真),然后它将返回下一个相应 DST 更改的日期。

我实际上不知道如何处理这个问题,除了获取提供的日期并围绕它编写一个案例陈述,遍历提供的日期的给定年份。并且每个时间持有不同的年份

# method to take todays date and if the date falls in between DST dates, 
# then the method will return the date of the next DST change

def dst_date_change(date)
    return case 
        when date.include? ='2018'
            if (date > Time.parse('March 11, 2018 2:00am') &&  (date < Time.parse('November 4, 2018 2:00am'))
    end
        when date.include? ='2019'
            if 

    end
        when date.include? ='2020'
        if
    end
        when date.include? ='2020'
        if  
    end
    else 

这是我目前拥有的。显然未完成..

【问题讨论】:

  • 日期 不包括时区数据。您是指Time 还是DateTime?还是您想为默认时区处理这个问题,因为不同国家/地区的 dst 不同?无论如何,我建议您看看Daylight Saving Time start and end dates in Ruby/Rails
  • 我正在尝试使用 DateTime,这也需要通用..
  • 基本上我需要获取系统的当前日期时间,并且我需要查看它是否比另一个更接近一个夏令时。但是,DST 每年都在变化,这对我来说很棘手。
  • 您需要澄清您的问题(通过编辑)。请参阅我的回答以获取建议。

标签: ruby date dst


【解决方案1】:

我对问题的解释如下:“给定一个日期(可能是今天的日期),如果那天没有时间是 DST,则返回 nil。否则,返回下一个日期(可能是同一日期)以 DST 开始,以非 DST 结束”。

require 'date'

def next_dst_date_change(base_date)
  base_date_time = base_date.to_time
  next_date_time = (base_date+1).to_time
  if base_date_time.dst? || next_date_time.dst?
    base_date + 1.step.find { |n| (base_date + n).to_time.dst? == false } - 1
  end
end

请参阅Date#to_timeTime#dst?Numeric#stepEnumerable#find

示例

d0 = Date.today
  #=> #<Date: 2018-09-24 ((2458386j,0s,0n),+0s,2299161j)>
next_dst_date_change(d0)
  #=> #<Date: 2018-11-04 ((2458427j,0s,0n),+0s,2299161j)>

d1 = Date.new(2018, 11, 04)
  #=> #<Date: 2018-11-04 ((2458427j,0s,0n),+0s,2299161j)>
next_dst_date_change(d0)
  #=> #<Date: 2018-11-04 ((2458427j,0s,0n),+0s,2299161j)>

d2 = d0 + 90
  #=> #<Date: 2018-12-23 ((2458476j,0s,0n),+0s,2299161j)>
next_dst_date_change(d2)
  #=> nil

d3 = d0 + 365
  #=> #<Date: 2019-09-24 ((2458751j,0s,0n),+0s,2299161j)>
next_dst_date_change(d3)
  #=> #<Date: 2019-11-03 ((2458791j,0s,0n),+0s,2299161j)>

d4 = Date.new(2018, 3, 10)
  #=> #<Date: 2018-03-10 ((2458188j,0s,0n),+0s,2299161j)>
next_dst_date_change(d4)
  #=> nil

d5 = Date.new(2018, 3, 11)
next_dst_date_change(d5)
  #=> #<Date: 2018-11-04 ((2458427j,0s,0n),+0s,2299161j)>

为了加快速度,可以使用Range#bsearch 进行二分搜索以确定下一个 DST 到非 DST 更改日期。我假设 DST 在每年 12 月 1 日之前的某个时间结束。

def next_dst_date_change(base_date)
  base_date_time = base_date.to_time
  next_date_time = (base_date+1).to_time
  if base_date_time.dst? || next_date_time.dst?
    diff = (Date.new(base_date.year, 12, 1) - base_date).to_i
    base_date + (1..diff).bsearch { |n| !(base_date + n).to_time.dst? } - 1
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多