Class: Seasonal::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/seasonal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, zone, options = {}) ⇒ Event

Returns a new instance of Event.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/seasonal.rb', line 14

def initialize(payload, zone, options={})
  @payload = payload
  @zone = zone
  if options[:on]
    @start = "#{options[:on]} 00:00:00"
    @ennd = "#{options[:on]} 23:59:59"
  else
    @start = options[:start]
    @ennd = options[:end]
  end
end

Instance Attribute Details

#enndObject

Returns the value of attribute ennd.



12
13
14
# File 'lib/seasonal.rb', line 12

def ennd
  @ennd
end

#payloadObject

Returns the value of attribute payload.



9
10
11
# File 'lib/seasonal.rb', line 9

def payload
  @payload
end

#startObject

Returns the value of attribute start.



11
12
13
# File 'lib/seasonal.rb', line 11

def start
  @start
end

#zoneObject

Returns the value of attribute zone.



10
11
12
# File 'lib/seasonal.rb', line 10

def zone
  @zone
end

Instance Method Details

#change_year(time, new_year) ⇒ Object



49
50
51
52
# File 'lib/seasonal.rb', line 49

def change_year(time, new_year)
  Time.utc(new_year, time.utc.month, time.utc.day, time.utc.hour,
    time.utc.min, time.utc.sec, time.utc.usec)
end

#end_utcObject



33
34
35
36
37
38
# File 'lib/seasonal.rb', line 33

def end_utc
  unless ennd.nil?
    tz = TZInfo::Timezone.get(zone)
    tz.local_to_utc(Time.parse(ennd))
  end
end

#going_on?(test_time = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/seasonal.rb', line 70

def going_on?(test_time=Time.now)
  # puts "#{start_utc} - #{end_utc}"
  if start_utc.nil?
    if end_utc.nil?
      result = false
    else
      result = test_time <= end_utc
    end
  else
    if end_utc.nil?
      result = test_time >= start_utc
    else
      result = going_on_yearly_range(test_time)
      if result.nil?
        result = (start_utc <= test_time and test_time <= end_utc)
      end
    end
  end
  result
end

#going_on_yearly_range(test_time) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/seasonal.rb', line 54

def going_on_yearly_range(test_time)
  if is_yearly_range
    start_shifted = change_year(start_utc, test_time.utc.year)
    end_shifted = change_year(end_utc, test_time.utc.year)
    if (end_utc < start_utc)
      start_shifted_prev = change_year(start_shifted,
        start_shifted.year - 1)
      end_shifted_next = change_year(end_shifted, end_shifted.year + 1)
      ((start_shifted_prev <= test_time and test_time <= end_shifted) or
        (start_shifted <= test_time and test_time <= end_shifted_next))
    else
      (start_shifted <= test_time and test_time <= end_shifted)
    end
  end
end

#has_year(s) ⇒ Object



40
41
42
43
# File 'lib/seasonal.rb', line 40

def has_year(s)
  p = Time.parse(s)
  p.eql?(Time.parse(s + ' ' + (p.year + 1).to_s))
end

#is_yearly_rangeObject



45
46
47
# File 'lib/seasonal.rb', line 45

def is_yearly_range
  !start.nil? and !has_year(start) and !ennd.nil? and !has_year(ennd)
end

#start_utcObject



26
27
28
29
30
31
# File 'lib/seasonal.rb', line 26

def start_utc
  unless start.nil?
    tz = TZInfo::Timezone.get(zone)
    tz.local_to_utc(Time.parse(start))
  end
end