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

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

common/generic near completed

File size: 7.7 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
32 undef magnitude=
33 end
34
35 class DvDate < DvTemporal
36 include ISO8601DateModule
37
38 DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31]
39
40 def value=(value)
41 super(value)
42 iso8601_date = ISO8601Date.new(value)
43 @year = iso8601_date.year
44 @month = iso8601_date.month
45 @day = iso8601_date.day
46 end
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 ISO8601TimeModule
92
93 def value=(value)
94 super(value)
95 iso8601_time = ISO8601Time.new(value)
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
111 fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0
112 return DvDuration.new('P0Y0M0W0DT' + hour.to_s + 'H' +
113 minute.to_s + 'M' +
114 second.to_s + fractional_second.to_s[1..-1] + 'S')
115 end
116 end
117
118 class DvDateTime < DvTemporal
119 include OpenEHR::AssumedLibraryTypes::ISO8601DateTimeModule
120
121 def value=(value)
122 super(value)
123 iso8601date_time = AssumedLibraryTypes::ISO8601DateTime.new(value)
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
135 seconds = (((@year * 365.24 +
136 @month * 30.42 +
137 @day) * 24 + @hour) * 60 +
138 @minute) * 60 + @second
139 if @fractional_second.nil?
140 return seconds
141 else
142 return seconds + @fractional_second
143 end
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
163 fractional_second =
164 ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0
165 return DvDuration.new(date_duration.value + 'T' +
166 hour.to_s + 'H' +
167 minute.to_s + 'M' +
168 second.to_s + fractional_second.to_s[1..-1] + 'S')
169 end
170
171 private
172
173 def split_date_time(date_time)
174 /^(.*)T(.*)$/ =~ date_time.as_string
175 return DvDate.new($1), DvTime.new($2)
176 end
177 end
178
179 class DvDuration < DvAmount
180 include AssumedLibraryTypes::ISO8601DurationModule
181 attr_reader :value
182
183 def initialize(value, magnitude_status=nil, accuracy=nil,
184 accuracy_percent=nil, normal_range=nil,
185 normal_status = nil, other_reference_ranges=nil)
186 self.value = value
187 end
188
189 def value=(value)
190 raise ArgumentError, 'value must be not nil' if value.nil?
191 @value = value
192 iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
193 self.years = iso8601_duration.years
194 self.months = iso8601_duration.months
195 self.weeks = iso8601_duration.weeks
196 self.days = iso8601_duration.days
197 self.hours = iso8601_duration.hours
198 self.minutes = iso8601_duration.minutes
199 self.seconds = iso8601_duration.seconds
200 self.fractional_second = iso8601_duration.fractional_second
201 end
202
203 def magnitude
204 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
205 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
206 SECONDS_IN_MINUTE) + @second + @fractional_second
207 end
208
209 undef magnitude=
210
211 end
212 end # of DateTime
213 end # of Quantity
214 end # of Data_Types
215 end # of RM
216end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.