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
541 def initialize(format, segments)
542   @format = format.freeze
543   @segments = segments.freeze
544 end

Public Instance Methods

format(timespan) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
546 def format(timespan)
547   bld = timespan.negative? ? '-' : ''
548   @segments.each { |segment| segment.append_to(bld, timespan) }
549   bld
550 end
parse(timespan) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
552 def parse(timespan)
553   md = regexp.match(timespan)
554   raise ArgumentError, _("Unable to parse '%{timespan}' using format '%{format}'") % { timespan: timespan, format: @format } if md.nil?
555   nanoseconds = 0
556   md.captures.each_with_index do |group, index|
557     segment = @segments[index]
558     next if segment.is_a?(LiteralSegment)
559     group.lstrip!
560     raise ArgumentError, _("Unable to parse '%{timespan}' using format '%{format}'") % { timespan: timespan, format: @format } unless group =~ /\A[0-9]+\z/
561     nanoseconds += segment.nanoseconds(group)
562   end
563   Timespan.new(timespan.start_with?('-') ? -nanoseconds : nanoseconds)
564 end
to_s() click to toggle source
    # File lib/puppet/pops/time/timespan.rb
566 def to_s
567   @format
568 end

Private Instance Methods

build_regexp() click to toggle source
    # File lib/puppet/pops/time/timespan.rb
576 def build_regexp
577   bld = '\A-?'
578   @segments.each { |segment| segment.append_regexp(bld) }
579   bld << '\z'
580   Regexp.new(bld)
581 end
regexp() click to toggle source
    # File lib/puppet/pops/time/timespan.rb
572 def regexp
573   @regexp ||= build_regexp
574 end