Class: Async::Cron::Time

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/async/cron/time.rb

Overview

A base-zero that supports assigning arbitrary values (both positive and negative) to all units. This simplifies computing relative times and dates by incrementing the relevant units and normalizing the result.

**Note that base-zero means that the month and day start from zero, not one, as is the case with the standard Ruby Time and Date classes.** That is because the fields also accept negative values, which would be ambiguous if the month and day started from one.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(years, months, days, hours, minutes, seconds, offset) ⇒ Time

Returns a new instance of Time.



40
41
42
43
44
45
46
47
48
# File 'lib/async/cron/time.rb', line 40

def initialize(years, months, days, hours, minutes, seconds, offset)
  @years = years
  @months = months
  @days = days
  @hours = hours
  @minutes = minutes
  @seconds = seconds
  @offset = offset
end

Instance Attribute Details

#daysObject

Returns the value of attribute days.



66
67
68
# File 'lib/async/cron/time.rb', line 66

def days
  @days
end

#hoursObject

Returns the value of attribute hours.



68
69
70
# File 'lib/async/cron/time.rb', line 68

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



69
70
71
# File 'lib/async/cron/time.rb', line 69

def minutes
  @minutes
end

#monthsObject

Returns the value of attribute months.



65
66
67
# File 'lib/async/cron/time.rb', line 65

def months
  @months
end

#offsetObject

Returns the value of attribute offset.



72
73
74
# File 'lib/async/cron/time.rb', line 72

def offset
  @offset
end

#secondsObject

Returns the value of attribute seconds.



70
71
72
# File 'lib/async/cron/time.rb', line 70

def seconds
  @seconds
end

#yearsObject

Returns the value of attribute years.



64
65
66
# File 'lib/async/cron/time.rb', line 64

def years
  @years
end

Class Method Details

.from(time) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/async/cron/time.rb', line 16

def self.from(time)
  case time
  when ::Time
    return self.new(time.year, time.month-1, time.day-1, time.hour, time.min, time.sec, time.utc_offset)
  when ::DateTime
    return self.new(time.year, time.month-1, time.day-1, time.hour, time.min, time.sec, time.offset)
  when ::Date
    return self.new(time.year, time.month-1, time.day-1, 0, 0, 0, 0)
  when self
    return time.dup
  end
end

.nowObject



29
30
31
# File 'lib/async/cron/time.rb', line 29

def self.now
  return self.from(::Time.now)
end

Instance Method Details

#<=>(other) ⇒ Object



94
95
96
# File 'lib/async/cron/time.rb', line 94

def <=> other
  to_a <=> other.to_a
end

#deltaObject



106
107
108
# File 'lib/async/cron/time.rb', line 106

def delta
  self.to_time - ::Time.now
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/async/cron/time.rb', line 102

def eql? other
  to_a.eql?(other.to_a)
end

#freezeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/async/cron/time.rb', line 50

def freeze
  return self if frozen?
  
  @years.freeze
  @months.freeze
  @days.freeze
  @hours.freeze
  @minutes.freeze
  @seconds.freeze
  @offset.freeze
  
  super
end

#hashObject



98
99
100
# File 'lib/async/cron/time.rb', line 98

def hash
  to_a.hash
end

#monthday=(value) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/async/cron/time.rb', line 82

def monthday=(value)
  if value >= 0
    @days = value
  else
    @days = ::Date.new(@years, @months+1, value).day
  end
end

#normalize!Object



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
153
154
155
# File 'lib/async/cron/time.rb', line 128

def normalize!
  seconds = self.seconds
  delta_minutes = seconds / 60
  seconds = seconds % 60
  
  minutes = self.minutes + delta_minutes
  delta_hours = minutes / 60
  minutes = minutes % 60
  
  hours = self.hours + delta_hours
  delta_days = hours / 24
  hours = hours % 24
  
  days = self.days + delta_days
  months = self.months
  years = self.years
  
  date = (Date.new(years, 1, 1) >> months) + days
  
  @years = date.year
  @months = date.month - 1
  @days = date.day - 1
  @hours = hours
  @minutes = minutes
  @seconds = seconds
  
  return self
end

#to_aObject



90
91
92
# File 'lib/async/cron/time.rb', line 90

def to_a
  [@years, @months, @days, @hours, @minutes, @seconds, @offset]
end

#to_dateObject



122
123
124
125
126
# File 'lib/async/cron/time.rb', line 122

def to_date
  normalized = self.dup.normalize!
  
  return ::Date.new(normalized.years, normalized.months+1, normalized.days+1)
end

#to_datetimeObject



116
117
118
119
120
# File 'lib/async/cron/time.rb', line 116

def to_datetime
  normalized = self.dup.normalize!
  
  return ::DateTime.new(normalized.years, normalized.months+1, normalized.days+1, normalized.hours, normalized.minutes, normalized.seconds, normalized.offset)
end

#to_sObject



157
158
159
# File 'lib/async/cron/time.rb', line 157

def to_s
  sprintf("%04d+%02d+%02d %02d:%02d:%02d %d", years, months, days, hours, minutes, seconds, offset)
end

#to_timeObject



110
111
112
113
114
# File 'lib/async/cron/time.rb', line 110

def to_time
  normalized = self.dup.normalize!
  
  return ::Time.new(normalized.years, normalized.months+1, normalized.days+1, normalized.hours, normalized.minutes, normalized.seconds, normalized.offset)
end

#weekdayObject



74
75
76
# File 'lib/async/cron/time.rb', line 74

def weekday
  to_date.wday
end

#weekday=(value) ⇒ Object



78
79
80
# File 'lib/async/cron/time.rb', line 78

def weekday=(value)
  @days += value - weekday
end