class Puppet::Pops::Time::Timespan::Format
Represents a compiled Timestamp format. The format is used both when parsing a timestamp in string format and when producing a string from a timestamp instance.
Constants
- DEFAULTS
Public Class Methods
new(format, segments)
click to toggle source
# File lib/puppet/pops/time/timespan.rb 542 def initialize(format, segments) 543 @format = format.freeze 544 @segments = segments.freeze 545 end
Public Instance Methods
format(timespan)
click to toggle source
# File lib/puppet/pops/time/timespan.rb 547 def format(timespan) 548 bld = String.new(timespan.negative? ? '-' : '') 549 @segments.each { |segment| segment.append_to(bld, timespan) } 550 bld 551 end
parse(timespan)
click to toggle source
# File lib/puppet/pops/time/timespan.rb 553 def parse(timespan) 554 md = regexp.match(timespan) 555 raise ArgumentError, _("Unable to parse '%{timespan}' using format '%{format}'") % { timespan: timespan, format: @format } if md.nil? 556 nanoseconds = 0 557 md.captures.each_with_index do |group, index| 558 segment = @segments[index] 559 next if segment.is_a?(LiteralSegment) 560 group.lstrip! 561 raise ArgumentError, _("Unable to parse '%{timespan}' using format '%{format}'") % { timespan: timespan, format: @format } unless group =~ /\A[0-9]+\z/ 562 nanoseconds += segment.nanoseconds(group) 563 end 564 Timespan.new(timespan.start_with?('-') ? -nanoseconds : nanoseconds) 565 end
to_s()
click to toggle source
# File lib/puppet/pops/time/timespan.rb 567 def to_s 568 @format 569 end
Private Instance Methods
build_regexp()
click to toggle source
# File lib/puppet/pops/time/timespan.rb 577 def build_regexp 578 bld = String.new('\A-?') 579 @segments.each { |segment| segment.append_regexp(bld) } 580 bld << '\z' 581 Regexp.new(bld) 582 end
regexp()
click to toggle source
# File lib/puppet/pops/time/timespan.rb 573 def regexp 574 @regexp ||= build_regexp 575 end