Ignore:
Timestamp:
Jun 30, 2009, 11:59:58 PM (15 years ago)
Author:
KOBAYASHI, Shinji
Message:

refs #49

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk/lib/models/rm/data_types/quantity/date_time.rb

    r142 r149  
    22# http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109696321450_28117_5362Report.html
    33# Ticket refs #49
     4require 'assumed_library_types'
     5
    46module OpenEHR
    57  module RM
     
    1012            attr_reader :value
    1113
    12             def initialize(value)
     14            def initialize(value, magnitude_status=nil, accuracy=nil,
     15                         normal_range=nil, normal_status=nil,
     16                         other_reference_ranges=nil)
    1317              self.value = value
     18              self.magnitude_status = magnitude_status
     19              self.accuracy = accuracy
     20              self.normal_range = normal_range
     21              self.normal_status = normal_status
     22              self.other_reference_ranges = other_reference_ranges
    1423            end
    1524
    1625            def value=(value)
    17               if value.empty? or value.nil?
     26              if value.nil? or value.empty?
    1827                raise ArgumentError, 'invalid value'
    1928              end
    2029              @value = value
    21             end
    22 
    23             def diff(value)
    24               raise NotImplementedError, 'diff must be implemented'
    2530            end
    2631          end
     
    2833          class DV_Date < DV_Temporal
    2934            include OpenEHR::Assumed_Library_Types::ISO8601_DATE_MODULE
    30             def initialize
    31              
     35
     36            DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31]
     37
     38            def initialize(value, magnitude_status=nil, accuracy=nil,
     39                           normal_range=nil, normal_status=nil,
     40                           other_reference_range=nil)
     41              super(value, magnitude_status, accuracy, normal_range,
     42                    normal_status, other_reference_range)
    3243            end
    3344
     45            def value=(value)
     46              super(value)
     47              iso8601_date = Assumed_Library_Types::ISO8601_DATE.new(value)
     48              @year = iso8601_date.year
     49              @month = iso8601_date.month
     50              @day = iso8601_date.day
     51            end
     52
     53            undef magnitude=
     54
     55            def magnitude
     56              return Date.new(@year, @month, @day)-Date.new(1601,1,1)
     57            end
     58
     59            def diff(other)
     60              if self.magnitude > other.magnitude
     61                past, future = other, self
     62              else
     63                past, future = self, other
     64              end
     65              year, month, day = 0, 0, 0
     66              if (future.day >= past.day)
     67                day = future.day - past.day
     68              else
     69                month -= 1
     70                previous_month = future.month - 1
     71                if previous_month <= 0
     72                  previous_month = 12
     73                end
     74                day = DAYS_IN_MONTH[previous_month] + future.day - past.day
     75                if leapyear?(future.year) && (previous_month == 2)
     76                  day += 1
     77                end
     78              end
     79              week = day / 7
     80              if (future.month >= past.month)
     81                month += future.month - past.month
     82              else
     83                year -= 1
     84                month += future.month + 12 - past.month
     85              end
     86              year += future.year - past.year
     87              return DV_Duration.new(
     88                   'P' + year.to_s + 'Y' + month.to_s + 'M' +
     89                         week.to_s + 'W' + day.to_s + 'D')
     90            end
    3491          end
    3592         
     
    43100            end
    44101          end
     102
     103          class DV_Duration < DV_Amount
     104            include Assumed_Library_Types::ISO8601_DURATION_MODULE
     105            attr_reader :value
     106           
     107            def initialize(value, magnitude_status=nil, accuracy=nil,
     108                         accuracy_percent=nil, normal_range=nil,
     109                         normal_status = nil, other_reference_ranges=nil)
     110              self.value = value
     111            end
     112
     113            def value=(value)
     114              raise ArgumentError, 'value must be not nil' if value.nil?
     115              @value = value
     116              iso8601_duration = Assumed_Library_Types::ISO8601_DURATION.new(value)
     117              self.years = iso8601_duration.years
     118              self.months = iso8601_duration.months
     119              self.weeks = iso8601_duration.weeks
     120              self.days = iso8601_duration.days
     121              self.hours = iso8601_duration.hours
     122              self.minutes = iso8601_duration.minutes
     123              self.seconds = iso8601_duration.seconds
     124              self.fractional_second = iso8601_duration.fractional_second
     125            end
     126
     127            def magnitude
     128              return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
     129                @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
     130                SECONDS_IN_MINUTE) + @second + @fractional_second
     131            end
     132           
     133            undef magnitude=
     134
     135          end
    45136        end # of Date_Time
    46137      end # of Quantity
Note: See TracChangeset for help on using the changeset viewer.