source: ruby/trunk/lib/models/assumed_library_types.rb@ 81

Last change on this file since 81 was 81, checked in by KOBAYASHI, Shinji, 16 years ago

refs #38
modifed valid_iso_8601_time in ISO_8601_TIME
change instance method to class method valid_iso8601_time in ISO_8601_TIME class
change instance method to class method valid_iso8601_date in ISO_8601_date class

File size: 6.6 KB
Line 
1require 'date'
2require 'time'
3
4module OpenEHR
5 module Assumed_Library_Types
6 class Any < Object
7
8 end # of Any
9 class Interval < Any
10 attr_reader :lower, :lower_included, :lower_unbounded
11 attr_reader :upper, :upper_included, :upper_unbounded
12 def initialize(lower, upper,
13 lower_included = nil, upper_included = nil)
14 check_lower_upper(lower, upper)
15 set_lower_included(lower_included)
16 set_upper_included(upper_included)
17 end
18
19 def set_lower(lower)
20 check_lower_upper(lower, @upper)
21 end
22
23 def set_upper(upper)
24 check_lower_upper(@lower, upper)
25 end
26
27 def set_lower_included(lower_included)
28 if (lower == nil) && (lower_included != nil)
29 raise ArgumentError, "lower is not set"
30 end
31 @lower_included = lower_included
32 end
33
34 def set_upper_included(upper_included)
35 @upper_included = upper_included
36 if (upper == nil) && (upper_included != nil)
37 raise ArgumentError, "upper is not set"
38 end
39 end
40
41 def has?(value)
42 if ((@lower < value) && (value < @upper) ||
43 (@lower_included == true) && (@lower == value) ||
44 (@upper_included == true) && (@upper == value))
45 true
46 else
47 false
48 end
49 end
50
51 private
52
53 def check_lower_upper(lower, upper)
54 if (lower == nil) && (upper == nil)
55 raise ArgumentError, "Either lower or upper must be assigned"
56 end
57 if (lower == nil) && (upper != nil)
58 @lower_unbounded = true
59 elsif (lower != nil) && (upper == nil)
60 @upper_unbounded = true
61 elsif lower > upper
62 raise ArgumentError, "Upper must be larger than lower."
63 end
64 @lower = lower
65 @upper = upper
66 end
67 end # end of Interval
68
69 class TIME_DEFINITIONS < Any
70 DAYS_IN_LEAP_YEAR = 366
71 DAYS_IN_WEEK = 7
72 DAYS_IN_YEAR = 365
73 HOURS_IN_DAY = 24
74 MAX_DAYS_IN_MONTH = 31
75 MAX_DAYS_IN_YEAR = 366
76 MINUTES_IN_HOUR = 60
77 MONTH_IN_YEAR = 12
78 NOMINAL_DAYS_IN_MONTH = 30.42
79 NOMINAL_DAYS_IN_YEAR = 365.24
80 SECONDS_IN_MINUTE = 60
81
82 def self.valid_year?(year)
83 year >= 0
84 end
85
86 def self.valid_day?(y, m, d)
87 Date.valid_date?(y,m,d) and valid_year? y
88 end
89
90 def self.valid_hour?(h,m,s)
91 valid_minute?(m) and valid_second?(s) and ((h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0))
92 end
93 def self.valid_minute?(mi)
94 mi >= 0 and mi < MINUTES_IN_HOUR
95 end
96 def self.valid_second?(s)
97 s >= 0 and s < SECONDS_IN_MINUTE
98 end
99 def self.valid_month?(mo)
100 mo >= 1 and mo <= MONTH_IN_YEAR
101 end
102 end # end of TIME_DEFINITIONS
103
104 class ISO8601_DATE < TIME_DEFINITIONS
105 attr_reader :year, :month, :day
106 def initialize(year = nil, month = nil, day = nil)
107 @year = @month = @day = nil
108 if !year.nil?
109 self.year = year
110 end
111 if !month.nil?
112 self.month = month
113 end
114 if !day.nil?
115 self.day = day
116 end
117 end
118 def year=(year)
119 raise ArgumentError, "Year is not valid" if !ISO8601_DATE.valid_year?(year)
120 @year = year
121 end
122 def month=(month)
123 raise ArgumentError, "Month is not valid" if !ISO8601_DATE.valid_month?(month)
124 @month = month
125 end
126 def day=(day)
127 raise ArgumentError, "Day is not valid" if !ISO8601_DATE.valid_day?(@year, @month, day)
128 @day = day
129 end
130 def as_string
131 if (!@year.nil? and !@month.nil? and !@day.nil?)
132 Date.new(@year, @month, @day).to_s
133 elsif (!@year.nil? and !@month.nil? and @day.nil?)
134 Date.new(@year, @month).to_s[0,7]
135 elsif (!@year.nil? and @month.nil? and @day.nil?)
136 Date.new(@year).to_s[0,4]
137 end
138 end
139 def month_unknown?
140 @month.nil?
141 end
142 def day_unknown?
143 @day.nil?
144 end
145 def is_extended?
146 true
147 end
148 def is_partial?
149 month_unknown? or day_unknown?
150 end
151 def self.valid_iso8601_date?(string)
152 begin
153 Date.parse(string)
154 rescue
155 return false
156 end
157 true
158 end
159
160 end # end of ISO8601_DATE
161
162 class ISO8601_TIME < TIME_DEFINITIONS
163 attr_reader :hh, :mm, :ss, :msec, :tz
164 def initialize(hh = nil, mm = nil, ss = nil, msec = nil, tz = nil)
165
166 end
167 def as_string
168 s = @hh
169 if !@mm.nil?
170 s += ":" + @mm
171 if !@ss.nil?
172 s += ":" + @ss
173 if !@msec.nil?
174 s += "," + @msec
175 if !@tz.nil?
176 s += @tz
177 end
178 end
179 end
180 end
181 s
182 end
183 def self.valid_iso8601_time?(s)
184 if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
185# ISO 8601 regular expression by H. Yuki
186# http://digit.que.ne.jp/work/wiki.cgi?Perl%E3%83%A1%E3%83%A2%2FW3C%E5%BD%A2%E5%BC%8F%E3%81%AE%E6%97%A5%E6%99%82%E3%81%AE%E8%A7%A3%E6%9E%90
187# (\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?
188 hh = $1; mm = $2; ss = $4; msec = $7; tz = $8
189 if hh.to_i == HOURS_IN_DAY and (mm.nil? or mm.to_i == "00") and (ss.nil? or ss.to_i == "00")
190 return true
191 end
192 if hh.nil? or (hh.to_i < 0 or hh.to_i >= HOURS_IN_DAY)
193 return false
194 end
195 if !mm.nil?
196 if !self.valid_minute?(mm.to_i)
197 return false
198 end
199 end
200 if !ss.nil?
201 if !self.valid_second?(ss.to_i)
202 return false
203 end
204 end
205 if !tz.nil? and tz != "Z"
206 if /[+-](\d{2}):?(\d{2})/ =~ tz
207 h = $1; m = $2
208 if h.to_i < 0 or h.to_i >= HOURS_IN_DAY
209 return false
210 end
211 if m.to_i < 0 or m.to_i >= MINUTES_IN_HOUR
212 return false
213 end
214 else
215 return false
216 end
217 end
218 return true
219 else
220 return false
221 end
222 end
223 end # end of ISO8601_TIME
224
225 class ISO8601_TIMEZONE
226 attr_accessor :sign, :hour, :minute
227 def is_gmt?
228 @sign == "+1" and @hour == 0 and @minute == 0
229 end
230 def as_string
231 if @sign == "+1"
232 s = "+"
233 elsif @sign == "-1"
234 s = "-"
235 end
236 sprintf("Z%s%02d%02d", s, @hour, @minute)
237 end
238 end # end of ISO8601_TIMEZONE
239 end # end of Assumed_Types
240end # end of OpenEHR
Note: See TracBrowser for help on using the repository browser.