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


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

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


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

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


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

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

.from_string_hash(hash) ⇒ Object


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

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

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


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

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


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

def %(o)
  modulo(o)
end

#*(o) ⇒ Object


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

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


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

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


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

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

#[email protected]Object


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

def [email protected]
  Timespan.new(-@nsecs)
end

#/(o) ⇒ Object


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

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


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

def days
  total_days
end

#div(o) ⇒ Object


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

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


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

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


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

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


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

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


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

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


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

def minutes
  total_minutes % 60
end

#modulo(o) ⇒ Object


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

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


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

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


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

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


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

def seconds
  total_seconds % 60
end

#to_hash(compact = false) ⇒ Object


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

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


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

def total_seconds
  total_nanoseconds / NSECS_PER_SEC
end