source: ruby/trunk/lib/models/rm/data_types/quantity/date_time.rb@ 150

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

refs #49

File size: 6.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
4require 'assumed_library_types'
5
6
7module OpenEHR
8 module RM
9 module Data_Types
10 module Quantity
11 module Date_Time
12 class DV_Temporal < OpenEHR::RM::Data_Types::Quantity::DV_Absolute_Quantity
13 attr_reader :value
14
15 def initialize(value, magnitude_status=nil, accuracy=nil,
16 normal_range=nil, normal_status=nil,
17 other_reference_ranges=nil)
18 self.value = value
19 self.magnitude_status = magnitude_status
20 self.accuracy = accuracy
21 self.normal_range = normal_range
22 self.normal_status = normal_status
23 self.other_reference_ranges = other_reference_ranges
24 end
25
26 def value=(value)
27 if value.nil? or value.empty?
28 raise ArgumentError, 'invalid value'
29 end
30 @value = value
31 end
32 end
33
34 class DV_Date < DV_Temporal
35 include OpenEHR::Assumed_Library_Types::ISO8601_DATE_MODULE
36
37 DAYS_IN_MONTH = [0,31,28,31,30,31,30,31,31,30,31,30,31]
38
39 def initialize(value, magnitude_status=nil, accuracy=nil,
40 normal_range=nil, normal_status=nil,
41 other_reference_range=nil)
42 super(value, magnitude_status, accuracy, normal_range,
43 normal_status, other_reference_range)
44 end
45
46 def value=(value)
47 super(value)
48 iso8601_date = Assumed_Library_Types::ISO8601_DATE.new(value)
49 @year = iso8601_date.year
50 @month = iso8601_date.month
51 @day = iso8601_date.day
52 end
53
54 undef magnitude=
55
56 def magnitude
57 return Date.new(@year, @month, @day)-Date.new(0000,1,1)
58 end
59
60 def diff(other)
61 if self.magnitude > other.magnitude
62 past, future = other, self
63 else
64 past, future = self, other
65 end
66 year, month, day = 0, 0, 0
67 if (future.day >= past.day)
68 day = future.day - past.day
69 else
70 month -= 1
71 previous_month = future.month - 1
72 if previous_month <= 0
73 previous_month = 12
74 end
75 day = DAYS_IN_MONTH[previous_month] + future.day - past.day
76 if leapyear?(future.year) && (previous_month == 2)
77 day += 1
78 end
79 end
80 week = day / 7
81 if (future.month >= past.month)
82 month += future.month - past.month
83 else
84 year -= 1
85 month += future.month + 12 - past.month
86 end
87 year += future.year - past.year
88 return DV_Duration.new(
89 'P' + year.to_s + 'Y' + month.to_s + 'M' +
90 week.to_s + 'W' + day.to_s + 'D')
91 end
92 end
93
94 class DV_Time < DV_Temporal
95 include OpenEHR::Assumed_Library_Types::ISO8601_TIME_MODULE
96 def initialize(value, magnitude_status=nil, accuracy=nil,
97 normal_range=nil, normal_status=nil,
98 other_reference_range=nil)
99 super(value, magnitude_status, accuracy, normal_range,
100 normal_status, other_reference_range)
101 end
102
103 def value=(value)
104 super(value)
105 iso8601_time = Assumed_Library_Types::ISO8601_TIME.new(value)
106 @hour = iso8601_time.hour
107 @minute = iso8601_time.minute
108 @second = iso8601_time.second
109 @fractional_second = iso8601_time.fractional_second
110 end
111
112 def magnitude
113 return @hour * 60 * 60 + @minute * 60 + @second + @fractional_second
114 end
115
116 def diff(other)
117 diff = (other.magnitude - self.magnitude).abs
118 hour = (diff / 60 / 60).to_i
119 minute = ((diff - hour*60*60)/60).to_i
120 second = (diff - hour * 60 *60 - minute * 60).to_i
121 fractional_second = ((diff - diff.to_i)*1000.0).to_i/1000.0
122 return 'P0Y0M0W0DT' + hour.to_s + 'H' + minute.to_s + 'M' +
123 second.to_s + fractional_second.to_s[1..-1] + 'S'
124 end
125 end
126
127 class DV_Date_Time < DV_Date
128 include OpenEHR::Assumed_Library_Types::ISO8601_TIME_MODULE
129
130 def initialize
131 end
132 end
133
134 class DV_Duration < DV_Amount
135 include Assumed_Library_Types::ISO8601_DURATION_MODULE
136 attr_reader :value
137
138 def initialize(value, magnitude_status=nil, accuracy=nil,
139 accuracy_percent=nil, normal_range=nil,
140 normal_status = nil, other_reference_ranges=nil)
141 self.value = value
142 end
143
144 def value=(value)
145 raise ArgumentError, 'value must be not nil' if value.nil?
146 @value = value
147 iso8601_duration = Assumed_Library_Types::ISO8601_DURATION.new(value)
148 self.years = iso8601_duration.years
149 self.months = iso8601_duration.months
150 self.weeks = iso8601_duration.weeks
151 self.days = iso8601_duration.days
152 self.hours = iso8601_duration.hours
153 self.minutes = iso8601_duration.minutes
154 self.seconds = iso8601_duration.seconds
155 self.fractional_second = iso8601_duration.fractional_second
156 end
157
158 def magnitude
159 return ((((@year + @month/MONTH_IN_YEAR)*NOMINAL_DAYS_IN_MONTH) +
160 @week * DAYS_IN_WEEK + @days) * HOURS_IN_DAY * MINUTES_IN_HOUR*
161 SECONDS_IN_MINUTE) + @second + @fractional_second
162 end
163
164 undef magnitude=
165
166 end
167 end # of Date_Time
168 end # of Quantity
169 end # of Data_Types
170 end # of RM
171end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.