Class: Seasonal::Event
- Inherits:
-
Object
- Object
- Seasonal::Event
- Defined in:
- lib/seasonal.rb
Instance Attribute Summary collapse
-
#ennd ⇒ Object
Returns the value of attribute ennd.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#start ⇒ Object
Returns the value of attribute start.
-
#zone ⇒ Object
Returns the value of attribute zone.
Instance Method Summary collapse
- #change_year(time, new_year) ⇒ Object
- #end_utc ⇒ Object
- #going_on?(test_time = Time.now) ⇒ Boolean
- #going_on_yearly_range(test_time) ⇒ Object
- #has_year(s) ⇒ Object
-
#initialize(payload, zone, options = {}) ⇒ Event
constructor
A new instance of Event.
- #is_yearly_range ⇒ Object
- #start_utc ⇒ Object
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, ={}) @payload = payload @zone = zone if [:on] @start = "#{[:on]} 00:00:00" @ennd = "#{[:on]} 23:59:59" else @start = [:start] @ennd = [:end] end end |
Instance Attribute Details
#ennd ⇒ Object
Returns the value of attribute ennd.
12 13 14 |
# File 'lib/seasonal.rb', line 12 def ennd @ennd end |
#payload ⇒ Object
Returns the value of attribute payload.
9 10 11 |
# File 'lib/seasonal.rb', line 9 def payload @payload end |
#start ⇒ Object
Returns the value of attribute start.
11 12 13 |
# File 'lib/seasonal.rb', line 11 def start @start end |
#zone ⇒ Object
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_utc ⇒ Object
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
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_range ⇒ Object
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_utc ⇒ Object
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 |