source: ruby/trunk/lib/open_ehr/rm/data_types/quantity/date_time.rb@ 397

Last change on this file since 397 was 397, checked in by KOBAYASHI, Shinji, 14 years ago

refs #71

File size: 8.3 KB
Line 
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
4#require 'assumed_library_types'
5require 'date'
6include OpenEHR::RM::DataTypes::Quantity
7include OpenEHR::AssumedLibraryTypes
8
9module OpenEHR
10 module RM
11 module DataTypes
12 module Quantity
13 module DateTime
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]
22 end
23
24 def value=(value)
25 if value.nil? or value.empty?
26 raise ArgumentError, 'invalid value'
27 end
28 @value = value
29 end
30
31 undef magnitude=
32 end
33
34 class DvDate < DvTemporal
35 include ISO8601DateModule
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)
41 iso8601_date = ISO8601Date.new(value)
42 @year = iso8601_date.year
43 @month = iso8601_date.month
44 @day = iso8601_date.day
45 end
46
47 def magnitude
48 return Date.new(@year, @month, @day)-Date.new(0000,1,1)
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
61 month = -1
62 previous_month = future.month - 1
63 if previous_month == 0
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
78 if month < 0
79 year -= 1
80 month += 12
81 end
82 year += future.year - past.year
83 return DvDuration.new(:value =>
84 'P' + year.to_s + 'Y' + month.to_s + 'M' +
85 week.to_s + 'W' + day.to_s + 'D')
86 end
87 end
88
89 class DvTime < DvTemporal
90 include ISO8601TimeModule
91
92 def value=(value)
93 super(value)
94 iso8601_time = ISO8601Time.new(value)
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 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
107 end
108
109 def diff(other)
110 diff = (other.magnitude - self.magnitude).abs
111 hour = (diff / 60 / 60).to_i
112 minute = ((diff - hour*60*60)/60).to_i
113 second = (diff - hour * 60 *60 - minute * 60).to_i
114 fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0
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)
123 end
124 end
125
126 class DvDateTime < DvTemporal
127 include OpenEHR::AssumedLibraryTypes::ISO8601DateTimeModule
128
129 def value=(value)
130 super(value)
131 iso8601date_time = AssumedLibraryTypes::ISO8601DateTime.new(value)
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
143 seconds = (((@year * 365.24 +
144 @month * 30.42 +
145 @day) * 24 + @hour) * 60 +
146 @minute) * 60 + @second
147 if @fractional_second.nil?
148 return seconds
149 else
150 return seconds + @fractional_second
151 end
152 end
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 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
181 end
182
183 def <=>(other)
184 self.magnitude <=> other.magnitude
185 end
186
187 private
188
189 def split_date_time(date_time)
190 /^(.*)T(.*)$/ =~ date_time.as_string
191 return DvDate.new(:value => $1), DvTime.new(:value => $2)
192 end
193 end
194
195 class DvDuration < DvAmount
196 include AssumedLibraryTypes::ISO8601DurationModule
197 attr_reader :value
198
199 def initialize(args = { })
200 self.value = args[:value]
201 self.magnitude_status = args[:magnitude_status]
202 self.normal_range = args[:normal_range]
203 self.normal_status = args[:normal_status]
204 self.other_reference_ranges = args[:other_reference_ranges]
205 end
206
207 def value=(value)
208 raise ArgumentError, 'value must be not nil' if value.nil?
209 @value = value
210 iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
211 self.years = iso8601_duration.years
212 self.months = iso8601_duration.months
213 self.weeks = iso8601_duration.weeks
214 self.days = iso8601_duration.days
215 self.hours = iso8601_duration.hours
216 self.minutes = iso8601_duration.minutes
217 self.seconds = iso8601_duration.seconds
218 self.fractional_second = iso8601_duration.fractional_second
219 end
220
221 def magnitude
222 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
223 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
224 SECONDS_IN_MINUTE) + @second + @fractional_second
225 end
226
227 undef magnitude=
228
229 end
230 end # of DateTime
231 end # of Quantity
232 end # of Data_Types
233 end # of RM
234end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.