Class: OpenEHR::AssumedLibraryTypes::ISO8601Time

Inherits:
Object
  • Object
show all
Includes:
Comparable, ISO8601TimeModule
Defined in:
lib/openehr/assumed_library_types.rb

Constant Summary

Constants included from TimeDefinitions

TimeDefinitions::DAYS_IN_LEAP_YEAR, TimeDefinitions::DAYS_IN_MONTH, TimeDefinitions::DAYS_IN_WEEK, TimeDefinitions::DAYS_IN_YEAR, TimeDefinitions::HOURS_IN_DAY, TimeDefinitions::MAX_DAYS_IN_MONTH, TimeDefinitions::MAX_DAYS_IN_YEAR, TimeDefinitions::MINUTES_IN_HOUR, TimeDefinitions::MONTH_IN_YEAR, TimeDefinitions::NOMINAL_DAYS_IN_MONTH, TimeDefinitions::NOMINAL_DAYS_IN_YEAR, TimeDefinitions::SECONDS_IN_MINUTE

Instance Attribute Summary

Attributes included from ISO8601TimeModule

#fractional_second, #hour, #minute, #second

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ISO8601TimeModule

#as_string, #has_fractional_second?, #is_decimal_sign_comma?, #is_extended?, #is_partial?, #minute_unknown?, #second_unknown?, #timezone, #timezone=, #to_second

Methods included from TimeDefinitions

valid_day?, valid_hour?, valid_minute?, valid_month?, valid_second?, valid_year?

Constructor Details

#initialize(string) ⇒ ISO8601Time

Returns a new instance of ISO8601Time.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/openehr/assumed_library_types.rb', line 418

def initialize(string)
  /^(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?$/ =~ string
  if $2.nil?
    self.minute = nil
  else
    self.minute = $2.to_i
  end
  if $4.nil?
    self.second = nil
  else
    self.second = $4.to_i
  end
  if $1.nil?
    raise ArgumentError, 'data invalid'
  else
    self.hour = $1.to_i
  end
  if $7.nil?
    self.fractional_second = nil
  else
    self.fractional_second = ("0." + $7).to_f
  end
  if $8.nil?
    self.timezone = nil
  else
    self.timezone = $8
  end
end

Class Method Details

.valid_iso8601_time?(s) ⇒ Boolean

Returns:

  • (Boolean)


451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/openehr/assumed_library_types.rb', line 451

def self.valid_iso8601_time?(s)
  if /^(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?$/ =~ s
# ISO 8601 regular expression by H. Yuki
#  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
# (\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?
    hh = $1; mm = $2; ss = $4; msec = $7; tz = $8
    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)
      return true
    end
    if hh.nil? or (hh.to_i < 0 or hh.to_i >= HOURS_IN_DAY)
      return false
    end
    if !mm.nil? 
      if !TimeDefinitions.valid_minute?(mm.to_i)
        return false
      end
    end
    if !ss.nil? 
      if !TimeDefinitions.valid_second?(ss.to_i)
        return false
      end
    end
    # A timezone is optional in ISO 8601; its absence does not make an
    # otherwise-valid time invalid.
    unless tz.nil?
      timezone = Timezone.new(tz)
      if timezone.hour < 0 or timezone.hour >= HOURS_IN_DAY
        return false
      end
      if timezone.minute < 0 or timezone.minute >= MINUTES_IN_HOUR
        return false
      end
    end
    return true
  end
end

Instance Method Details

#<=>(other) ⇒ Object



447
448
449
# File 'lib/openehr/assumed_library_types.rb', line 447

def <=>(other)
  self.to_second <=> other.to_second
end