Determining the time between NOW (local time) and subsequent DST change in [Ruby]
In Ruby, we can find the current time in a specific time zone and determine if it is currently DST in that time zone.
pry(main)> t = Time.now.in_time_zone('America/Los_Angeles')
=> Tue, 02 Sep 2014 18:14:25 PDT -07:00
pry(main)> t.dst?
=> true
I am looking to find the time difference between t
and on next change t.dst?
.
Is there a way to figure out when the next DST change occurs?
# pseudo code
dst_time = # Time when next DST occurs
local_time = Time.now.in_time_zone('America/Los_Angeles')
time_till_dst_change = dst_time - local_time
+3
source to share
1 answer
You can get this information from the tzinfo
gem. It can either parse the system timezone files or use the data from the gem tzinfo-data
.
#!/usr/bin/env ruby
require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
puts "Next timezone change is at #{tz.current_period.end_transition.datetime}"
Prints:
Next timezone change is at 2014-11-02T06:00:00+00:00
+4
source to share