Class: Puppet::Pops::Time::Timestamp

Inherits:
TimeData show all
Defined in:
lib/puppet/pops/time/timestamp.rb

Constant Summary collapse

DEFAULT_FORMATS_WO_TZ =
['%FT%T.L', '%FT%T', '%F']
DEFAULT_FORMATS =
['%FT%T.%L %Z', '%FT%T %Z', '%F %Z'] + DEFAULT_FORMATS_WO_TZ
CURRENT_TIMEZONE =
'current'.freeze
KEY_TIMEZONE =
'timezone'.freeze

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

.assert_no_tz_extractor(format) ⇒ Object



95
96
97
98
99
# File 'lib/puppet/pops/time/timestamp.rb', line 95

def self.assert_no_tz_extractor(format)
  if format =~ /[^%]%[zZ]/
    raise ArgumentError, 'Using a Timezone designator in format specification is mutually exclusive to providing an explicit timezone argument'
  end
end

.convert_timezone(tz) ⇒ String

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 timezone that strptime can parse using ‘%z’ into ‘-HH:MM’ or ‘+HH:MM’

Parameters:

  • tz (String)

    the timezone to convert

Returns:

  • (String)

    the converted timezone



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/pops/time/timestamp.rb', line 15

def self.convert_timezone(tz)
  if tz =~ /\A[+-]\d\d:\d\d\z/
    tz
  else
    offset = utc_offset(tz) / 60
    if offset < 0
      offset = offset.abs
      sprintf('-%2.2d:%2.2d', offset / 60, offset % 60)
    else
      sprintf('+%2.2d:%2.2d', offset / 60, offset % 60)
    end
  end
end

.format_time(format, time, timezone) ⇒ Object

Formats a ruby Time object using the given timezone



46
47
48
49
50
51
# File 'lib/puppet/pops/time/timestamp.rb', line 46

def self.format_time(format, time, timezone)
  unless timezone.nil? || timezone.empty?
    time = time.localtime(convert_timezone(timezone))
  end
  time.strftime(format)
end

.from_hash(args_hash) ⇒ Object



61
62
63
# File 'lib/puppet/pops/time/timestamp.rb', line 61

def self.from_hash(args_hash)
  parse(args_hash[KEY_STRING], args_hash[KEY_FORMAT], args_hash[KEY_TIMEZONE])
end

.from_time(t) ⇒ Object



57
58
59
# File 'lib/puppet/pops/time/timestamp.rb', line 57

def self.from_time(t)
  new(t.tv_sec * NSECS_PER_SEC + t.tv_nsec)
end

.nowObject



53
54
55
# File 'lib/puppet/pops/time/timestamp.rb', line 53

def self.now
  from_time(::Time.now)
end

.parse(str, format = :default, timezone = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/pops/time/timestamp.rb', line 65

def self.parse(str, format = :default, timezone = nil)
  has_timezone = !(timezone.nil? || timezone.empty? || timezone == :default)
  if format.nil? || format == :default
    format = has_timezone ? DEFAULT_FORMATS_WO_TZ : DEFAULT_FORMATS
  end

  parsed = nil
  if format.is_a?(Array)
    format.each do |fmt|
      assert_no_tz_extractor(fmt) if has_timezone
      begin
        parsed = DateTime.strptime(str, fmt)
        break
      rescue ArgumentError
      end
    end
    raise ArgumentError, "Unable to parse '#{str}' using any of the formats #{format.join(', ')}" if parsed.nil?
  else
    assert_no_tz_extractor(format) if has_timezone
    begin
      parsed = DateTime.strptime(str, format)
    rescue ArgumentError
      raise ArgumentError, "Unable to parse '#{str}' using format '#{format}'"
    end
  end
  parsed_time = parsed.to_time
  parsed_time -= utc_offset(timezone) if has_timezone
  from_time(parsed_time)
end

.utc_offset(timezone) ⇒ Integer

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.

Returns the zone offset from utc for the given ‘timezone`

Parameters:

  • timezone (String)

    the timezone to get the offset for

Returns:

  • (Integer)

    the timezone offset, in seconds



34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/pops/time/timestamp.rb', line 34

def self.utc_offset(timezone)
  if CURRENT_TIMEZONE.casecmp(timezone) == 0
    ::Time.now.utc_offset
  else
    hash = DateTime._strptime(timezone, '%z')
    offset = hash.nil? ? nil : hash[:offset]
    raise ArgumentError, "Illegal timezone '#{timezone}'" if offset.nil?
    offset
  end
end

Instance Method Details

#+(o) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/puppet/pops/time/timestamp.rb', line 109

def +(o)
  case o
  when Timespan
    Timestamp.new(@nsecs + o.nsecs)
  when Integer, Float
    Timestamp.new(@nsecs + (o * NSECS_PER_SEC).to_i)
  else
    raise ArgumentError, "#{a_an_uc(o)} cannot be added to a Timestamp"
  end
end

#-(o) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/puppet/pops/time/timestamp.rb', line 120

def -(o)
  case o
  when Timestamp
    # Diff between two timestamps is a timespan
    Timespan.new(@nsecs - o.nsecs)
  when Timespan
    Timestamp.new(@nsecs - o.nsecs)
  when Integer, Float
    # Subtract seconds
    Timestamp.new(@nsecs - (o * NSECS_PER_SEC).to_i)
  else
    raise ArgumentError, "#{a_an_uc(o)} cannot be subtracted from a Timestamp"
  end
end

#format(format, timezone = nil) ⇒ Object



135
136
137
# File 'lib/puppet/pops/time/timestamp.rb', line 135

def format(format, timezone = nil)
  self.class.format_time(format, to_time, timezone)
end

#to_sObject



139
140
141
# File 'lib/puppet/pops/time/timestamp.rb', line 139

def to_s
  format(DEFAULT_FORMATS[0])
end

#to_timeObject



143
144
145
# File 'lib/puppet/pops/time/timestamp.rb', line 143

def to_time
  ::Time.at(to_r).utc
end