Class: Puppet::Pops::Time::Timespan
Defined Under Namespace
Classes: Format, FormatParser
Constant Summary
LabelProvider::A, LabelProvider::AN, LabelProvider::SKIPPED_CHARACTERS, LabelProvider::VOWELS
Instance Attribute Summary
Attributes inherited from TimeData
#nsecs
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from TimeData
#<=>, #initialize, #label, #to_c, #to_f, #to_i, #to_int, #to_r
#a_an, #a_an_uc, #article, #combine_strings, #label, #plural_s, #the, #the_uc
Class Method Details
.from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0) ⇒ Object
82 83 84 85 | # File 'lib/puppet/pops/time/timespan.rb', line 82 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
.from_hash(hash) ⇒ Object
87 88 89 | # File 'lib/puppet/pops/time/timespan.rb', line 87 def self.from_hash(hash) hash.include?('string') ? from_string_hash(hash) : from_fields_hash(hash) end |
.from_string_hash(hash) ⇒ Object
.parse(str, format = Format::DEFAULTS) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # File 'lib/puppet/pops/time/timespan.rb', line 108 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 %{formats}") % { str: str, formats: format.join(', ') } end format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format) format.parse(str) end |
Instance Method Details
182 183 184 | # File 'lib/puppet/pops/time/timespan.rb', line 182 def %(o) modulo(o) end |
158 159 160 161 162 163 164 165 | # File 'lib/puppet/pops/time/timespan.rb', line 158 def *(o) case o when Integer, Float Timespan.new((@nsecs * o).to_i) else raise ArgumentError, _("A Timestamp cannot be multiplied by %{klass}") % { klass: a_an(o) } end end |
128 129 130 131 132 133 134 135 136 137 138 139 140 | # File 'lib/puppet/pops/time/timespan.rb', line 128 def +(o) case o when Timestamp Timestamp.new(@nsecs + o.nsecs) when Timespan Timespan.new(@nsecs + o.nsecs) when Integer, Float Timespan.new(@nsecs + (o * NSECS_PER_SEC).to_i) else raise ArgumentError, _("%{klass} cannot be added to a Timespan") % { klass: a_an_uc(o) } unless o.is_a?(Timespan) end end |
142 143 144 145 146 147 148 149 150 151 152 | # File 'lib/puppet/pops/time/timespan.rb', line 142 def -(o) case o when Timespan Timespan.new(@nsecs - o.nsecs) when Integer, Float Timespan.new(@nsecs - (o * NSECS_PER_SEC).to_i) else raise ArgumentError, _("%{klass} cannot be subtracted from a Timespan") % { klass: a_an_uc(o) } end end |
154 155 156 | # File 'lib/puppet/pops/time/timespan.rb', line 154 def -@ Timespan.new(-@nsecs) end |
198 199 200 | # File 'lib/puppet/pops/time/timespan.rb', line 198 def /(o) div(o) end |
#days ⇒ Integer
Returns a positive integer denoting the number of days.
203 204 205 | # File 'lib/puppet/pops/time/timespan.rb', line 203 def days total_days end |
186 187 188 189 190 191 192 193 194 195 196 | # File 'lib/puppet/pops/time/timespan.rb', line 186 def div(o) case o when Timespan @nsecs.fdiv(o.nsecs) when Integer, Float Timespan.new(@nsecs.div(o)) else raise ArgumentError, _("A Timespan cannot be divided by %{klass}") % { klass: a_an(o) } end end |
#divmod(o) ⇒ Object
167 168 169 170 171 172 173 174 175 176 | # File 'lib/puppet/pops/time/timespan.rb', line 167 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 %{klass}") % { klass: a_an(o) } end end |
Formats this timestamp into a string according to the given ‘format`
238 239 240 241 | # File 'lib/puppet/pops/time/timespan.rb', line 238 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.
208 209 210 | # File 'lib/puppet/pops/time/timespan.rb', line 208 def hours total_hours % 24 end |
#milliseconds ⇒ Integer
Returns a positive integer, 0 - 999 denoting milliseconds of second.
223 224 225 | # File 'lib/puppet/pops/time/timespan.rb', line 223 def milliseconds total_milliseconds % 1000 end |
#minutes ⇒ Integer
Returns a positive integer, 0 - 59 denoting minutes of hour.
213 214 215 | # File 'lib/puppet/pops/time/timespan.rb', line 213 def minutes total_minutes % 60 end |
#modulo(o) ⇒ Object
178 179 180 | # File 'lib/puppet/pops/time/timespan.rb', line 178 def modulo(o) divmod(o)[1] end |
#nanoseconds ⇒ Integer
Returns a positive integer, 0 - 999.999.999 denoting nanoseconds of second.
228 229 230 | # File 'lib/puppet/pops/time/timespan.rb', line 228 def nanoseconds total_nanoseconds % NSECS_PER_SEC end |
#negative? ⇒ true
Returns if the stored value is negative.
124 125 126 | # File 'lib/puppet/pops/time/timespan.rb', line 124 def negative? @nsecs < 0 end |
#seconds ⇒ Integer
Returns a positive integer, 0 - 59 denoting seconds of minute.
218 219 220 | # File 'lib/puppet/pops/time/timespan.rb', line 218 def seconds total_seconds % 60 end |
#to_hash(compact = false) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | # File 'lib/puppet/pops/time/timespan.rb', line 251 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
247 248 249 | # File 'lib/puppet/pops/time/timespan.rb', line 247 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.
280 281 282 | # File 'lib/puppet/pops/time/timespan.rb', line 280 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.
285 286 287 | # File 'lib/puppet/pops/time/timespan.rb', line 285 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.
305 306 307 | # File 'lib/puppet/pops/time/timespan.rb', line 305 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.
300 301 302 | # File 'lib/puppet/pops/time/timespan.rb', line 300 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.
290 291 292 | # File 'lib/puppet/pops/time/timespan.rb', line 290 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.
310 311 312 | # File 'lib/puppet/pops/time/timespan.rb', line 310 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.
295 296 297 | # File 'lib/puppet/pops/time/timespan.rb', line 295 def total_seconds total_nanoseconds / NSECS_PER_SEC end |