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

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

DvDate completed

File size: 8.5 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 if month < 0
80 year -= 1
81 month += 12
82 end
83 year += future.year - past.year
84 return DvDuration.new(
85 'P' + year.to_s + 'Y' + month.to_s + 'M' +
86 week.to_s + 'W' + day.to_s + 'D')
87 end
88 end
89
90 class DvTime < DvTemporal
91 include OpenEHR::AssumedLibraryTypes::ISO8601TimeModule
92 def initialize(value, magnitude_status=nil, accuracy=nil,
93 normal_range=nil, normal_status=nil,
94 other_reference_range=nil)
95 super(value, magnitude_status, accuracy, normal_range,
96 normal_status, other_reference_range)
97 end
98
99 def value=(value)
100 super(value)
101 iso8601_time = AssumedLibraryTypes::ISO8601Time.new(value)
102 @hour = iso8601_time.hour
103 @minute = iso8601_time.minute
104 @second = iso8601_time.second
105 @fractional_second = iso8601_time.fractional_second
106 end
107
108 def magnitude
109 return @hour * 60 * 60 + @minute * 60 + @second + @fractional_second
110 end
111
112 def diff(other)
113 diff = (other.magnitude - self.magnitude).abs
114 hour = (diff / 60 / 60).to_i
115 minute = ((diff - hour*60*60)/60).to_i
116 second = (diff - hour * 60 *60 - minute * 60).to_i
117 fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0
118 return DvDuration.new('P0Y0M0W0DT' + hour.to_s + 'H' +
119 minute.to_s + 'M' +
120 second.to_s + fractional_second.to_s[1..-1] + 'S')
121 end
122 end
123
124 class DvDateTime < DvTemporal
125 include OpenEHR::AssumedLibraryTypes::ISO8601DateTimeModule
126 attr_reader :value
127
128 def initialize(value, magnitude_status=nil, accuracy=nil,
129 normal_range=nil, normal_status=nil,
130 other_reference_range=nil)
131 super(value, magnitude_status, accuracy, normal_range,
132 normal_status, other_reference_range)
133 end
134
135 def value=(value)
136 super(value)
137 iso8601date_time = AssumedLibraryTypes::ISO8601DateTime.new(value)
138 self.year = iso8601date_time.year
139 self.month = iso8601date_time.month
140 self.day = iso8601date_time.day
141 self.minute = iso8601date_time.minute
142 self.second = iso8601date_time.second
143 self.hour = iso8601date_time.hour
144 self.fractional_second = iso8601date_time.fractional_second
145 self.timezone = iso8601date_time.timezone
146 end
147
148 def magnitude
149 seconds = ::DateTime.new(@year,@month,@day,@hour,@minute,@second) -
150 ::DateTime.new(0000,1,1,0,0,0)
151 if @fractional_second.nil?
152 return seconds
153 else
154 return seconds + @fractional_second
155 end
156 end
157
158 undef magnitude=
159
160 def diff(other)
161 if self.magnitude >= other.magnitude
162 past, future = other, self
163 else
164 past, future = self, other
165 end
166 past_date, past_time = split_date_time(past)
167 future_date, future_time = split_date_time(future)
168 time_diff = future_time.magnitude - past_time.magnitude
169 if future_time.magnitude < past_time.magnitude
170 future_date.day = future_date.day - 1
171 time_diff += 24 * 60 * 60
172 end
173 date_duration = past_date.diff(future_date)
174 hour = (time_diff / 60 / 60).to_i
175 minute = ((time_diff - hour*60*60)/60).to_i
176 second = (time_diff - hour * 60 *60 - minute * 60).to_i
177 fractional_second = ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0
178
179 return DvDuration.new(date_duration.value + 'T' +
180 hour.to_s + 'H' +
181 minute.to_s + 'M' +
182 second.to_s + fractional_second.to_s[1..-1] + 'S')
183
184 end
185
186 private
187
188 def split_date_time(date_time)
189 /^(.*)T(.*)$/ =~ date_time.as_string
190 return DvDate.new($1), DvTime.new($2)
191 end
192 end
193
194 class DvDuration < DvAmount
195 include AssumedLibraryTypes::ISO8601DurationModule
196 attr_reader :value
197
198 def initialize(value, magnitude_status=nil, accuracy=nil,
199 accuracy_percent=nil, normal_range=nil,
200 normal_status = nil, other_reference_ranges=nil)
201 self.value = value
202 end
203
204 def value=(value)
205 raise ArgumentError, 'value must be not nil' if value.nil?
206 @value = value
207 iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
208 self.years = iso8601_duration.years
209 self.months = iso8601_duration.months
210 self.weeks = iso8601_duration.weeks
211 self.days = iso8601_duration.days
212 self.hours = iso8601_duration.hours
213 self.minutes = iso8601_duration.minutes
214 self.seconds = iso8601_duration.seconds
215 self.fractional_second = iso8601_duration.fractional_second
216 end
217
218 def magnitude
219 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
220 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
221 SECONDS_IN_MINUTE) + @second + @fractional_second
222 end
223
224 undef magnitude=
225
226 end
227 end # of DateTime
228 end # of Quantity
229 end # of Data_Types
230 end # of RM
231end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.