Changeset 126
- Timestamp:
- May 25, 2009, 8:56:49 PM (15 years ago)
- Location:
- ruby/trunk/lib/models
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/models/assumed_library_types.rb
r125 r126 237 237 238 238 def is_decimal_sign_comma? 239 true239 false 240 240 end 241 241 … … 391 391 elsif (!@year.nil? and @month.nil? and @day.nil?) 392 392 return Date.new(@year).to_s[0,4] 393 end 394 s += sprintf("T%02d", @hour) 395 if !@minute.nil? 396 s += ":" + sprintf("%02d",@minute) 397 if !@second.nil? 398 s += ":" + sprintf("%02d", @second) 399 if !@fractional_second.nil? 400 s += "." + @fractional_second.to_s[2..-1] 401 if !@timezone.nil? 402 s += @timezone 393 end 394 unless hour.nil? 395 s += sprintf("T%02d", @hour) 396 unless @minute.nil? 397 s += ":" + sprintf("%02d",@minute) 398 unless @second.nil? 399 s += ":" + sprintf("%02d", @second) 400 unless @fractional_second.nil? 401 s += "." + @fractional_second.to_s[2..-1] 402 unless @timezone.nil? 403 s += @timezone 404 end 403 405 end 404 406 end … … 427 429 end # end of ISO8601_TIMEZONE 428 430 429 class ISO8601_DURATION 430 431 def initialize 432 431 class ISO8601_DURATION < TIME_DEFINITIONS 432 attr_reader :years, :months, :weeks, :days 433 attr_reader :hours, :minutes, :seconds, :fractional_second 434 435 def initialize(str) 436 /^P((\d+)Y)?((\d+)M)?((\d+)W)?((\d)D)?(T((\d+)H)?((\d+)M)?((\d+)(\.\d+)?S)?)?$/ =~ str 437 self.years = $2.to_i 438 self.months = $4.to_i 439 self.weeks = $6.to_i 440 self.days = $8.to_i 441 self.hours = $11.to_i 442 self.minutes = $13.to_i 443 self.seconds = $15.to_i 444 self.fractional_second = $16.to_f 445 end 446 447 def years=(years) 448 unless years.nil? || years > 0 449 raise ArgumentError, 'years must be above zero' 450 end 451 @years = years 452 end 453 454 def months=(months) 455 unless months.nil? || months > 0 456 raise ArgumentError, 'months must be above zero' 457 end 458 @months = months 459 end 460 461 def weeks=(weeks) 462 unless weeks.nil? || weeks > 0 463 raise ArgumentError, 'weeks must be above zero' 464 end 465 @weeks = weeks 466 end 467 468 def days=(days) 469 unless days.nil? || days > 0 470 raise ArgumentError, 'days must be above zero' 471 end 472 @days = days 473 end 474 475 def hours=(hours) 476 unless hours.nil? || hours > 0 477 raise ArgumentError, 'hours must be above zero' 478 end 479 @hours = hours 480 end 481 482 def minutes=(minutes) 483 unless minutes.nil? || minutes > 0 484 raise ArgumentError, 'minutes must be above zero' 485 end 486 @minutes = minutes 487 end 488 489 def seconds=(seconds) 490 unless seconds.nil? || seconds > 0 491 raise ArgumentError, 'seconds must be above zero' 492 end 493 @seconds = seconds 494 end 495 496 def fractional_second=(fractional_second) 497 unless fractional_second.nil? || (fractional_second > 0 && fractional_second < 1.0) 498 raise ArgumentError, 'fractional_second must be between 0.0 and 1.0' 499 end 500 @fractional_second = fractional_second 433 501 end 434 502 435 503 def as_string 436 504 str = 'P' 505 unless @years.nil? 506 str += @years.to_s + 'Y' 507 end 508 unless @months.nil? 509 str += @months.to_s + 'M' 510 end 511 unless @weeks.nil? 512 str += @weeks.to_s + 'W' 513 end 514 unless @days.nil? 515 str += @days.to_s + 'D' 516 end 517 unless @hours.nil? 518 str += 'T' + @hours.to_s + 'H' 519 unless @minutes.nil? 520 str += @minutes.to_s + 'M' 521 unless @seconds.nil? 522 str += @seconds.to_s 523 unless @fractional_second.nil? 524 str += @fractional_second.to_s[1 .. -1] 525 end 526 str += 'S' 527 end 528 end 529 end 530 return str 437 531 end 438 532 end # end of ISO8601_DURATION -
ruby/trunk/lib/models/tests/test_assumed_library_types.rb
r125 r126 9 9 assert_nothing_raised(Exception){@iso8601_time = OpenEHR::Assumed_Library_Types::ISO8601_TIME.new('15:55:37.32+0900')} 10 10 assert_nothing_raised(Exception){@iso8601_date_time = OpenEHR::Assumed_Library_Types::ISO8601_DATE_TIME.new('2009-04-27T15:55:37.32+0900')} 11 assert_nothing_raised(Exception){@iso8601_duration = OpenEHR::Assumed_Library_Types::ISO8601_DURATION.new( )}11 assert_nothing_raised(Exception){@iso8601_duration = OpenEHR::Assumed_Library_Types::ISO8601_DURATION.new('P1Y2M3W4DT5H6M7.8S')} 12 12 assert_nothing_raised(Exception){@iso8601_timezone = OpenEHR::Assumed_Library_Types::ISO8601_TIMEZONE.new} 13 13 end … … 250 250 assert_equal "23:59:59.23", @iso8601_time.as_string 251 251 assert @iso8601_time.is_extended? 252 assert @iso8601_time.is_decimal_sign_comma?252 assert !@iso8601_time.is_decimal_sign_comma? 253 253 assert OpenEHR::Assumed_Library_Types::ISO8601_TIME.valid_iso8601_time?("012345Z") 254 254 assert OpenEHR::Assumed_Library_Types::ISO8601_TIME.valid_iso8601_time?("012345.67+0900") … … 291 291 292 292 def test_iso8601_duration 293 293 assert_equal 1, @iso8601_duration.years 294 assert_equal 2, @iso8601_duration.months 295 assert_equal 3, @iso8601_duration.weeks 296 assert_equal 4, @iso8601_duration.days 297 assert_equal 5, @iso8601_duration.hours 298 assert_equal 6, @iso8601_duration.minutes 299 assert_equal 7, @iso8601_duration.seconds 300 assert_equal 0.8, @iso8601_duration.fractional_second 301 assert_equal 'P1Y2M3W4DT5H6M7.8S', @iso8601_duration.as_string 302 assert_raise(ArgumentError){@iso8601_duration.years = -1} 303 assert_nothing_raised(Exception){@iso8601_duration.years = nil} 304 assert_equal 'P2M3W4DT5H6M7.8S', @iso8601_duration.as_string 294 305 end 295 306 end
Note:
See TracChangeset
for help on using the changeset viewer.