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 15 def self.convert_timezone(tz) 16 if tz =~ /\A[+-]\d\d:\d\d\z/ 17 tz 18 else 19 offset = utc_offset(tz) / 60 20 if offset < 0 21 offset = offset.abs 22 sprintf('-%2.2d:%2.2d', offset / 60, offset % 60) 23 else 24 sprintf('+%2.2d:%2.2d', offset / 60, offset % 60) 25 end 26 end 27 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 46 def self.format_time(format, time, timezone) 47 unless timezone.nil? || timezone.empty? 48 time = time.localtime(convert_timezone(timezone)) 49 end 50 time.strftime(format) 51 end
from_hash(args_hash)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 61 def self.from_hash(args_hash) 62 parse(args_hash[KEY_STRING], args_hash[KEY_FORMAT], args_hash[KEY_TIMEZONE]) 63 end
from_time(t)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 57 def self.from_time(t) 58 new(t.tv_sec * NSECS_PER_SEC + t.tv_nsec) 59 end
now()
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 53 def self.now 54 from_time(::Time.now) 55 end
parse(str, format = :default, timezone = nil)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 65 def self.parse(str, format = :default, timezone = nil) 66 has_timezone = !(timezone.nil? || timezone.empty? || timezone == :default) 67 if format.nil? || format == :default 68 format = has_timezone ? DEFAULT_FORMATS_WO_TZ : DEFAULT_FORMATS 69 end 70 71 parsed = nil 72 if format.is_a?(Array) 73 format.each do |fmt| 74 parsed = DateTime._strptime(str, fmt) 75 next if parsed.nil? 76 if parsed.include?(:leftover) || (has_timezone && parsed.include?(:zone)) 77 parsed = nil 78 next 79 end 80 break 81 end 82 if parsed.nil? 83 raise ArgumentError, _( 84 "Unable to parse '%{str}' using any of the formats %{formats}") % { str: str, formats: format.join(', ') } 85 end 86 else 87 parsed = DateTime._strptime(str, format) 88 if parsed.nil? || parsed.include?(:leftover) 89 raise ArgumentError, _("Unable to parse '%{str}' using format '%{format}'") % { str: str, format: format } 90 end 91 if has_timezone && parsed.include?(:zone) 92 raise ArgumentError, _( 93 'Using a Timezone designator in format specification is mutually exclusive to providing an explicit timezone argument') 94 end 95 end 96 unless has_timezone 97 timezone = parsed[:zone] 98 has_timezone = !timezone.nil? 99 end 100 fraction = parsed[:sec_fraction] 101 102 # Convert msec rational found in _strptime hash to usec 103 fraction = fraction * 1000000 unless fraction.nil? 104 105 # Create the Time instance and adjust for timezone 106 parsed_time = ::Time.utc(parsed[:year], parsed[:mon], parsed[:mday], parsed[:hour], parsed[:min], parsed[:sec], fraction) 107 parsed_time -= utc_offset(timezone) if has_timezone 108 109 # Convert to Timestamp 110 from_time(parsed_time) 111 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 34 def self.utc_offset(timezone) 35 if CURRENT_TIMEZONE.casecmp(timezone) == 0 36 ::Time.now.utc_offset 37 else 38 hash = DateTime._strptime(timezone, '%z') 39 offset = hash.nil? ? nil : hash[:offset] 40 raise ArgumentError, _("Illegal timezone '%{timezone}'") % { timezone: timezone } if offset.nil? 41 offset 42 end 43 end
Public Instance Methods
+(o)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 121 def +(o) 122 case o 123 when Timespan 124 Timestamp.new(@nsecs + o.nsecs) 125 when Integer, Float 126 Timestamp.new(@nsecs + (o * NSECS_PER_SEC).to_i) 127 else 128 raise ArgumentError, _("%{klass} cannot be added to a Timestamp") % { klass: a_an_uc(o) } 129 end 130 end
-(o)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 132 def -(o) 133 case o 134 when Timestamp 135 # Diff between two timestamps is a timespan 136 Timespan.new(@nsecs - o.nsecs) 137 when Timespan 138 Timestamp.new(@nsecs - o.nsecs) 139 when Integer, Float 140 # Subtract seconds 141 Timestamp.new(@nsecs - (o * NSECS_PER_SEC).to_i) 142 else 143 raise ArgumentError, _("%{klass} cannot be subtracted from a Timestamp") % { klass: a_an_uc(o) } 144 end 145 end
format(format, timezone = nil)
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 147 def format(format, timezone = nil) 148 self.class.format_time(format, to_time, timezone) 149 end
to_s()
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 151 def to_s 152 format(DEFAULT_FORMATS[0]) 153 end
to_time()
click to toggle source
# File lib/puppet/pops/time/timestamp.rb 155 def to_time 156 ::Time.at(to_r).utc 157 end