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

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

ref #55
time calculation is difficult

File size: 8.2 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 private
184
185 def split_date_time(date_time)
186 /^(.*)T(.*)$/ =~ date_time.as_string
187 return DvDate.new(:value => $1), DvTime.new(:value => $2)
188 end
189 end
190
191 class DvDuration < DvAmount
192 include AssumedLibraryTypes::ISO8601DurationModule
193 attr_reader :value
194
195 def initialize(args = { })
196 self.value = args[:value]
197 self.magnitude_status = args[:magnitude_status]
198 self.normal_range = args[:normal_range]
199 self.normal_status = args[:normal_status]
200 self.other_reference_ranges = args[:other_reference_ranges]
201 end
202
203 def value=(value)
204 raise ArgumentError, 'value must be not nil' if value.nil?
205 @value = value
206 iso8601_duration = AssumedLibraryTypes::ISO8601Duration.new(value)
207 self.years = iso8601_duration.years
208 self.months = iso8601_duration.months
209 self.weeks = iso8601_duration.weeks
210 self.days = iso8601_duration.days
211 self.hours = iso8601_duration.hours
212 self.minutes = iso8601_duration.minutes
213 self.seconds = iso8601_duration.seconds
214 self.fractional_second = iso8601_duration.fractional_second
215 end
216
217 def magnitude
218 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
219 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
220 SECONDS_IN_MINUTE) + @second + @fractional_second
221 end
222
223 undef magnitude=
224
225 end
226 end # of DateTime
227 end # of Quantity
228 end # of Data_Types
229 end # of RM
230end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.