Class: OpenEHR::AssumedLibraryTypes::ISO8601Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable, ISO8601DurationModule
Defined in:
lib/openehr/assumed_library_types.rb

Constant Summary

Constants included from TimeDefinitions

TimeDefinitions::DAYS_IN_LEAP_YEAR, TimeDefinitions::DAYS_IN_MONTH, TimeDefinitions::DAYS_IN_WEEK, TimeDefinitions::DAYS_IN_YEAR, TimeDefinitions::HOURS_IN_DAY, TimeDefinitions::MAX_DAYS_IN_MONTH, TimeDefinitions::MAX_DAYS_IN_YEAR, TimeDefinitions::MINUTES_IN_HOUR, TimeDefinitions::MONTH_IN_YEAR, TimeDefinitions::NOMINAL_DAYS_IN_MONTH, TimeDefinitions::NOMINAL_DAYS_IN_YEAR, TimeDefinitions::SECONDS_IN_MINUTE

Instance Attribute Summary

Attributes included from ISO8601DurationModule

#days, #fractional_second, #hours, #minutes, #months, #negative, #seconds, #weeks, #years

Instance Method Summary collapse

Methods included from ISO8601DurationModule

#as_string, #negative?, #to_seconds

Methods included from TimeDefinitions

valid_day?, valid_hour?, valid_minute?, valid_month?, valid_second?, valid_year?

Constructor Details

#initialize(str) ⇒ ISO8601Duration

Returns a new instance of ISO8601Duration.



738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'lib/openehr/assumed_library_types.rb', line 738

def initialize(str)
  # A leading '-' negates the whole duration (RM 1.1.0, SPECRM-96);
  # stripped before the main match so the existing capture group
  # numbering below doesn't need to shift.
  self.negative = str.start_with?('-')
  duration_str = negative? ? str[1..-1] : str
  md = /^P((\d+)[Yy])?((\d+)[Mm])?((\d+)[Ww])?((\d+)[dD])?(T((\d+)[Hh])?((\d+)[Mm])?((\d+)(\.\d+)?[Ss])?)?$/.match(duration_str)
  if md.nil?
    raise ArgumentError, 'invalid ISO8601 duration format'
  end
  # Absent components stay nil rather than being coerced to 0, so
  # as_string can round-trip a partially-specified duration and
  # "unset" is distinguishable from "explicitly zero".
  self.years = md[2] && md[2].to_i
  self.months = md[4] && md[4].to_i
  self.weeks = md[6] && md[6].to_i
  self.days = md[8] && md[8].to_i
  self.hours = md[11] && md[11].to_i
  self.minutes = md[13] && md[13].to_i
  self.seconds = md[15] && md[15].to_i
  self.fractional_second = md[16] && md[16].to_f
end

Instance Method Details

#<=>(other) ⇒ Object



761
762
763
# File 'lib/openehr/assumed_library_types.rb', line 761

def <=>(other)
  self.to_seconds <=> other.to_seconds
end