Class: Puppet::Pops::Time::Timespan

Inherits:
TimeData show all
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

#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

Methods included from LabelProvider

#a_an, #a_an_uc, #article, #combine_strings, #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



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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/puppet/pops/time/timespan.rb', line 95

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



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



91
92
93
# File 'lib/puppet/pops/time/timespan.rb', line 91

def self.from_string_hash(hash)
  parse(hash[KEY_STRING], hash[KEY_FORMAT] || Format::DEFAULTS)
end

.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

#%(o) ⇒ Object



182
183
184
# File 'lib/puppet/pops/time/timespan.rb', line 182

def %(o)
  modulo(o)
end

#*(o) ⇒ Object



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

#+(o) ⇒ Object



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
    # Add seconds
    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

#-(o) ⇒ Object



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
    # Subtract seconds
    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

#-@Object



154
155
156
# File 'lib/puppet/pops/time/timespan.rb', line 154

def -@
  Timespan.new(-@nsecs)
end

#/(o) ⇒ Object



198
199
200
# File 'lib/puppet/pops/time/timespan.rb', line 198

def /(o)
  div(o)
end

#daysInteger

Returns a positive integer denoting the number of days.

Returns:

  • (Integer)

    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

#div(o) ⇒ Object



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
    # 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 %{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

#format(format) ⇒ String

Formats this timestamp into a string according to the given ‘format`

Parameters:

  • format (String, Format)

    The format to use when producing the string

Returns:

  • (String)

    the string representing the formatted timestamp

Raises:

  • (ArgumentError)

    if the format is a string with illegal format characters



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

#hoursInteger

Returns a positive integer, 0 - 23 denoting hours of day.

Returns:

  • (Integer)

    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

#millisecondsInteger

Returns a positive integer, 0 - 999 denoting milliseconds of second.

Returns:

  • (Integer)

    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

#minutesInteger

Returns a positive integer, 0 - 59 denoting minutes of hour.

Returns:

  • (Integer)

    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

#nanosecondsInteger

Returns a positive integer, 0 - 999.999.999 denoting nanoseconds of second.

Returns:

  • (Integer)

    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.

Returns:

  • (true)

    if the stored value is negative



124
125
126
# File 'lib/puppet/pops/time/timespan.rb', line 124

def negative?
  @nsecs < 0
end

#secondsInteger

Returns a positive integer, 0 - 59 denoting seconds of minute.

Returns:

  • (Integer)

    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_sString

Formats this timestamp into a string according to 0

Returns:

  • (String)

    the string representing the formatted timestamp



247
248
249
# File 'lib/puppet/pops/time/timespan.rb', line 247

def to_s
  format(Format::DEFAULTS[0])
end

#total_daysObject

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_hoursObject

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_microsecondsObject

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_millisecondsObject

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_minutesObject

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_nanosecondsObject

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_secondsObject

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