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
RevLine 
[83]1# This module is related to the ticket #36
[70]2require 'date'
[81]3require 'time'
[70]4
[4]5module OpenEHR
6 module Assumed_Library_Types
[69]7 class Any < Object
8
9 end # of Any
[143]10
[69]11 class Interval < Any
[143]12 attr_reader :lower, :upper
13
[4]14 def initialize(lower, upper,
15 lower_included = nil, upper_included = nil)
16 check_lower_upper(lower, upper)
[143]17 self.lower_included = lower_included
18 self.upper_included = upper_included
[4]19 end
[21]20
[143]21 def lower=(lower)
[4]22 check_lower_upper(lower, @upper)
23 end
[21]24
[143]25 def upper=(upper)
[4]26 check_lower_upper(@lower, upper)
27 end
[21]28
[143]29 def lower_included?
30 return @lower_included
31 end
32
33 def lower_included=(lower_included)
[4]34 if (lower == nil) && (lower_included != nil)
35 raise ArgumentError, "lower is not set"
36 end
37 @lower_included = lower_included
38 end
[143]39
40 def lower_unbounded?
41 return @lower.nil?
42 end
[21]43
[143]44 def upper_included?
45 return @upper_included
46 end
47
48 def upper_included=(upper_included)
[4]49 if (upper == nil) && (upper_included != nil)
50 raise ArgumentError, "upper is not set"
51 end
[143]52 @upper_included = upper_included
[4]53 end
[21]54
[143]55 def upper_unbounded?
56 return @upper.nil?
57 end
58
[4]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
[21]68
[4]69 private
[21]70
[4]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
[143]75 unless (lower.nil? || upper.nil?)
76 if lower > upper
77 raise ArgumentError, "Upper must be larger than lower."
78 end
[4]79 end
80 @lower = lower
81 @upper = upper
[143]82 end
[69]83 end # end of Interval
[143]84
[69]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
[70]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
[83]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)
[70]114 end
[81]115 def self.valid_minute?(mi)
116 mi >= 0 and mi < MINUTES_IN_HOUR
[70]117 end
118 def self.valid_second?(s)
119 s >= 0 and s < SECONDS_IN_MINUTE
120 end
[81]121 def self.valid_month?(mo)
122 mo >= 1 and mo <= MONTH_IN_YEAR
[70]123 end
[69]124 end # end of TIME_DEFINITIONS
125
[120]126 module ISO8601_DATE_MODULE
[70]127 attr_reader :year, :month, :day
128 def year=(year)
[120]129 raise ArgumentError, "Year is not valid" unless ISO8601_DATE.valid_year?(year)
[70]130 @year = year
131 end
132 def month=(month)
[120]133 raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601_DATE.valid_month?(month)
[70]134 @month = month
135 end
[125]136
[70]137 def day=(day)
[120]138 raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601_DATE.valid_day?(@year, @month, day)
[70]139 @day = day
140 end
[125]141
[70]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
[125]151
[70]152 def month_unknown?
153 @month.nil?
154 end
[125]155
[70]156 def day_unknown?
157 @day.nil?
158 end
[125]159
[70]160 def is_extended?
161 true
162 end
[125]163
[70]164 def is_partial?
165 month_unknown? or day_unknown?
166 end
[120]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
[81]189 def self.valid_iso8601_date?(string)
[70]190 begin
[81]191 Date.parse(string)
[70]192 rescue
193 return false
194 end
195 true
196 end
[79]197 end # end of ISO8601_DATE
198
[120]199 module ISO8601_TIME_MODULE
[83]200 attr_reader :hour, :minute, :second, :fractional_second, :timezone
[120]201
[83]202 def hour=(hour)
203 raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second)
204 @hour = hour
205 end
[124]206
[83]207 def minute_unknown?
208 @minute.nil?
209 end
[124]210
[83]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
[124]215
[83]216 def second_unknown?
217 @second.nil?
218 end
[124]219
[83]220 def second=(second)
[120]221 raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil?
[83]222 raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601_TIME.valid_second?(second)
223 @second = second
224 end
[124]225
[83]226 def fractional_second=(fractional_second)
[120]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
[83]230 @fractional_second = fractional_second
231 end
[124]232
[83]233 def has_fractional_second?
234 if @fractional_second.nil?
235 return false
236 else
237 return true
238 end
239 end
[124]240
[120]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
[124]252
[83]253 def is_decimal_sign_comma?
[126]254 false
[83]255 end
[124]256
[83]257 def is_extended?
258 true
259 end
[124]260
[83]261 def is_partial?
262 second_unknown? or minute_unknown?
263 end
[125]264
[81]265 def as_string
[83]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?
[125]272 s += "." + @fractional_second.to_s[2..-1]
[83]273 if !@timezone.nil?
274 s += @timezone
[81]275 end
276 end
277 end
278 end
[125]279 return s
[81]280 end
[120]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
[81]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
[83]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)
[81]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
[79]353 end # end of ISO8601_TIME
354
[120]355 class ISO8601_DATE_TIME < ISO8601_DATE
[125]356 include ISO8601_TIME_MODULE, ISO8601_DATE_MODULE
[120]357 def initialize(string)
[121]358 /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-]\d{2}):?(\d{2}))?)?)?)?/ =~ string
[120]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
[121]389 if $7.nil? or $7.empty?
[120]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
[121]397 self.timezone = $9+$10
[120]398 end
399 end
[125]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]
[126]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
[125]420 end
421 end
422 end
423 end
424 return s
425 end
426
[120]427 end
428
[79]429 class ISO8601_TIMEZONE
430 attr_accessor :sign, :hour, :minute
[124]431
[79]432 def is_gmt?
[80]433 @sign == "+1" and @hour == 0 and @minute == 0
[79]434 end
[124]435
[79]436 def as_string
[80]437 if @sign == "+1"
438 s = "+"
439 elsif @sign == "-1"
440 s = "-"
441 end
442 sprintf("Z%s%02d%02d", s, @hour, @minute)
[79]443 end
444 end # end of ISO8601_TIMEZONE
[123]445
[126]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
[123]460 end
461
[126]462 def years=(years)
[128]463 unless years.nil? || years >= 0
[126]464 raise ArgumentError, 'years must be above zero'
465 end
466 @years = years
467 end
[124]468
[126]469 def months=(months)
[128]470 unless months.nil? || months >= 0
[126]471 raise ArgumentError, 'months must be above zero'
472 end
473 @months = months
[124]474 end
[126]475
476 def weeks=(weeks)
[128]477 unless weeks.nil? || weeks >= 0
[126]478 raise ArgumentError, 'weeks must be above zero'
479 end
480 @weeks = weeks
481 end
482
483 def days=(days)
[128]484 unless days.nil? || days >= 0
[126]485 raise ArgumentError, 'days must be above zero'
486 end
487 @days = days
488 end
489
490 def hours=(hours)
[128]491 unless hours.nil? || hours >= 0
[126]492 raise ArgumentError, 'hours must be above zero'
493 end
494 @hours = hours
495 end
496
497 def minutes=(minutes)
[128]498 unless minutes.nil? || minutes >= 0
[126]499 raise ArgumentError, 'minutes must be above zero'
500 end
501 @minutes = minutes
502 end
503
504 def seconds=(seconds)
[128]505 unless seconds.nil? || seconds >= 0
[126]506 raise ArgumentError, 'seconds must be above zero'
507 end
508 @seconds = seconds
509 end
510
511 def fractional_second=(fractional_second)
[128]512 unless fractional_second.nil? || (fractional_second >= 0 && fractional_second < 1.0)
[126]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
[124]547 end # end of ISO8601_DURATION
[69]548 end # end of Assumed_Types
549end # end of OpenEHR
Note: See TracBrowser for help on using the repository browser.