Class: Rufus::Scheduler::ZoTime

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/scheduler/zotime.rb

Overview

Zoning|edTime, whatever.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, zone) ⇒ ZoTime

Returns a new instance of ZoTime.



38
39
40
41
42
# File 'lib/rufus/scheduler/zotime.rb', line 38

def initialize(s, zone)

  @seconds = s.to_f
  @zone = zone
end

Instance Attribute Details

#secondsObject

Returns the value of attribute seconds.



35
36
37
# File 'lib/rufus/scheduler/zotime.rb', line 35

def seconds
  @seconds
end

#zoneObject

Returns the value of attribute zone.



36
37
38
# File 'lib/rufus/scheduler/zotime.rb', line 36

def zone
  @zone
end

Class Method Details

.envtzable?(s) ⇒ Boolean

DELTA_TZ_REX = /^[+-][0-1]:?[0-5]$/

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/rufus/scheduler/zotime.rb', line 90

def self.envtzable?(s)

  TIMEZONES.include?(s)
end

.is_timezone?(str) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rufus/scheduler/zotime.rb', line 127

def self.is_timezone?(str)

  return false if str == nil
  return false if str == '*'

  return false if str.index('#')
    # "sun#2", etc... On OSX would go all the way to true

  return true if Time.zone_offset(str)

  return !! (::TZInfo::Timezone.get(str) rescue nil) if defined?(::TZInfo)

  return true if TIMEZONES.include?(str)
  return true if TIMEZONEs.include?(str)

  t = ZoTime.new(0, str).time

  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...

  return false if RUBY_PLATFORM.include?('java') && ! envtzable?(str)

  true
end

.parse(str, opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rufus/scheduler/zotime.rb', line 95

def self.parse(str, opts={})

  if defined?(::Chronic) && t = ::Chronic.parse(str, opts)
    return ZoTime.new(t, ENV['TZ'])
  end

  begin
    DateTime.parse(str)
  rescue
    raise ArgumentError, "no time information in #{o.inspect}"
  end if RUBY_VERSION < '1.9.0'

  zone = nil

  s =
    str.gsub(/\S+/) { |m|
      if envtzable?(m)
        zone ||= m
        ''
      else
        m
      end
    }

  return nil unless zone.nil? || is_timezone?(zone)

  zt = ZoTime.new(0, zone || ENV['TZ'])
  zt.in_zone { zt.seconds = Time.parse(s).to_f }

  zt.seconds == nil ? nil : zt
end

Instance Method Details

#add(s) ⇒ Object



73
74
75
76
# File 'lib/rufus/scheduler/zotime.rb', line 73

def add(s)

  @seconds += s.to_f
end

#in_zone(&block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rufus/scheduler/zotime.rb', line 154

def in_zone(&block)

  current_timezone = ENV['TZ']
  ENV['TZ'] = @zone

  block.call

ensure

  ENV['TZ'] = current_timezone
end

#substract(s) ⇒ Object



78
79
80
81
# File 'lib/rufus/scheduler/zotime.rb', line 78

def substract(s)

  @seconds -= s.to_f
end

#timeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rufus/scheduler/zotime.rb', line 44

def time

  in_zone do

    t = Time.at(@seconds)

    #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

    t.hour # force t to compute itself
      #
      # stay in DST as long as possible, no jump seen from UTC

    t
  end
end

#to_fObject



83
84
85
86
# File 'lib/rufus/scheduler/zotime.rb', line 83

def to_f

  @seconds
end

#utcObject



68
69
70
71
# File 'lib/rufus/scheduler/zotime.rb', line 68

def utc

  time.utc
end