Class: PuppetX::PuppetLabs::ScheduledTask::Trigger::Duration Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/scheduled_task/trigger.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Gets or sets the amount of time that is allowed to complete the task.

Class Method Summary collapse

Class Method Details

.hash_to_seconds(value) ⇒ 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.

Converts a hash in a time format to seconds



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 43

def self.hash_to_seconds(value)
  return 0 if value.nil?
  time = 0
  # Note - the Year and Month calculations are approximate
  time += value[:year].to_i   * (365.2422 * 24 * 60**2)      unless value[:year].nil?
  time += value[:month].to_i  * (365.2422 * 2 * 60**2)       unless value[:month].nil?
  time += value[:day].to_i    * 24 * 60**2                   unless value[:day].nil?
  time += value[:hour].to_i   * 60**2                        unless value[:hour].nil?
  time += value[:minute].to_i * 60                           unless value[:minute].nil?
  time += value[:second].to_i                                unless value[:second].nil?

  time.to_i
end

.to_hash(duration) ⇒ 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.

From msdn.microsoft.com/en-us/library/windows/desktop/aa381850(v=vs.85).aspx en.wikipedia.org/wiki/ISO_8601#Durations

The format for this string is PnYnMnDTnHnMnS, where nY is the number of years, nM is the number of months, nD is the number of days, ‘T’ is the date/time separator, nH is the number of hours, nM is the number of minutes, and nS is the number of seconds (for example, PT5M specifies 5 minutes and P1M4DT2H5M specifies one month, four days, two hours, and five minutes)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 26

def self.to_hash(duration)
  regex = %r{^P((?'year'\d+)Y)?((?'month'\d+)M)?((?'day'\d+)D)?(T((?'hour'\d+)H)?((?'minute'\d+)M)?((?'second'\d+)S)?)?$}

  matches = regex.match(duration)
  return nil if matches.nil?

  {
    year: matches['year'],
    month: matches['month'],
    day: matches['day'],
    minute: matches['minute'],
    hour: matches['hour'],
    second: matches['second'],
  }
end

.to_minutes(value) ⇒ 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.

Converts a hash in a time format to minutes



58
59
60
61
62
63
64
65
66
# File 'lib/puppet_x/puppetlabs/scheduled_task/trigger.rb', line 58

def self.to_minutes(value)
  return 0 if value.nil?
  return 0 unless value.is_a?(String)
  return 0 if value.empty?

  duration = hash_to_seconds(to_hash(value))

  duration / 60
end