Ignore:
Timestamp:
Nov 20, 2009, 3:11:04 PM (14 years ago)
Author:
KOBAYASHI, Shinji
Message:

merge from branches/0.5

Location:
ruby/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk

  • ruby/trunk/lib/open_ehr/rm/data_types/quantity/date_time.rb

    r167 r369  
    44require 'assumed_library_types'
    55require 'date'
    6 
    7 module OpenEhr
     6include OpenEHR::RM::DataTypes::Quantity
     7include OpenEHR::AssumedLibraryTypes
     8
     9module OpenEHR
    810  module RM
    911    module DataTypes
    1012      module Quantity
    1113        module DateTime
    12           class DvTemporal < OpenEhr::RM::DataTypes::Quantity::DvAbsoluteQuantity
    13             include Comparable
    14             attr_reader :value
    15 
    16             def initialize(value, magnitude_status=nil, accuracy=nil,
    17                          normal_range=nil, normal_status=nil,
    18                          other_reference_ranges=nil)
    19               self.value = value
    20               self.magnitude_status = magnitude_status
    21               self.accuracy = accuracy
    22               self.normal_range = normal_range
    23               self.normal_status = normal_status
    24               self.other_reference_ranges = other_reference_ranges
     14          class DvTemporal < DvAbsoluteQuantity
     15            def initialize(args = {})
     16              self.value = args[:value]
     17              self.magnitude_status = args[:magnitude_status]
     18              self.accuracy = args[:accuracy]
     19              self.normal_range = args[:normal_range]
     20              self.normal_status = args[:normal_status]
     21              self.other_reference_ranges = args[:other_reference_ranges]
    2522            end
    2623
     
    3229            end
    3330
    34             def <=>(other)
    35               self.magnitude <=> other.magnitude
    36             end
     31            undef magnitude=
    3732          end
    3833
    3934          class DvDate < DvTemporal
    40             include OpenEhr::AssumedLibraryTypes::ISO8601_DATE_MODULE
     35            include ISO8601DateModule
    4136
    4237            DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31]
    4338
    44             def initialize(value, magnitude_status=nil, accuracy=nil,
    45                            normal_range=nil, normal_status=nil,
    46                            other_reference_range=nil)
    47               super(value, magnitude_status, accuracy, normal_range,
    48                     normal_status, other_reference_range)
    49             end
    50 
    5139            def value=(value)
    5240              super(value)
    53               iso8601_date = AssumedLibraryTypes::ISO8601_DATE.new(value)
     41              iso8601_date = ISO8601Date.new(value)
    5442              @year = iso8601_date.year
    5543              @month = iso8601_date.month
     
    5745            end
    5846
    59             undef magnitude=
    60 
    6147            def magnitude
    6248              return Date.new(@year, @month, @day)-Date.new(0000,1,1)
     
    7359                day = future.day - past.day
    7460              else
    75                 month -= 1
     61                month = -1
    7662                previous_month = future.month - 1
    77                 if previous_month <= 0
     63                if previous_month == 0
    7864                  previous_month = 12
    7965                end
     
    9076                month += future.month + 12 - past.month
    9177              end
     78              if month < 0
     79                year -= 1
     80                month += 12
     81              end
    9282              year += future.year - past.year
    93               return DvDuration.new(
     83              return DvDuration.new(:value =>
    9484                   'P' + year.to_s + 'Y' + month.to_s + 'M' +
    9585                         week.to_s + 'W' + day.to_s + 'D')
     
    9888         
    9989          class DvTime < DvTemporal
    100             include OpenEhr::AssumedLibraryTypes::ISO8601_TIME_MODULE
    101             def initialize(value, magnitude_status=nil, accuracy=nil,
    102                            normal_range=nil, normal_status=nil,
    103                            other_reference_range=nil)
    104               super(value, magnitude_status, accuracy, normal_range,
    105                     normal_status, other_reference_range)
    106             end
     90            include ISO8601TimeModule
    10791
    10892            def value=(value)
    10993              super(value)
    110               iso8601_time = AssumedLibraryTypes::ISO8601_TIME.new(value)
     94              iso8601_time = ISO8601Time.new(value)
    11195              @hour = iso8601_time.hour
    11296              @minute = iso8601_time.minute
     
    116100
    117101            def magnitude
    118               return @hour * 60 * 60 + @minute * 60 + @second + @fractional_second
     102              if @fractional_second.nil?
     103                return @hour * 60 * 60 + @minute * 60 + @second
     104              else
     105                return @hour * 60 * 60 + @minute * 60 + @second + @fractional_second
     106              end
    119107            end
    120108
     
    125113              second = (diff - hour * 60 *60 - minute * 60).to_i
    126114              fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0
    127               return DvDuration.new('P0Y0M0W0DT' + hour.to_s + 'H' +
    128                         minute.to_s + 'M' +
    129                         second.to_s + fractional_second.to_s[1..-1] + 'S')
     115              str = 'P0Y0M0W0DT' + hour.to_s + 'H' +
     116                minute.to_s + 'M' + second.to_s
     117              if @fractional_second.nil?
     118                str += 'S'
     119              else
     120                str += fractional_second.to_s[1..-1] + 'S'
     121              end
     122              return DvDuration.new(:value => str)
    130123            end
    131124          end
    132125
    133126          class DvDateTime < DvTemporal
    134             include OpenEhr::AssumedLibraryTypes::ISO8601_DATE_TIME_MODULE
    135             attr_reader :value
    136 
    137             def initialize(value, magnitude_status=nil, accuracy=nil,
    138                            normal_range=nil, normal_status=nil,
    139                            other_reference_range=nil)
    140               super(value, magnitude_status, accuracy, normal_range,
    141                     normal_status, other_reference_range)
    142             end
     127            include OpenEHR::AssumedLibraryTypes::ISO8601DateTimeModule
    143128
    144129            def value=(value)             
    145130              super(value)
    146               iso8601date_time = AssumedLibraryTypes::ISO8601_DATE_TIME.new(value)
     131              iso8601date_time = AssumedLibraryTypes::ISO8601DateTime.new(value)
    147132              self.year = iso8601date_time.year
    148133              self.month = iso8601date_time.month
     
    156141
    157142            def magnitude
    158               seconds = DateTime.new(@year,@month,@day,@hour,@minute,@second) -
    159                 DateTime.new(0000,1,1,0,0,0)
     143              seconds = (((@year * 365.24 +
     144                           @month * 30.42 +
     145                           @day) * 24 + @hour) * 60 +
     146                         @minute) * 60 + @second
    160147              if @fractional_second.nil?
    161148                return seconds
     
    164151              end
    165152            end
    166 
    167             undef magnitude=
    168153
    169154            def diff(other)
     
    184169              minute = ((time_diff - hour*60*60)/60).to_i
    185170              second = (time_diff - hour * 60 *60 - minute * 60).to_i
    186               fractional_second = ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0
    187 
    188               return DvDuration.new(date_duration.value + 'T' +
    189                         hour.to_s + 'H' +
    190                         minute.to_s + 'M' +
    191                         second.to_s + fractional_second.to_s[1..-1] + 'S')
    192                                    
     171              str = date_duration.value + 'T' + hour.to_s + 'H' +
     172                minute.to_s + 'M' + second.to_s
     173              if @fractional_second.nil?
     174                return DvDuration.new(:value => str +'S')
     175              else
     176                fractional_second =
     177                  ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0
     178                return DvDuration.new(:value => str +
     179                                      fractional_second.to_s[1..-1] + 'S')
     180              end
    193181            end
    194182
     
    197185            def split_date_time(date_time)
    198186              /^(.*)T(.*)$/ =~ date_time.as_string
    199               return DvDate.new($1), DvTime.new($2)
     187              return DvDate.new(:value => $1), DvTime.new(:value => $2)
    200188            end
    201189          end
    202190
    203191          class DvDuration < DvAmount
    204             include AssumedLibraryTypes::ISO8601_DURATION_MODULE
     192            include AssumedLibraryTypes::ISO8601DurationModule
    205193            attr_reader :value
    206194           
    207             def initialize(value, magnitude_status=nil, accuracy=nil,
    208                          accuracy_percent=nil, normal_range=nil,
    209                          normal_status = nil, other_reference_ranges=nil)
    210               self.value = value
     195            def initialize(args = { })
     196              self.value = args[:value]
     197              self.magnitude_status = args[:magnitude_status]
     198              self.normal_range = args[:normal_range]
     199              self.normal_status = args[:normal_status]
     200              self.other_reference_ranges = args[:other_reference_ranges]
    211201            end
    212202
     
    214204              raise ArgumentError, 'value must be not nil' if value.nil?
    215205              @value = value
    216               iso8601_duration = AssumedLibraryTypes::ISO8601_DURATION.new(value)
     206              iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
    217207              self.years = iso8601_duration.years
    218208              self.months = iso8601_duration.months
Note: See TracChangeset for help on using the changeset viewer.