class Puppet::Pops::Time::Timestamp

Constants

CURRENT_TIMEZONE
DEFAULT_FORMATS
DEFAULT_FORMATS_WO_TZ
KEY_TIMEZONE

Public Class Methods

convert_timezone(tz) click to toggle source

Converts a timezone that strptime can parse using '%z' into '-HH:MM' or '+HH:MM' @param [String] tz the timezone to convert @return [String] the converted timezone

@api private

   # File lib/puppet/pops/time/timestamp.rb
16 def self.convert_timezone(tz)
17   if tz =~ /\A[+-]\d\d:\d\d\z/
18     tz
19   else
20     offset = utc_offset(tz) / 60
21     if offset < 0
22       offset = offset.abs
23       sprintf('-%2.2d:%2.2d', offset / 60, offset % 60)
24     else
25       sprintf('+%2.2d:%2.2d', offset / 60, offset % 60)
26     end
27   end
28 end
format_time(format, time, timezone) click to toggle source

Formats a ruby Time object using the given timezone

   # File lib/puppet/pops/time/timestamp.rb
47 def self.format_time(format, time, timezone)
48   unless timezone.nil? || timezone.empty?
49     time = time.localtime(convert_timezone(timezone))
50   end
51   time.strftime(format)
52 end
from_hash(args_hash) click to toggle source
   # File lib/puppet/pops/time/timestamp.rb
62 def self.from_hash(args_hash)
63   parse(args_hash[KEY_STRING], args_hash[KEY_FORMAT], args_hash[KEY_TIMEZONE])
64 end
from_time(t) click to toggle source
   # File lib/puppet/pops/time/timestamp.rb
58 def self.from_time(t)
59   new(t.tv_sec * NSECS_PER_SEC + t.tv_nsec)
60 end
now() click to toggle source
   # File lib/puppet/pops/time/timestamp.rb
54 def self.now
55   from_time(::Time.now)
56 end
parse(str, format = :default, timezone = nil) click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
 66 def self.parse(str, format = :default, timezone = nil)
 67   has_timezone = !(timezone.nil? || timezone.empty? || timezone == :default)
 68   if format.nil? || format == :default
 69     format = has_timezone ? DEFAULT_FORMATS_WO_TZ : DEFAULT_FORMATS
 70   end
 71 
 72   parsed = nil
 73   if format.is_a?(Array)
 74     format.each do |fmt|
 75       parsed = DateTime._strptime(str, fmt)
 76       next if parsed.nil?
 77       if parsed.include?(:leftover) || (has_timezone && parsed.include?(:zone))
 78         parsed = nil
 79         next
 80       end
 81       break
 82     end
 83     if parsed.nil?
 84       raise ArgumentError, _(
 85         "Unable to parse '%{str}' using any of the formats %{formats}") % { str: str, formats: format.join(', ') }
 86     end
 87   else
 88     parsed = DateTime._strptime(str, format)
 89     if parsed.nil? || parsed.include?(:leftover)
 90       raise ArgumentError, _("Unable to parse '%{str}' using format '%{format}'") % { str: str, format: format }
 91     end
 92     if has_timezone && parsed.include?(:zone)
 93       raise ArgumentError, _(
 94         'Using a Timezone designator in format specification is mutually exclusive to providing an explicit timezone argument')
 95     end
 96   end
 97   unless has_timezone
 98     timezone = parsed[:zone]
 99     has_timezone = !timezone.nil?
100   end
101   fraction = parsed[:sec_fraction]
102 
103   # Convert msec rational found in _strptime hash to usec
104   fraction = fraction * 1000000 unless fraction.nil?
105 
106   # Create the Time instance and adjust for timezone
107   parsed_time = ::Time.utc(parsed[:year], parsed[:mon], parsed[:mday], parsed[:hour], parsed[:min], parsed[:sec], fraction)
108   parsed_time -= utc_offset(timezone) if has_timezone
109 
110   # Convert to Timestamp
111   from_time(parsed_time)
112 end
utc_offset(timezone) click to toggle source

Returns the zone offset from utc for the given `timezone` @param [String] timezone the timezone to get the offset for @return [Integer] the timezone offset, in seconds

@api private

   # File lib/puppet/pops/time/timestamp.rb
35 def self.utc_offset(timezone)
36   if CURRENT_TIMEZONE.casecmp(timezone) == 0
37     ::Time.now.utc_offset
38   else
39     hash = DateTime._strptime(timezone, '%z')
40     offset = hash.nil? ? nil : hash[:offset]
41     raise ArgumentError, _("Illegal timezone '%{timezone}'") % { timezone: timezone } if offset.nil?
42     offset
43   end
44 end

Public Instance Methods

+(o) click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
122 def +(o)
123   case o
124   when Timespan
125     Timestamp.new(@nsecs + o.nsecs)
126   when Integer, Float
127     Timestamp.new(@nsecs + (o * NSECS_PER_SEC).to_i)
128   else
129     raise ArgumentError, _("%{klass} cannot be added to a Timestamp") % { klass: a_an_uc(o) }
130   end
131 end
-(o) click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
133 def -(o)
134   case o
135   when Timestamp
136     # Diff between two timestamps is a timespan
137     Timespan.new(@nsecs - o.nsecs)
138   when Timespan
139     Timestamp.new(@nsecs - o.nsecs)
140   when Integer, Float
141     # Subtract seconds
142     Timestamp.new(@nsecs - (o * NSECS_PER_SEC).to_i)
143   else
144     raise ArgumentError, _("%{klass} cannot be subtracted from a Timestamp") % { klass: a_an_uc(o) }
145   end
146 end
format(format, timezone = nil) click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
148 def format(format, timezone = nil)
149   self.class.format_time(format, to_time, timezone)
150 end
to_s() click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
152 def to_s
153   format(DEFAULT_FORMATS[0])
154 end
to_time() click to toggle source
    # File lib/puppet/pops/time/timestamp.rb
156 def to_time
157   ::Time.at(to_r).utc
158 end