[88] | 1 | # This module is implementation of the UML:
|
---|
| 2 | # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109696321450_28117_5362Report.html
|
---|
| 3 | # Ticket refs #49
|
---|
[175] | 4 | #require 'assumed_library_types'
|
---|
[151] | 5 | require 'date'
|
---|
[308] | 6 | include OpenEHR::RM::DataTypes::Quantity
|
---|
[149] | 7 |
|
---|
[308] | 8 | module OpenEHR
|
---|
[4] | 9 | module RM
|
---|
[167] | 10 | module DataTypes
|
---|
[4] | 11 | module Quantity
|
---|
[167] | 12 | module DateTime
|
---|
[308] | 13 | class DvTemporal < DvAbsoluteQuantity
|
---|
[157] | 14 | include Comparable
|
---|
[123] | 15 |
|
---|
[120] | 16 | def value=(value)
|
---|
[149] | 17 | if value.nil? or value.empty?
|
---|
[120] | 18 | raise ArgumentError, 'invalid value'
|
---|
| 19 | end
|
---|
| 20 | @value = value
|
---|
| 21 | end
|
---|
| 22 | end
|
---|
| 23 |
|
---|
[167] | 24 | class DvDate < DvTemporal
|
---|
[308] | 25 | include OpenEHR::AssumedLibraryTypes::ISO8601DateModule
|
---|
[149] | 26 |
|
---|
| 27 | DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31]
|
---|
| 28 |
|
---|
| 29 | def initialize(value, magnitude_status=nil, accuracy=nil,
|
---|
| 30 | normal_range=nil, normal_status=nil,
|
---|
| 31 | other_reference_range=nil)
|
---|
| 32 | super(value, magnitude_status, accuracy, normal_range,
|
---|
| 33 | normal_status, other_reference_range)
|
---|
[120] | 34 | end
|
---|
[123] | 35 |
|
---|
[149] | 36 | def value=(value)
|
---|
| 37 | super(value)
|
---|
[167] | 38 | iso8601_date = AssumedLibraryTypes::ISO8601_DATE.new(value)
|
---|
[149] | 39 | @year = iso8601_date.year
|
---|
| 40 | @month = iso8601_date.month
|
---|
| 41 | @day = iso8601_date.day
|
---|
| 42 | end
|
---|
| 43 |
|
---|
| 44 | undef magnitude=
|
---|
| 45 |
|
---|
| 46 | def magnitude
|
---|
[150] | 47 | return Date.new(@year, @month, @day)-Date.new(0000,1,1)
|
---|
[149] | 48 | end
|
---|
| 49 |
|
---|
| 50 | def diff(other)
|
---|
| 51 | if self.magnitude > other.magnitude
|
---|
| 52 | past, future = other, self
|
---|
| 53 | else
|
---|
| 54 | past, future = self, other
|
---|
| 55 | end
|
---|
| 56 | year, month, day = 0, 0, 0
|
---|
| 57 | if (future.day >= past.day)
|
---|
| 58 | day = future.day - past.day
|
---|
| 59 | else
|
---|
| 60 | month -= 1
|
---|
| 61 | previous_month = future.month - 1
|
---|
| 62 | if previous_month <= 0
|
---|
| 63 | previous_month = 12
|
---|
| 64 | end
|
---|
| 65 | day = DAYS_IN_MONTH[previous_month] + future.day - past.day
|
---|
| 66 | if leapyear?(future.year) && (previous_month == 2)
|
---|
| 67 | day += 1
|
---|
| 68 | end
|
---|
| 69 | end
|
---|
| 70 | week = day / 7
|
---|
| 71 | if (future.month >= past.month)
|
---|
| 72 | month += future.month - past.month
|
---|
| 73 | else
|
---|
| 74 | year -= 1
|
---|
| 75 | month += future.month + 12 - past.month
|
---|
| 76 | end
|
---|
| 77 | year += future.year - past.year
|
---|
[167] | 78 | return DvDuration.new(
|
---|
[149] | 79 | 'P' + year.to_s + 'Y' + month.to_s + 'M' +
|
---|
| 80 | week.to_s + 'W' + day.to_s + 'D')
|
---|
| 81 | end
|
---|
[88] | 82 | end
|
---|
[123] | 83 |
|
---|
[167] | 84 | class DvTime < DvTemporal
|
---|
[308] | 85 | include OpenEHR::AssumedLibraryTypes::ISO8601TimeModule
|
---|
[150] | 86 | def initialize(value, magnitude_status=nil, accuracy=nil,
|
---|
| 87 | normal_range=nil, normal_status=nil,
|
---|
| 88 | other_reference_range=nil)
|
---|
| 89 | super(value, magnitude_status, accuracy, normal_range,
|
---|
| 90 | normal_status, other_reference_range)
|
---|
[123] | 91 | end
|
---|
[150] | 92 |
|
---|
| 93 | def value=(value)
|
---|
| 94 | super(value)
|
---|
[308] | 95 | iso8601_time = AssumedLibraryTypes::ISO8601Time.new(value)
|
---|
[150] | 96 | @hour = iso8601_time.hour
|
---|
| 97 | @minute = iso8601_time.minute
|
---|
| 98 | @second = iso8601_time.second
|
---|
| 99 | @fractional_second = iso8601_time.fractional_second
|
---|
| 100 | end
|
---|
| 101 |
|
---|
| 102 | def magnitude
|
---|
| 103 | return @hour * 60 * 60 + @minute * 60 + @second + @fractional_second
|
---|
| 104 | end
|
---|
| 105 |
|
---|
| 106 | def diff(other)
|
---|
| 107 | diff = (other.magnitude - self.magnitude).abs
|
---|
| 108 | hour = (diff / 60 / 60).to_i
|
---|
| 109 | minute = ((diff - hour*60*60)/60).to_i
|
---|
| 110 | second = (diff - hour * 60 *60 - minute * 60).to_i
|
---|
[151] | 111 | fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0
|
---|
[167] | 112 | return DvDuration.new('P0Y0M0W0DT' + hour.to_s + 'H' +
|
---|
[151] | 113 | minute.to_s + 'M' +
|
---|
| 114 | second.to_s + fractional_second.to_s[1..-1] + 'S')
|
---|
[150] | 115 | end
|
---|
[123] | 116 | end
|
---|
| 117 |
|
---|
[167] | 118 | class DvDateTime < DvTemporal
|
---|
[308] | 119 | include OpenEHR::AssumedLibraryTypes::ISO8601DateTimeModule
|
---|
[151] | 120 | attr_reader :value
|
---|
| 121 |
|
---|
| 122 | def initialize(value, magnitude_status=nil, accuracy=nil,
|
---|
| 123 | normal_range=nil, normal_status=nil,
|
---|
| 124 | other_reference_range=nil)
|
---|
| 125 | super(value, magnitude_status, accuracy, normal_range,
|
---|
| 126 | normal_status, other_reference_range)
|
---|
[123] | 127 | end
|
---|
[151] | 128 |
|
---|
| 129 | def value=(value)
|
---|
| 130 | super(value)
|
---|
[308] | 131 | iso8601date_time = AssumedLibraryTypes::ISO8601DateTime.new(value)
|
---|
[151] | 132 | self.year = iso8601date_time.year
|
---|
| 133 | self.month = iso8601date_time.month
|
---|
| 134 | self.day = iso8601date_time.day
|
---|
| 135 | self.minute = iso8601date_time.minute
|
---|
| 136 | self.second = iso8601date_time.second
|
---|
| 137 | self.hour = iso8601date_time.hour
|
---|
| 138 | self.fractional_second = iso8601date_time.fractional_second
|
---|
| 139 | self.timezone = iso8601date_time.timezone
|
---|
| 140 | end
|
---|
| 141 |
|
---|
| 142 | def magnitude
|
---|
[174] | 143 | seconds = ::DateTime.new(@year,@month,@day,@hour,@minute,@second) -
|
---|
| 144 | ::DateTime.new(0000,1,1,0,0,0)
|
---|
[157] | 145 | if @fractional_second.nil?
|
---|
| 146 | return seconds
|
---|
| 147 | else
|
---|
| 148 | return seconds + @fractional_second
|
---|
| 149 | end
|
---|
[151] | 150 | end
|
---|
| 151 |
|
---|
| 152 | undef magnitude=
|
---|
| 153 |
|
---|
| 154 | def diff(other)
|
---|
| 155 | if self.magnitude >= other.magnitude
|
---|
| 156 | past, future = other, self
|
---|
| 157 | else
|
---|
| 158 | past, future = self, other
|
---|
| 159 | end
|
---|
| 160 | past_date, past_time = split_date_time(past)
|
---|
| 161 | future_date, future_time = split_date_time(future)
|
---|
| 162 | time_diff = future_time.magnitude - past_time.magnitude
|
---|
| 163 | if future_time.magnitude < past_time.magnitude
|
---|
| 164 | future_date.day = future_date.day - 1
|
---|
| 165 | time_diff += 24 * 60 * 60
|
---|
| 166 | end
|
---|
| 167 | date_duration = past_date.diff(future_date)
|
---|
| 168 | hour = (time_diff / 60 / 60).to_i
|
---|
| 169 | minute = ((time_diff - hour*60*60)/60).to_i
|
---|
| 170 | second = (time_diff - hour * 60 *60 - minute * 60).to_i
|
---|
| 171 | fractional_second = ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0
|
---|
| 172 |
|
---|
[167] | 173 | return DvDuration.new(date_duration.value + 'T' +
|
---|
[151] | 174 | hour.to_s + 'H' +
|
---|
| 175 | minute.to_s + 'M' +
|
---|
| 176 | second.to_s + fractional_second.to_s[1..-1] + 'S')
|
---|
| 177 |
|
---|
| 178 | end
|
---|
| 179 |
|
---|
| 180 | private
|
---|
| 181 |
|
---|
| 182 | def split_date_time(date_time)
|
---|
| 183 | /^(.*)T(.*)$/ =~ date_time.as_string
|
---|
[167] | 184 | return DvDate.new($1), DvTime.new($2)
|
---|
[151] | 185 | end
|
---|
[123] | 186 | end
|
---|
[149] | 187 |
|
---|
[167] | 188 | class DvDuration < DvAmount
|
---|
[308] | 189 | include AssumedLibraryTypes::ISO8601DurationModule
|
---|
[149] | 190 | attr_reader :value
|
---|
| 191 |
|
---|
| 192 | def initialize(value, magnitude_status=nil, accuracy=nil,
|
---|
| 193 | accuracy_percent=nil, normal_range=nil,
|
---|
| 194 | normal_status = nil, other_reference_ranges=nil)
|
---|
| 195 | self.value = value
|
---|
| 196 | end
|
---|
| 197 |
|
---|
| 198 | def value=(value)
|
---|
| 199 | raise ArgumentError, 'value must be not nil' if value.nil?
|
---|
| 200 | @value = value
|
---|
[308] | 201 | iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
|
---|
[149] | 202 | self.years = iso8601_duration.years
|
---|
| 203 | self.months = iso8601_duration.months
|
---|
| 204 | self.weeks = iso8601_duration.weeks
|
---|
| 205 | self.days = iso8601_duration.days
|
---|
| 206 | self.hours = iso8601_duration.hours
|
---|
| 207 | self.minutes = iso8601_duration.minutes
|
---|
| 208 | self.seconds = iso8601_duration.seconds
|
---|
| 209 | self.fractional_second = iso8601_duration.fractional_second
|
---|
| 210 | end
|
---|
| 211 |
|
---|
| 212 | def magnitude
|
---|
| 213 | return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
|
---|
| 214 | @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
|
---|
| 215 | SECONDS_IN_MINUTE) + @second + @fractional_second
|
---|
| 216 | end
|
---|
| 217 |
|
---|
| 218 | undef magnitude=
|
---|
| 219 |
|
---|
| 220 | end
|
---|
[167] | 221 | end # of DateTime
|
---|
[4] | 222 | end # of Quantity
|
---|
| 223 | end # of Data_Types
|
---|
| 224 | end # of RM
|
---|
| 225 | end # of OpenEHR
|
---|