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



81
82
83
84
# File 'lib/puppet/pops/time/timespan.rb', line 81

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



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

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



86
87
88
# File 'lib/puppet/pops/time/timespan.rb', line 86

def self.from_hash(hash)
  hash.include?('string') ? from_string_hash(hash) : from_fields_hash(hash)
end

.from_string_hash(hash) ⇒ Object



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

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

.parse(str, format = Format::DEFAULTS) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppet/pops/time/timespan.rb', line 106

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



180
181
182
# File 'lib/puppet/pops/time/timespan.rb', line 180

def %(o)
  modulo(o)
end

#*(o) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/puppet/pops/time/timespan.rb', line 156

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



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet/pops/time/timespan.rb', line 126

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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puppet/pops/time/timespan.rb', line 140

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



152
153
154
# File 'lib/puppet/pops/time/timespan.rb', line 152

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

#/(o) ⇒ Object



196
197
198
# File 'lib/puppet/pops/time/timespan.rb', line 196

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



201
202
203
# File 'lib/puppet/pops/time/timespan.rb', line 201

def days
  total_days
end

#div(o) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/pops/time/timespan.rb', line 184

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



165
166
167
168
169
170
171
172
173
174
# File 'lib/puppet/pops/time/timespan.rb', line 165

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



236
237
238
239
# File 'lib/puppet/pops/time/timespan.rb', line 236

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



206
207
208
# File 'lib/puppet/pops/time/timespan.rb', line 206

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



221
222
223
# File 'lib/puppet/pops/time/timespan.rb', line 221

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



211
212
213
# File 'lib/puppet/pops/time/timespan.rb', line 211

def minutes
  total_minutes % 60
end

#modulo(o) ⇒ Object



176
177
178
# File 'lib/puppet/pops/time/timespan.rb', line 176

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



226
227
228
# File 'lib/puppet/pops/time/timespan.rb', line 226

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



122
123
124
# File 'lib/puppet/pops/time/timespan.rb', line 122

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



216
217
218
# File 'lib/puppet/pops/time/timespan.rb', line 216

def seconds
  total_seconds % 60
end

#to_hash(compact = false) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/puppet/pops/time/timespan.rb', line 249

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



245
246
247
# File 'lib/puppet/pops/time/timespan.rb', line 245

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.



278
279
280
# File 'lib/puppet/pops/time/timespan.rb', line 278

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.



283
284
285
# File 'lib/puppet/pops/time/timespan.rb', line 283

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.



303
304
305
# File 'lib/puppet/pops/time/timespan.rb', line 303

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.



298
299
300
# File 'lib/puppet/pops/time/timespan.rb', line 298

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.



288
289
290
# File 'lib/puppet/pops/time/timespan.rb', line 288

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.



308
309
310
# File 'lib/puppet/pops/time/timespan.rb', line 308

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.



293
294
295
# File 'lib/puppet/pops/time/timespan.rb', line 293

def total_seconds
  total_nanoseconds / NSECS_PER_SEC
end