Method: RdfContext::Duration#to_s

Defined in:
lib/rdf_context/duration.rb

#to_s(format = nil) ⇒ String

Parameters:

  • format (Symbol) (defaults to: nil)

    Output format, :xml outputs in xmlschema mode, otherwise in Human form

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rdf_context/duration.rb', line 83

def to_s(format = nil)
  usec = (@se * 1000).to_i % 1000
  sec_str = usec > 0 ? "%2.3f" % @se : @se.to_i.to_s
  
  if format == :xml
    str = @ne < 0 ? "-P" : "P"
    str << "%dY" % @yr if @yr > 0
    str << "%dM" % @mo if @mo > 0
    str << "%dD" % @da if @da > 0
    str << "T" if @hr + @mi + @se > 0
    str << "%dH" % @hr if @hr > 0
    str << "%dM" % @mi if @mi > 0
    str << "#{sec_str}S" if @se > 0
  else
    ar = []
    ar << "%d years"    % @yr     if @yr > 0
    ar << "%d months"   % @mo     if @mo > 0
    ar << "%d days"     % @da     if @da > 0
    ar << "%d hours"    % @hr     if @hr > 0
    ar << "%d minutes"  % @mi     if @mi > 0
    ar << "%s seconds"  % sec_str if @se > 0
    last = ar.pop
    first = ar.join(", ")
    res = first.empty? ? last : "#{first} and #{last}"
    ne < 0 ? "#{res} ago" : res
  end
end