Class: On::OnTime
- Inherits:
-
Object
- Object
- On::OnTime
- Defined in:
- lib/ontime/ontime.rb
Constant Summary collapse
- SYSTEM_TIME =
"%a %b %d %H:%M:%S %Y %Z"- ATTR_READERS =
i{ year month date hour min sec }.freeze
Instance Method Summary collapse
- #am ⇒ Object
- #and(given_seconds = 0) ⇒ Object
- #at(time = 0) ⇒ Object
- #in(timezone = 'UTC') ⇒ Object
-
#initialize ⇒ OnTime
constructor
A new instance of OnTime.
- #of(month = Time.now.month, year = Time.now.year) ⇒ Object
- #offset ⇒ Object
- #pm ⇒ Object
- #set(new_state) ⇒ Object
- #to_s ⇒ Object
- #to_time ⇒ Object
Constructor Details
#initialize ⇒ OnTime
Returns a new instance of OnTime.
7 8 9 |
# File 'lib/ontime/ontime.rb', line 7 def initialize() @state = {} end |
Instance Method Details
#am ⇒ Object
39 40 41 42 |
# File 'lib/ontime/ontime.rb', line 39 def am set(hour: hour - 12) if hour > 11 self end |
#and(given_seconds = 0) ⇒ Object
30 31 32 |
# File 'lib/ontime/ontime.rb', line 30 def and(given_seconds=0) set(sec: given_seconds.to_i) end |
#at(time = 0) ⇒ Object
24 25 26 27 28 |
# File 'lib/ontime/ontime.rb', line 24 def at(time=0) time = 0 if time == :midnight time = 12 if time == :midday set(hour: time.to_s.split(':').first.to_i, min: time.to_s.split(':')[1].to_i) end |
#in(timezone = 'UTC') ⇒ Object
57 58 59 60 61 |
# File 'lib/ontime/ontime.rb', line 57 def in(timezone='UTC') system_time = `zdump #{timezone}`.split()[1..-1].join(' ') set(offset: DateTime.strptime(system_time, SYSTEM_TIME).strftime('%Z')) end |
#of(month = Time.now.month, year = Time.now.year) ⇒ Object
20 21 22 |
# File 'lib/ontime/ontime.rb', line 20 def of(month=Time.now.month, year=Time.now.year) set(month: MONTHS.index(month.to_sym) + 1, year: year) end |
#offset ⇒ Object
63 64 65 |
# File 'lib/ontime/ontime.rb', line 63 def offset @state[:offset] end |
#pm ⇒ Object
34 35 36 37 |
# File 'lib/ontime/ontime.rb', line 34 def pm set(hour: hour + 12) if hour < 12 self end |
#set(new_state) ⇒ Object
52 53 54 55 |
# File 'lib/ontime/ontime.rb', line 52 def set(new_state) @state = @state.merge(new_state) self end |
#to_s ⇒ Object
44 45 46 |
# File 'lib/ontime/ontime.rb', line 44 def to_s to_time.to_s end |
#to_time ⇒ Object
48 49 50 |
# File 'lib/ontime/ontime.rb', line 48 def to_time Time.new(year, month, date, hour, min, sec, offset) end |