Class: Puppet::Pops::Time::Timespan
- Defined in:
- lib/puppet/pops/time/timespan.rb
Defined Under Namespace
Classes: Format, FormatParser
Constant Summary
Constants included from LabelProvider
LabelProvider::A, LabelProvider::AN, LabelProvider::SKIPPED_CHARACTERS, LabelProvider::VOWELS
Instance Attribute Summary
Attributes inherited from TimeData
Class Method Summary collapse
- .from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0) ⇒ Object
- .from_fields_hash(hash) ⇒ Object
- .from_hash(hash) ⇒ Object
- .from_string_hash(hash) ⇒ Object
- .parse(str, format = Format::DEFAULTS) ⇒ Object
Instance Method Summary collapse
- #%(o) ⇒ Object
- #*(o) ⇒ Object
- #+(o) ⇒ Object
- #-(o) ⇒ Object
- #-@ ⇒ Object
- #/(o) ⇒ Object
-
#days ⇒ Integer
A positive integer denoting the number of days.
- #div(o) ⇒ Object
- #divmod(o) ⇒ Object
-
#format(format) ⇒ String
Formats this timestamp into a string according to the given ‘format`.
-
#hours ⇒ Integer
A positive integer, 0 - 23 denoting hours of day.
-
#milliseconds ⇒ Integer
A positive integer, 0 - 999 denoting milliseconds of second.
-
#minutes ⇒ Integer
A positive integer, 0 - 59 denoting minutes of hour.
- #modulo(o) ⇒ Object
-
#nanoseconds ⇒ Integer
A positive integer, 0 - 999.999.999 denoting nanoseconds of second.
-
#negative? ⇒ true
If the stored value is negative.
-
#seconds ⇒ Integer
A positive integer, 0 - 59 denoting seconds of minute.
- #to_hash(compact = false) ⇒ Object
-
#to_s ⇒ String
Formats this timestamp into a string according to 0.
- #total_days ⇒ Object private
- #total_hours ⇒ Object private
- #total_microseconds ⇒ Object private
- #total_milliseconds ⇒ Object private
- #total_minutes ⇒ Object private
- #total_nanoseconds ⇒ Object private
- #total_seconds ⇒ Object private
Methods inherited from TimeData
#<=>, #initialize, #label, #to_c, #to_f, #to_i, #to_int, #to_r
Methods included from LabelProvider
#a_an, #a_an_uc, #article, #label, #plural_s, #the, #the_uc
Constructor Details
This class inherits a constructor from Puppet::Pops::Time::TimeData
Class Method Details
.from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0) ⇒ Object
78 79 80 81 |
# File 'lib/puppet/pops/time/timespan.rb', line 78 def self.from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0) ns = (((((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000 + milliseconds) * 1000 + microseconds) * 1000 + nanoseconds new(negative ? -ns : ns) end |
.from_fields_hash(hash) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/puppet/pops/time/timespan.rb', line 91 def self.from_fields_hash(hash) from_fields( hash[KEY_NEGATIVE] || false, hash[KEY_DAYS] || 0, hash[KEY_HOURS] || 0, hash[KEY_MINUTES] || 0, hash[KEY_SECONDS] || 0, hash[KEY_MILLISECONDS] || 0, hash[KEY_MICROSECONDS] || 0, hash[KEY_NANOSECONDS] || 0) end |
.from_hash(hash) ⇒ Object
83 84 85 |
# File 'lib/puppet/pops/time/timespan.rb', line 83 def self.from_hash(hash) hash.include?('string') ? from_string_hash(hash) : from_fields_hash(hash) end |
.from_string_hash(hash) ⇒ Object
87 88 89 |
# File 'lib/puppet/pops/time/timespan.rb', line 87 def self.from_string_hash(hash) parse(hash[KEY_STRING], hash[KEY_FORMAT] || Format::DEFAULTS) end |
.parse(str, format = Format::DEFAULTS) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puppet/pops/time/timespan.rb', line 103 def self.parse(str, format = Format::DEFAULTS) if format.is_a?(::Array) format.each do |fmt| fmt = FormatParser.singleton.parse_format(fmt) unless fmt.is_a?(Format) begin return fmt.parse(str) rescue ArgumentError end end raise ArgumentError, "Unable to parse '#{str}' using any of the formats #{format.join(', ')}" end format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format) format.parse(str) end |
Instance Method Details
#%(o) ⇒ Object
177 178 179 |
# File 'lib/puppet/pops/time/timespan.rb', line 177 def %(o) modulo(o) end |
#*(o) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/puppet/pops/time/timespan.rb', line 153 def *(o) case o when Integer, Float Timespan.new((@nsecs * o).to_i) else raise ArgumentError, "A Timestamp cannot be multiplied by #{a_an(o)}" end end |
#+(o) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/puppet/pops/time/timespan.rb', line 123 def +(o) case o when Timestamp Timestamp.new(@nsecs + o.nsecs) when Timespan Timespan.new(@nsecs + o.nsecs) when Integer, Float # Add seconds Timespan.new(@nsecs + (o * NSECS_PER_SEC).to_i) else raise ArgumentError, "#{a_an_uc(o)} cannot be added to a Timespan" unless o.is_a?(Timespan) end end |
#-(o) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/puppet/pops/time/timespan.rb', line 137 def -(o) case o when Timespan Timespan.new(@nsecs - o.nsecs) when Integer, Float # Subtract seconds Timespan.new(@nsecs - (o * NSECS_PER_SEC).to_i) else raise ArgumentError, "#{a_an_uc(o)} cannot be subtracted from a Timespan" end end |
#-@ ⇒ Object
149 150 151 |
# File 'lib/puppet/pops/time/timespan.rb', line 149 def -@ Timespan.new(-@nsecs) end |
#days ⇒ Integer
Returns a positive integer denoting the number of days.
198 199 200 |
# File 'lib/puppet/pops/time/timespan.rb', line 198 def days total_days end |
#div(o) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/puppet/pops/time/timespan.rb', line 181 def div(o) case o when Timespan # Timespan/Timespan yields a Float @nsecs.fdiv(o.nsecs) when Integer, Float Timespan.new(@nsecs.div(o)) else raise ArgumentError, "A Timespan cannot be divided by #{a_an(o)}" end end |
#divmod(o) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/puppet/pops/time/timespan.rb', line 162 def divmod(o) case o when Integer to_i.divmod(o) when Float to_f.divmod(o) else raise ArgumentError, "Can not do modulus on a Timespan using a #{a_an(o)}" end end |
#format(format) ⇒ String
Formats this timestamp into a string according to the given ‘format`
233 234 235 236 |
# File 'lib/puppet/pops/time/timespan.rb', line 233 def format(format) format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format) format.format(self) end |
#hours ⇒ Integer
Returns a positive integer, 0 - 23 denoting hours of day.
203 204 205 |
# File 'lib/puppet/pops/time/timespan.rb', line 203 def hours total_hours % 24 end |
#milliseconds ⇒ Integer
Returns a positive integer, 0 - 999 denoting milliseconds of second.
218 219 220 |
# File 'lib/puppet/pops/time/timespan.rb', line 218 def milliseconds total_milliseconds % 1000 end |
#minutes ⇒ Integer
Returns a positive integer, 0 - 59 denoting minutes of hour.
208 209 210 |
# File 'lib/puppet/pops/time/timespan.rb', line 208 def minutes total_minutes % 60 end |
#modulo(o) ⇒ Object
173 174 175 |
# File 'lib/puppet/pops/time/timespan.rb', line 173 def modulo(o) divmod(o)[1] end |
#nanoseconds ⇒ Integer
Returns a positive integer, 0 - 999.999.999 denoting nanoseconds of second.
223 224 225 |
# File 'lib/puppet/pops/time/timespan.rb', line 223 def nanoseconds total_nanoseconds % NSECS_PER_SEC end |
#negative? ⇒ true
Returns if the stored value is negative.
119 120 121 |
# File 'lib/puppet/pops/time/timespan.rb', line 119 def negative? @nsecs < 0 end |
#seconds ⇒ Integer
Returns a positive integer, 0 - 59 denoting seconds of minute.
213 214 215 |
# File 'lib/puppet/pops/time/timespan.rb', line 213 def seconds total_seconds % 60 end |
#to_hash(compact = false) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/puppet/pops/time/timespan.rb', line 246 def to_hash(compact = false) result = {} n = nanoseconds if compact s = total_seconds result[KEY_SECONDS] = negative? ? -s : s result[KEY_NANOSECONDS] = negative? ? -n : n unless n == 0 else add_unless_zero(result, KEY_DAYS, days) add_unless_zero(result, KEY_HOURS, hours) add_unless_zero(result, KEY_MINUTES, minutes) add_unless_zero(result, KEY_SECONDS, seconds) unless n == 0 add_unless_zero(result, KEY_NANOSECONDS, n % 1000) n /= 1000 add_unless_zero(result, KEY_MICROSECONDS, n % 1000) add_unless_zero(result, KEY_MILLISECONDS, n /= 1000) end result[KEY_NEGATIVE] = true if negative? end result end |
#to_s ⇒ String
Formats this timestamp into a string according to 0
242 243 244 |
# File 'lib/puppet/pops/time/timespan.rb', line 242 def to_s format(Format::DEFAULTS[0]) end |
#total_days ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
275 276 277 |
# File 'lib/puppet/pops/time/timespan.rb', line 275 def total_days total_nanoseconds / NSECS_PER_DAY end |
#total_hours ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
280 281 282 |
# File 'lib/puppet/pops/time/timespan.rb', line 280 def total_hours total_nanoseconds / NSECS_PER_HOUR end |
#total_microseconds ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
300 301 302 |
# File 'lib/puppet/pops/time/timespan.rb', line 300 def total_microseconds total_nanoseconds / NSECS_PER_USEC end |
#total_milliseconds ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
295 296 297 |
# File 'lib/puppet/pops/time/timespan.rb', line 295 def total_milliseconds total_nanoseconds / NSECS_PER_MSEC end |
#total_minutes ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
285 286 287 |
# File 'lib/puppet/pops/time/timespan.rb', line 285 def total_minutes total_nanoseconds / NSECS_PER_MIN end |
#total_nanoseconds ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
305 306 307 |
# File 'lib/puppet/pops/time/timespan.rb', line 305 def total_nanoseconds @nsecs.abs end |
#total_seconds ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
290 291 292 |
# File 'lib/puppet/pops/time/timespan.rb', line 290 def total_seconds total_nanoseconds / NSECS_PER_SEC end |