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

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

Versioning system is complexed

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