获取下一个夏时制更改

获取下一个夏时制更改

本文介绍了Ruby-获取下一个夏时制更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种方法可以确定夏令时是否是某个时间( Time.now.dst?),但是有没有一种方法可以给我们

I know there is a method to determine if a certain time is on Daylight Savings Time (Time.now.dst?) but is there a method to give us the next date when Daylight Savings will change?

例如,Google将 11月1日星期日作为下一个夏令时的更改日期? 2015年的时间更改。

For example, Google returns Sunday, November 1 as the next Daylight Savings Time change in 2015.

推荐答案

由于这些日期是基于其他值的,例如您使用的时区,因此需要像ActiveSupport / TZInfo这样的模块。

Since these are dates that are based on other values, like the timezone you are working with, it requires a module like ActiveSupport/TZInfo.

require 'active_support/core_ext/time/zones'
tz = TZInfo::Timezone.get('US/Pacific')
     # pick a timezone to work with
tz.current_period     #returns an object of the current period

  => #<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition:

     #<TZInfo::TimeOrDateTime: 1425808800>,#<TZInfo::TimezoneOffset: -28800,3600,PDT>>,

     #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1446368400>,

     #<TZInfo::TimezoneOffset: -28800,0,PST>>>

tz.current_period.local_start.to_s
 # => "2015-03-08T03:00:00+00:00"
tz.current_period.local_end.to_s
 #  => "2015-11-01T02:00:00+00:00"

我还没有做过的一件事知道是因为常规的Ruby Core会这样做:

One thing I haven't figured out is that since regular Ruby Core does this:

Time.now.dst?
   # => true

在哪里获取此信息?我通过ActiveSupport找到了TZInfo类。 Ruby是否只是从操作系统中获取布尔值?

Where is it getting this info? I found the TZInfo classes through ActiveSupport. Is Ruby just getting a boolean value from the OS?

这篇关于Ruby-获取下一个夏时制更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:59