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

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

ref #26,#50

File size: 14.4 KB
Line 
1# This module is related to the ticket #36
2require 'date'
3require 'time'
4
5module OpenEHR
6 module Assumed_Library_Types
7 class Any < Object
8
9 end # of Any
10
11 class Interval < Any
12 attr_reader :lower, :upper
13
14 def initialize(lower, upper,
15 lower_included = nil, upper_included = nil)
16 check_lower_upper(lower, upper)
17 self.lower_included = lower_included
18 self.upper_included = upper_included
19 end
20
21 def lower=(lower)
22 check_lower_upper(lower, @upper)
23 end
24
25 def upper=(upper)
26 check_lower_upper(@lower, upper)
27 end
28
29 def lower_included?
30 return @lower_included
31 end
32
33 def lower_included=(lower_included)
34 if (lower == nil) && (lower_included != nil)
35 raise ArgumentError, "lower is not set"
36 end
37 @lower_included = lower_included
38 end
39
40 def lower_unbounded?
41 return @lower.nil?
42 end
43
44 def upper_included?
45 return @upper_included
46 end
47
48 def upper_included=(upper_included)
49 if (upper == nil) && (upper_included != nil)
50 raise ArgumentError, "upper is not set"
51 end
52 @upper_included = upper_included
53 end
54
55 def upper_unbounded?
56 return @upper.nil?
57 end
58
59 def has?(value)
60 if ((@lower < value) && (value < @upper) ||
61 (@lower_included == true) && (@lower == value) ||
62 (@upper_included == true) && (@upper == value))
63 true
64 else
65 false
66 end
67 end
68
69 private
70
71 def check_lower_upper(lower, upper)
72 if (lower == nil) && (upper == nil)
73 raise ArgumentError, "Either lower or upper must be assigned"
74 end
75 unless (lower.nil? || upper.nil?)
76 if lower > upper
77 raise ArgumentError, "Upper must be larger than lower."
78 end
79 end
80 @lower = lower
81 @upper = upper
82 end
83 end # end of Interval
84
85 class TIME_DEFINITIONS < Any
86 DAYS_IN_LEAP_YEAR = 366
87 DAYS_IN_WEEK = 7
88 DAYS_IN_YEAR = 365
89 HOURS_IN_DAY = 24
90 MAX_DAYS_IN_MONTH = 31
91 MAX_DAYS_IN_YEAR = 366
92 MINUTES_IN_HOUR = 60
93 MONTH_IN_YEAR = 12
94 NOMINAL_DAYS_IN_MONTH = 30.42
95 NOMINAL_DAYS_IN_YEAR = 365.24
96 SECONDS_IN_MINUTE = 60
97
98 def self.valid_year?(year)
99 year >= 0
100 end
101
102 def self.valid_day?(y, m, d)
103 Date.valid_date?(y,m,d) and valid_year? y
104 end
105
106 def self.valid_hour?(h,m = nil, s = nil)
107 if !m.nil? and !valid_minute?(m)
108 return false
109 end
110 if !s.nil? and (!m.nil? and !valid_second?(s))
111 return false
112 end
113 (h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0)
114 end
115 def self.valid_minute?(mi)
116 mi >= 0 and mi < MINUTES_IN_HOUR
117 end
118 def self.valid_second?(s)
119 s >= 0 and s < SECONDS_IN_MINUTE
120 end
121 def self.valid_month?(mo)
122 mo >= 1 and mo <= MONTH_IN_YEAR
123 end
124 end # end of TIME_DEFINITIONS
125
126 module ISO8601_DATE_MODULE
127 attr_reader :year, :month, :day
128 def year=(year)
129 raise ArgumentError, "Year is not valid" unless ISO8601_DATE.valid_year?(year)
130 @year = year
131 end
132 def month=(month)
133 raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601_DATE.valid_month?(month)
134 @month = month
135 end
136
137 def day=(day)
138 raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601_DATE.valid_day?(@year, @month, day)
139 @day = day
140 end
141
142 def as_string
143 if (!@year.nil? and !@month.nil? and !@day.nil?)
144 Date.new(@year, @month, @day).to_s
145 elsif (!@year.nil? and !@month.nil? and @day.nil?)
146 Date.new(@year, @month).to_s[0,7]
147 elsif (!@year.nil? and @month.nil? and @day.nil?)
148 Date.new(@year).to_s[0,4]
149 end
150 end
151
152 def month_unknown?
153 @month.nil?
154 end
155
156 def day_unknown?
157 @day.nil?
158 end
159
160 def is_extended?
161 true
162 end
163
164 def is_partial?
165 month_unknown? or day_unknown?
166 end
167 end
168
169 class ISO8601_DATE < TIME_DEFINITIONS
170 include ISO8601_DATE_MODULE
171 def initialize(string)
172 /(\d{4})(?:-(\d{2})(?:-(\d{2})?)?)?/ =~ string
173 if $1.nil?
174 raise ArgumentError, 'data invalid'
175 else
176 self.year = $1.to_i
177 end
178 if $2.nil?
179 self.month = nil
180 else
181 self.month = $2.to_i
182 end
183 if $3.nil?
184 self.day = nil
185 else
186 self.day = $3.to_i
187 end
188 end
189 def self.valid_iso8601_date?(string)
190 begin
191 Date.parse(string)
192 rescue
193 return false
194 end
195 true
196 end
197 end # end of ISO8601_DATE
198
199 module ISO8601_TIME_MODULE
200 attr_reader :hour, :minute, :second, :fractional_second, :timezone
201
202 def hour=(hour)
203 raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second)
204 @hour = hour
205 end
206
207 def minute_unknown?
208 @minute.nil?
209 end
210
211 def minute=(minute)
212 raise ArgumentError, "minute is not valid" if !minute.nil? and !ISO8601_TIME.valid_minute?(minute)
213 @minute = minute
214 end
215
216 def second_unknown?
217 @second.nil?
218 end
219
220 def second=(second)
221 raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil?
222 raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601_TIME.valid_second?(second)
223 @second = second
224 end
225
226 def fractional_second=(fractional_second)
227 raise ArgumentError, "minute not defined" if minute_unknown? and !fractional_second.nil?
228 raise ArgumentError, "second not defined" if second_unknown? and !fractional_second.nil?
229 raise ArgumentError, "fractional second should be lower than 1.0" if !fractional_second.nil? and fractional_second >= 1.0
230 @fractional_second = fractional_second
231 end
232
233 def has_fractional_second?
234 if @fractional_second.nil?
235 return false
236 else
237 return true
238 end
239 end
240
241 def timezone=(timezone)
242 unless timezone.nil? or timezone == 'Z'
243 if /[+-](\d{2}):?(\d{2})/ =~ timezone
244 @timezone = timezone
245 else
246 raise ArgumentError, "timezone invalid"
247 end
248 else
249 @timezone = nil
250 end
251 end
252
253 def is_decimal_sign_comma?
254 false
255 end
256
257 def is_extended?
258 true
259 end
260
261 def is_partial?
262 second_unknown? or minute_unknown?
263 end
264
265 def as_string
266 s = sprintf("%02d", @hour)
267 if !@minute.nil?
268 s += ":" + sprintf("%02d",@minute)
269 if !@second.nil?
270 s += ":" + sprintf("%02d", @second)
271 if !@fractional_second.nil?
272 s += "." + @fractional_second.to_s[2..-1]
273 if !@timezone.nil?
274 s += @timezone
275 end
276 end
277 end
278 end
279 return s
280 end
281 end
282
283 class ISO8601_TIME < TIME_DEFINITIONS
284 include ISO8601_TIME_MODULE
285 def initialize(string)
286 /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ string
287 if $2.nil?
288 self.minute = nil
289 else
290 self.minute = $2.to_i
291 end
292 if $4.nil?
293 self.second = nil
294 else
295 self.second = $4.to_i
296 end
297 if $1.nil?
298 raise ArgumentError, 'data invalid'
299 else
300 self.hour = $1.to_i
301 end
302 if $7.nil?
303 self.fractional_second = nil
304 else
305 self.fractional_second = ("0." + $7).to_f
306 end
307 if $8.nil?
308 self.timezone = nil
309 else
310 self.timezone = $8
311 end
312 end
313 def self.valid_iso8601_time?(s)
314 if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
315# ISO 8601 regular expression by H. Yuki
316# 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
317# (\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?
318 hh = $1; mm = $2; ss = $4; msec = $7; tz = $8
319 if hh.to_i == HOURS_IN_DAY and (mm.nil? or mm.to_i == 0) and (ss.nil? or ss.to_i == 0) and (msec.nil? or msec.to_i==0)
320 return true
321 end
322 if hh.nil? or (hh.to_i < 0 or hh.to_i >= HOURS_IN_DAY)
323 return false
324 end
325 if !mm.nil?
326 if !self.valid_minute?(mm.to_i)
327 return false
328 end
329 end
330 if !ss.nil?
331 if !self.valid_second?(ss.to_i)
332 return false
333 end
334 end
335 if !tz.nil? and tz != "Z"
336 if /[+-](\d{2}):?(\d{2})/ =~ tz
337 h = $1; m = $2
338 if h.to_i < 0 or h.to_i >= HOURS_IN_DAY
339 return false
340 end
341 if m.to_i < 0 or m.to_i >= MINUTES_IN_HOUR
342 return false
343 end
344 else
345 return false
346 end
347 end
348 return true
349 else
350 return false
351 end
352 end
353 end # end of ISO8601_TIME
354
355 class ISO8601_DATE_TIME < ISO8601_DATE
356 include ISO8601_TIME_MODULE, ISO8601_DATE_MODULE
357 def initialize(string)
358 /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string
359 if $1.empty?
360 raise ArgumentError, 'format invalid'
361 else
362 self.year = $1.to_i
363 end
364 if $2.nil?
365 self.month = nil
366 else
367 self.month = $2.to_i
368 end
369 if $3.nil?
370 self.day = nil
371 else
372 self.day = $3.to_i
373 end
374 if $5.nil?
375 self.minute = nil
376 else
377 self.minute = $5.to_i
378 end
379 if $6.nil?
380 self.second = nil
381 else
382 self.second = $6.to_i
383 end
384 if $4.nil?
385 self.hour = nil
386 else
387 self.hour = $4.to_i
388 end
389 if $7.nil? or $7.empty?
390 self.fractional_second = nil
391 else
392 self.fractional_second = ("0."+$7).to_f
393 end
394 if $8.nil?
395 self.timezone = nil
396 else
397 self.timezone = $9+$10
398 end
399 end
400
401 def as_string
402 if (!@year.nil? and !@month.nil? and !@day.nil?)
403 s = Date.new(@year, @month, @day).to_s
404 elsif (!@year.nil? and !@month.nil? and @day.nil?)
405 return Date.new(@year, @month).to_s[0,7]
406 elsif (!@year.nil? and @month.nil? and @day.nil?)
407 return Date.new(@year).to_s[0,4]
408 end
409 unless hour.nil?
410 s += sprintf("T%02d", @hour)
411 unless @minute.nil?
412 s += ":" + sprintf("%02d",@minute)
413 unless @second.nil?
414 s += ":" + sprintf("%02d", @second)
415 unless @fractional_second.nil?
416 s += "." + @fractional_second.to_s[2..-1]
417 unless @timezone.nil?
418 s += @timezone
419 end
420 end
421 end
422 end
423 end
424 return s
425 end
426
427 end
428
429 class ISO8601_TIMEZONE
430 attr_accessor :sign, :hour, :minute
431
432 def is_gmt?
433 @sign == "+1" and @hour == 0 and @minute == 0
434 end
435
436 def as_string
437 if @sign == "+1"
438 s = "+"
439 elsif @sign == "-1"
440 s = "-"
441 end
442 sprintf("Z%s%02d%02d", s, @hour, @minute)
443 end
444 end # end of ISO8601_TIMEZONE
445
446 class ISO8601_DURATION < TIME_DEFINITIONS
447 attr_reader :years, :months, :weeks, :days
448 attr_reader :hours, :minutes, :seconds, :fractional_second
449
450 def initialize(str)
451 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str
452 self.years = $2.to_i
453 self.months = $4.to_i
454 self.weeks = $6.to_i
455 self.days = $8.to_i
456 self.hours = $11.to_i
457 self.minutes = $13.to_i
458 self.seconds = $15.to_i
459 self.fractional_second = $16.to_f
460 end
461
462 def years=(years)
463 unless years.nil? || years >= 0
464 raise ArgumentError, 'years must be above zero'
465 end
466 @years = years
467 end
468
469 def months=(months)
470 unless months.nil? || months >= 0
471 raise ArgumentError, 'months must be above zero'
472 end
473 @months = months
474 end
475
476 def weeks=(weeks)
477 unless weeks.nil? || weeks >= 0
478 raise ArgumentError, 'weeks must be above zero'
479 end
480 @weeks = weeks
481 end
482
483 def days=(days)
484 unless days.nil? || days >= 0
485 raise ArgumentError, 'days must be above zero'
486 end
487 @days = days
488 end
489
490 def hours=(hours)
491 unless hours.nil? || hours >= 0
492 raise ArgumentError, 'hours must be above zero'
493 end
494 @hours = hours
495 end
496
497 def minutes=(minutes)
498 unless minutes.nil? || minutes >= 0
499 raise ArgumentError, 'minutes must be above zero'
500 end
501 @minutes = minutes
502 end
503
504 def seconds=(seconds)
505 unless seconds.nil? || seconds >= 0
506 raise ArgumentError, 'seconds must be above zero'
507 end
508 @seconds = seconds
509 end
510
511 def fractional_second=(fractional_second)
512 unless fractional_second.nil? || (fractional_second >= 0 && fractional_second < 1.0)
513 raise ArgumentError, 'fractional_second must be between 0.0 and 1.0'
514 end
515 @fractional_second = fractional_second
516 end
517
518 def as_string
519 str = 'P'
520 unless @years.nil?
521 str += @years.to_s + 'Y'
522 end
523 unless @months.nil?
524 str += @months.to_s + 'M'
525 end
526 unless @weeks.nil?
527 str += @weeks.to_s + 'W'
528 end
529 unless @days.nil?
530 str += @days.to_s + 'D'
531 end
532 unless @hours.nil?
533 str += 'T' + @hours.to_s + 'H'
534 unless @minutes.nil?
535 str += @minutes.to_s + 'M'
536 unless @seconds.nil?
537 str += @seconds.to_s
538 unless @fractional_second.nil?
539 str += @fractional_second.to_s[1 .. -1]
540 end
541 str += 'S'
542 end
543 end
544 end
545 return str
546 end
547 end # end of ISO8601_DURATION
548 end # end of Assumed_Types
549end # end of OpenEHR
Note: See TracBrowser for help on using the repository browser.