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

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

rm/common/archetyped completed. date_time test/unit moving to rspec

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