Class: RDF::Literal::DayTimeDuration

Inherits:
Duration show all
Defined in:
lib/rdf/xsd/duration.rb

Overview

A DayTimeDuration literal.

dayTimeDuration is a datatype ·derived· from duration by restricting its ·lexical representations· to instances of dayTimeDurationLexicalRep. The ·value space· of dayTimeDuration is therefore that of duration restricted to those whose ·months· property is 0. This results in a duration datatype which is totally ordered.

Constant Summary collapse

DATATYPE =
RDF::XSD.dayTimeDuration
GRAMMAR =
%r(\A
  (?<si>-)?
  P(?:(?:(?:(?<da>\d+)D)
      )
      (?:T(?:(?:(?:(?<hr>\d+)H)(?:(?<mi>\d+)M)?(?<se>\d+(?:\.\d+)?S)?)
          |  (?:(?:(?<mi>\d+)M)(?:(?<se>\d+(?:\.\d+)?)S)?)
          |  (?:(?<se>\d+(?:\.\d+)?)S)
          )
      )?
   |(?:T(?:(?:(?:(?<hr>\d+)H)(?:(?<mi>\d+)M)?(?<se>\d+(?:\.\d+)?S)?)
        |  (?:(?:(?<mi>\d+)M)(?:(?<se>\d+(?:\.\d+)?)S)?)
        |   (?:(?<se>\d+(?:\.\d+)?)S)
        )
    )
   )
\z)x.freeze

Instance Method Summary collapse

Methods inherited from Duration

#==, #canonicalize!, #days, #hours, #humanize, #initialize, #minutes, #months, #plural, #seconds, #to_h, #to_s, #valid?, #years

Constructor Details

This class inherits a constructor from RDF::Literal::Duration

Instance Method Details

#*(other) ⇒ DayTimeDuration

Returns the result of multiplying the value of self by other. The result is rounded to the nearest month.

From the XQuery function op:multiply-dayTimeDuration.

Parameters:

  • other (Literal::Numeric, ::Numeric)

Returns:

See Also:



421
422
423
424
# File 'lib/rdf/xsd/duration.rb', line 421

def *(other)
  return type_error("#{other.inspect} is not a valid Numeric")  unless (other.is_a?(::Numeric) || other.is_a?(Literal::Numeric))
  self.class.new([0, object.last * other.to_f])
end

#+(other) ⇒ DayTimeDuration

Returns the sum of two xs:dayTimeDuration values.

From the XQuery function op:add-dayTimeDurations.



395
396
397
398
# File 'lib/rdf/xsd/duration.rb', line 395

def +(other)
  return type_error("#{other.inspect} is not a valid DayTimeDuration") unless other.is_a?(Literal::DayTimeDuration) && other.valid?
  self.class.new([0, object.last + other.object.last])
end

#-(other) ⇒ DayTimeDuration

Returns the result of subtracting one xs:dayTimeDuration value from another.

From the XQuery function op:subtract-dayTimeDurationss.



408
409
410
411
# File 'lib/rdf/xsd/duration.rb', line 408

def -(other)
  return type_error("#{other.inspect} is not a valid DayTimeDuration")  unless other.is_a?(Literal::DayTimeDuration) && other.valid?
  self.class.new([0, object.last - other.object.last])
end

#/(other) ⇒ DayTimeDuration, Decimal

Returns the result of dividing the value of self by other. The result is rounded to the nearest month.

From the XQuery functions op:divide-yearMonthDuration and op:divide-yearMonthDuration-by-yearMonthDuration.



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/rdf/xsd/duration.rb', line 435

def /(other)
  case other
  when DayTimeDuration
    return type_error("#{other.inspect} is not a valid DayTimeDuration or Numeric")  unless other.valid?
    Decimal.new(object.last / other.object.last.to_f)
  when Literal::Numeric, ::Numeric
    self.class.new([0, object.last / other.to_f])
  else
    type_error("#{other.inspect} is not a valid DayTimeDuration or Numeric") 
  end
end

#<=>(other) ⇒ Boolean

Compares this literal to other for sorting purposes.

From the XQuery function op:dayTimeDuration-less-than.



456
457
458
459
# File 'lib/rdf/xsd/duration.rb', line 456

def <=>(other)
  return type_error("#{other.inspect} is not a valid DayTimeDuration") unless other.is_a?(Literal::DayTimeDuration) && other.valid?
  @object.last <=> other.object.last
end

#to_rRational

Converts the dayTimeDuration into rational seconds.

Returns:

  • (Rational)


465
466
467
# File 'lib/rdf/xsd/duration.rb', line 465

def to_r
  Rational(object.last) / (24 * 3600)
end