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

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

refs #49

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