source: ruby/branches/0.5/lib/open_ehr/rm/data_types/quantity/date_time.rb@ 342

Last change on this file since 342 was 342, checked in by KOBAYASHI, Shinji, 15 years ago

ref #55, fixed #54

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