Class: OpenEHR::AssumedLibraryTypes::ISO8601Time

Inherits:
TimeDefinitions show all
Includes:
Comparable, ISO8601TimeModule
Defined in:
lib/open_ehr/assumed_library_types.rb

Constant Summary

Constants inherited from TimeDefinitions

TimeDefinitions::DAYS_IN_LEAP_YEAR, 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, #timezone

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?, #to_second

Methods inherited 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.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/open_ehr/assumed_library_types.rb', line 333

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)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/open_ehr/assumed_library_types.rb', line 366

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 !self.valid_minute?(mm.to_i)
        return false
      end
    end
    if !ss.nil? 
      if !self.valid_second?(ss.to_i)
        return false
      end
    end
    if !tz.nil? and tz != "Z"
      if /[+-](\d{2}):?(\d{2})/ =~ tz
        h = $1; m = $2
        if h.to_i < 0 or h.to_i >= HOURS_IN_DAY
          return false
        end
        if m.to_i < 0 or m.to_i >= MINUTES_IN_HOUR
          return false
        end
      end
    end
    return true
  else
    return false
  end
end

Instance Method Details

#<=>(other) ⇒ Object



362
363
364
# File 'lib/open_ehr/assumed_library_types.rb', line 362

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