lib/rufus/scheduler/zotime.rb in rufus-scheduler-3.2.2 vs lib/rufus/scheduler/zotime.rb in rufus-scheduler-3.3.0
- old
+ new
@@ -20,148 +20,430 @@
# THE SOFTWARE.
#
# Made in Japan.
#++
-require 'rufus/scheduler/zones'
-
class Rufus::Scheduler
#
# Zon{ing|ed}Time, whatever.
#
class ZoTime
- attr_accessor :seconds
- attr_accessor :zone
+ attr_reader :seconds
+ attr_reader :zone
def initialize(s, zone)
@seconds = s.to_f
- @zone = zone
- end
+ @zone = self.class.get_tzone(zone || :current)
- def time
+ fail ArgumentError.new(
+ "cannot determine timezone from #{zone.inspect}"
+ ) unless @zone
- in_zone do
+ @time = nil # cache for #to_time result
+ end
- t = Time.at(@seconds)
+ def seconds=(f)
- #if t.isdst
- # t1 = Time.at(@seconds + 3600)
- # t = t1 if t.zone != t1.zone && t.hour == t1.hour && t.min == t1.min
- # # ambiguous TZ (getting out of DST)
- #else
- # t.hour # force t to compute itself
- #end
- #
- # jump out of DST as soon as possible, jumps 1h as seen from UTC
+ @time = nil
+ @seconds = f
+ end
- t.hour # force t to compute itself
- #
- # stay in DST as long as possible, no jump seen from UTC
+ def zone=(z)
- t
- end
+ @time = nil
+ @zone = self.class.get_tzone(zone || :current)
end
def utc
- time.utc
+ Time.utc(1970, 1, 1) + @seconds
end
- def add(s)
+ # Returns a Ruby Time instance.
+ #
+ # Warning: the timezone of that Time instance will be UTC.
+ #
+ def to_time
- @seconds += s.to_f
+ @time ||= begin; u = utc; @zone.period_for_utc(u).to_local(u); end
end
- def substract(s)
+ %w[
+ year month day wday hour min sec usec asctime
+ ].each do |m|
+ define_method(m) { to_time.send(m) }
+ end
+ def iso8601(fraction_digits=0); to_time.iso8601(fraction_digits); end
- @seconds -= s.to_f
+ def ==(o)
+
+ o.is_a?(ZoTime) && o.seconds == @seconds && o.zone == @zone
end
+ #alias eq? == # FIXME see Object#== (ri)
+ def >(o); @seconds > _to_f(o); end
+ def >=(o); @seconds >= _to_f(o); end
+ def <(o); @seconds < _to_f(o); end
+ def <=(o); @seconds <= _to_f(o); end
+ def <=>(o); @seconds <=> _to_f(o); end
+
+ alias getutc utc
+ alias getgm utc
+
+ def to_i
+
+ @seconds.to_i
+ end
+
def to_f
@seconds
end
- #DELTA_TZ_REX = /\A[+-][0-1][0-9]:?[0-5][0-9]\z/
+ def is_dst?
- def self.envtzable?(s)
+ @zone.period_for_utc(utc).std_offset != 0
+ end
+ alias isdst is_dst?
- TIMEZONES.include?(s)
+ def utc_offset
+
+ #@zone.period_for_utc(utc).utc_offset
+ #@zone.period_for_utc(utc).utc_total_offset
+ #@zone.period_for_utc(utc).std_offset
+ @zone.period_for_utc(utc).utc_offset
end
+ def strftime(format)
+
+ format = format.gsub(/%(\/?Z|:{0,2}z)/) { |f| strfz(f) }
+
+ to_time.strftime(format)
+ end
+
+ def add(t); @time = nil; @seconds += t.to_f; end
+ def substract(t); @time = nil; @seconds -= t.to_f; end
+
+ def +(t); inc(t, 1); end
+ def -(t); inc(t, -1); end
+
+ WEEK_S = 7 * 24 * 3600
+
+ def monthdays
+
+ date = to_time
+
+ pos = 1
+ d = self.dup
+
+ loop do
+ d.add(-WEEK_S)
+ break if d.month != date.month
+ pos = pos + 1
+ end
+
+ neg = -1
+ d = self.dup
+
+ loop do
+ d.add(WEEK_S)
+ break if d.month != date.month
+ neg = neg - 1
+ end
+
+ [ "#{date.wday}##{pos}", "#{date.wday}##{neg}" ]
+ end
+
+ def to_s
+
+ strftime('%Y-%m-%d %H:%M:%S %z')
+ end
+
+ def to_debug_s
+
+ uo = self.utc_offset
+ uos = uo < 0 ? '-' : '+'
+ uo = uo.abs
+ uoh, uom = [ uo / 3600, uo % 3600 ]
+
+ [
+ 'zt',
+ self.strftime('%Y-%m-%d %H:%M:%S'),
+ "%s%02d:%02d" % [ uos, uoh, uom ],
+ "dst:#{self.isdst}"
+ ].join(' ')
+ end
+
+ # Debug current time by showing local time / delta / utc time
+ # for example: "0120-7(0820)"
+ #
+ def to_utc_comparison_s
+
+ per = @zone.period_for_utc(utc)
+ off = per.utc_total_offset
+
+ off = off / 3600
+ off = off >= 0 ? "+#{off}" : off.to_s
+
+ strftime('%H%M') + off + utc.strftime('(%H%M)')
+ end
+
+ def self.now(zone=nil)
+
+ ZoTime.new(Time.now.to_f, zone)
+ end
+
+ # https://en.wikipedia.org/wiki/ISO_8601
+ # Postel's law applies
+ #
+ def self.extract_iso8601_zone(s)
+
+ m = s.match(
+ /[0-2]\d(?::?[0-6]\d(?::?[0-6]\d))?\s*([+-]\d\d(?::?\d\d)?)\s*\z/)
+ return nil unless m
+
+ zs = m[1].split(':')
+ zs << '00' if zs.length < 2
+
+ zh = zs[0].to_i.abs
+
+ return nil if zh > 24
+ return nil if zh == 24 && zs[1].to_i != 0
+
+ zs.join(':')
+ end
+
def self.parse(str, opts={})
if defined?(::Chronic) && t = ::Chronic.parse(str, opts)
- return ZoTime.new(t, ENV['TZ'])
+ return ZoTime.new(t, nil)
end
+ #rold = RUBY_VERSION < '1.9.0'
+ #rold = RUBY_VERSION < '2.0.0'
+
begin
DateTime.parse(str)
rescue
- fail ArgumentError, "no time information in #{o.inspect}"
- end if RUBY_VERSION < '1.9.0'
+ fail ArgumentError, "no time information in #{str.inspect}"
+ end #if rold
+ #
+ # is necessary since Time.parse('xxx') in Ruby < 1.9 yields `now`
zone = nil
s =
- str.gsub(/\S+/) { |m|
- if envtzable?(m)
- zone ||= m
+ str.gsub(/\S+/) do |w|
+ if z = get_tzone(w)
+ zone ||= z
''
else
- m
+ w
end
- }
+ end
- return nil unless zone.nil? || is_timezone?(zone)
+ local = Time.parse(s)
+ izone = extract_iso8601_zone(s)
- zt = ZoTime.new(0, zone || ENV['TZ'])
- zt.in_zone { zt.seconds = Time.parse(s).to_f }
+ zone ||=
+ if s.match(/\dZ\b/)
+ get_tzone('Zulu')
+ #elsif rold && izone
+ elsif izone
+ get_tzone(izone)
+ elsif local.zone.nil? && izone
+ get_tzone(local.strftime('%:z'))
+ else
+ get_tzone(:local)
+ end
- zt.seconds == nil ? nil : zt
+ secs =
+ #if rold && izone
+ if izone
+ local.to_f
+ else
+ zone.period_for_local(local).to_utc(local).to_f
+ end
+
+ ZoTime.new(secs, zone)
end
- def self.is_timezone?(str)
+ def self.get_tzone(str)
- return false if str == nil
- return false if str == '*'
+ return str if str.is_a?(::TZInfo::Timezone)
- return false if str.index('#')
- # "sun#2", etc... On OSX would go all the way to true
+ # discard quickly when it's certainly not a timezone
- return true if Time.zone_offset(str)
+ return nil if str == nil
+ return nil if str == '*'
- return !! (::TZInfo::Timezone.get(str) rescue nil) if defined?(::TZInfo)
+ # ok, it's a timezone then
- return true if TIMEZONES.include?(str)
- return true if TIMEZONEs.include?(str)
+ str = Time.now.zone if str == :current || str == :local
- t = ZoTime.new(0, str).time
+ # utc_offset
- return false if t.zone == ''
- return false if t.zone == 'UTC'
- return false if t.utc_offset == 0 && str.start_with?(t.zone)
- # 3 common fallbacks...
+ if str.is_a?(Numeric)
+ i = str.to_i
+ sn = i < 0 ? '-' : '+'; i = i.abs
+ hr = i / 3600; mn = i % 3600; sc = i % 60
+ str = (sc > 0 ? "%s%02d:%02d:%02d" : "%s%02d:%02d") % [ sn, hr, mn, sc ]
+ end
- return false if RUBY_PLATFORM.include?('java') && ! envtzable?(str)
+ return nil if str.index('#')
+ # counters "sun#2", etc... On OSX would go all the way to true
- true
+ # vanilla time zones
+
+ z = (::TZInfo::Timezone.get(str) rescue nil)
+ return z if z
+
+ # time zone abbreviations
+
+ if str.match(/\A[A-Z0-9-]{3,6}\z/)
+
+ twin = Time.utc(Time.now.year, 1, 1)
+ tsum = Time.utc(Time.now.year, 7, 1)
+
+ z =
+ ::TZInfo::Timezone.all.find do |tz|
+ tz.period_for_utc(twin).abbreviation.to_s == str ||
+ tz.period_for_utc(tsum).abbreviation.to_s == str
+ end
+ return z if z
+ end
+
+ # some time zone aliases
+
+ return ::TZInfo::Timezone.get('Zulu') if %w[ Z ].include?(str)
+
+ # custom timezones, no DST, just an offset, like "+08:00" or "-01:30"
+
+ tz = (@custom_tz_cache ||= {})[str]
+ return tz if tz
+
+ if m = str.match(/\A([+-][0-1][0-9]):?([0-5][0-9])\z/)
+
+ hr = m[1].to_i
+ mn = m[2].to_i
+
+ hr = nil if hr.abs > 11
+ hr = nil if mn > 59
+ mn = -mn if hr && hr < 0
+
+ return (
+ @custom_tz_cache[str] =
+ begin
+ tzi = TZInfo::TransitionDataTimezoneInfo.new(str)
+ tzi.offset(str, hr * 3600 + mn * 60, 0, str)
+ tzi.create_timezone
+ end
+ ) if hr
+ end
+
+ # so it's not a timezone.
+
+ nil
end
- def in_zone(&block)
+ def self.local_tzone
- current_timezone = ENV['TZ']
- ENV['TZ'] = @zone
+ get_tzone(:local)
+ end
- block.call
+ def self.make(o)
- ensure
+ zt =
+ case o
+ when Time
+ ZoTime.new(o.to_f, o.zone)
+ when Date
+ t =
+ o.respond_to?(:to_time) ?
+ o.to_time :
+ Time.parse(o.strftime('%Y-%m-%d %H:%M:%S'))
+ ZoTime.new(t.to_f, t.zone)
+ when String
+ Rufus::Scheduler.parse_in(o, :no_error => true) || self.parse(o)
+ else
+ o
+ end
- ENV['TZ'] = current_timezone
+ zt = ZoTime.new(Time.now.to_f + zt, nil) if zt.is_a?(Numeric)
+
+ fail ArgumentError.new(
+ "cannot turn #{o.inspect} to a ZoTime instance"
+ ) unless zt.is_a?(ZoTime)
+
+ zt
+ end
+
+# def in_zone(&block)
+#
+# current_timezone = ENV['TZ']
+# ENV['TZ'] = @zone
+#
+# block.call
+#
+# ensure
+#
+# ENV['TZ'] = current_timezone
+# end
+
+ protected
+
+ def inc(t, dir)
+
+ if t.is_a?(Numeric)
+ nt = self.dup
+ nt.seconds += dir * t.to_f
+ nt
+ elsif t.respond_to?(:to_f)
+ @seconds + dir * t.to_f
+ else
+ fail ArgumentError.new(
+ "cannot call ZoTime #- or #+ with arg of class #{t.class}")
+ end
+ end
+
+ def _to_f(o)
+
+ fail ArgumentError(
+ "comparison of ZoTime with #{o.inspect} failed"
+ ) unless o.is_a?(ZoTime) || o.is_a?(Time)
+
+ o.to_f
+ end
+
+ def strfz(code)
+
+ return @zone.name if code == '%/Z'
+
+ per = @zone.period_for_utc(utc)
+
+ return per.abbreviation.to_s if code == '%Z'
+
+ off = per.utc_total_offset
+ #
+ sn = off < 0 ? '-' : '+'; off = off.abs
+ hr = off / 3600
+ mn = (off % 3600) / 60
+ sc = 0
+
+ fmt =
+ if code == '%z'
+ "%s%02d%02d"
+ elsif code == '%:z'
+ "%s%02d:%02d"
+ else
+ "%s%02d:%02d:%02d"
+ end
+
+ fmt % [ sn, hr, mn, sc ]
end
end
end