Changeset 151 for ruby/trunk/lib/models/rm/data_types/quantity/date_time.rb
- Timestamp:
- Jul 1, 2009, 10:11:42 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/models/rm/data_types/quantity/date_time.rb
r150 r151 3 3 # Ticket refs #49 4 4 require 'assumed_library_types' 5 5 require 'date' 6 6 7 7 module OpenEHR … … 119 119 minute = ((diff - hour*60*60)/60).to_i 120 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 121 fractional_second = ((diff - diff.to_i)*1000000.0).to_i/1000000.0 122 return DV_Duration.new('P0Y0M0W0DT' + hour.to_s + 'H' + 123 minute.to_s + 'M' + 124 second.to_s + fractional_second.to_s[1..-1] + 'S') 125 end 126 end 127 128 class DV_Date_Time < DV_Temporal 129 include OpenEHR::Assumed_Library_Types::ISO8601_DATE_TIME_MODULE 130 attr_reader :value 131 132 def initialize(value, magnitude_status=nil, accuracy=nil, 133 normal_range=nil, normal_status=nil, 134 other_reference_range=nil) 135 super(value, magnitude_status, accuracy, normal_range, 136 normal_status, other_reference_range) 137 end 138 139 def value=(value) 140 super(value) 141 iso8601date_time = Assumed_Library_Types::ISO8601_DATE_TIME.new(value) 142 self.year = iso8601date_time.year 143 self.month = iso8601date_time.month 144 self.day = iso8601date_time.day 145 self.minute = iso8601date_time.minute 146 self.second = iso8601date_time.second 147 self.hour = iso8601date_time.hour 148 self.fractional_second = iso8601date_time.fractional_second 149 self.timezone = iso8601date_time.timezone 150 end 151 152 def magnitude 153 return DateTime.new(@year,@month,@day,@hour,@minute,@second) - 154 DateTime.new(0000,1,1,0,0,0) + @fractional_second 155 end 156 157 undef magnitude= 158 159 def diff(other) 160 if self.magnitude >= other.magnitude 161 past, future = other, self 162 else 163 past, future = self, other 164 end 165 past_date, past_time = split_date_time(past) 166 future_date, future_time = split_date_time(future) 167 time_diff = future_time.magnitude - past_time.magnitude 168 if future_time.magnitude < past_time.magnitude 169 future_date.day = future_date.day - 1 170 time_diff += 24 * 60 * 60 171 end 172 date_duration = past_date.diff(future_date) 173 hour = (time_diff / 60 / 60).to_i 174 minute = ((time_diff - hour*60*60)/60).to_i 175 second = (time_diff - hour * 60 *60 - minute * 60).to_i 176 fractional_second = ((time_diff - time_diff.to_i)*1000000.0).to_i/1000000.0 177 178 return DV_Duration.new(date_duration.value + 'T' + 179 hour.to_s + 'H' + 180 minute.to_s + 'M' + 181 second.to_s + fractional_second.to_s[1..-1] + 'S') 182 183 end 184 185 private 186 187 def split_date_time(date_time) 188 /^(.*)T(.*)$/ =~ date_time.as_string 189 return DV_Date.new($1), DV_Time.new($2) 131 190 end 132 191 end
Note:
See TracChangeset
for help on using the changeset viewer.