Changeset 81


Ignore:
Timestamp:
Jul 17, 2008, 11:02:57 PM (16 years ago)
Author:
KOBAYASHI, Shinji
Message:

refs #38
modifed valid_iso_8601_time in ISO_8601_TIME
change instance method to class method valid_iso8601_time in ISO_8601_TIME class
change instance method to class method valid_iso8601_date in ISO_8601_date class

Location:
ruby/trunk/lib/models
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk/lib/models/assumed_library_types.rb

    r80 r81  
    11require 'date'
     2require 'time'
    23
    34module OpenEHR
     
    9091        valid_minute?(m) and valid_second?(s) and ((h >= 0 and h < HOURS_IN_DAY) or (h == HOURS_IN_DAY and m == 0 and s == 0))
    9192      end
    92       def self.valid_minute?(m)
    93         m >= 0 and m < MINUTES_IN_HOUR
     93      def self.valid_minute?(mi)
     94        mi >= 0 and mi < MINUTES_IN_HOUR
    9495      end
    9596      def self.valid_second?(s)
    9697        s >= 0 and s < SECONDS_IN_MINUTE
    9798      end
    98       def self.valid_month?(m)
    99         m >= 1 and m <= MONTH_IN_YEAR
     99      def self.valid_month?(mo)
     100        mo >= 1 and mo <= MONTH_IN_YEAR
    100101      end
    101102    end # end of TIME_DEFINITIONS
     
    148149        month_unknown? or day_unknown?
    149150      end
    150       def valid_iso8601_date(string)
     151      def self.valid_iso8601_date?(string)
    151152        begin
    152           date = Date.parse(string)
     153          Date.parse(string)
    153154        rescue
    154155          return false
     
    160161
    161162    class ISO8601_TIME < TIME_DEFINITIONS
    162 
     163      attr_reader :hh, :mm, :ss, :msec, :tz
     164      def initialize(hh = nil, mm = nil, ss = nil, msec = nil, tz = nil)
     165       
     166      end
     167      def as_string
     168        s = @hh
     169        if !@mm.nil?
     170          s += ":" + @mm
     171          if !@ss.nil?
     172            s += ":" + @ss
     173            if !@msec.nil?
     174              s += "," + @msec
     175              if !@tz.nil?
     176                s += @tz
     177              end
     178            end
     179          end
     180        end
     181        s
     182      end
     183      def self.valid_iso8601_time?(s)
     184        if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
     185# ISO 8601 regular expression by H. Yuki
     186#  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
     187# (\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?
     188          hh = $1; mm = $2; ss = $4; msec = $7; tz = $8
     189          if hh.to_i == HOURS_IN_DAY and (mm.nil? or mm.to_i == "00") and (ss.nil? or ss.to_i == "00")
     190            return true
     191          end
     192          if hh.nil? or (hh.to_i < 0 or hh.to_i >= HOURS_IN_DAY)
     193            return false
     194          end
     195          if !mm.nil?
     196            if !self.valid_minute?(mm.to_i)
     197              return false
     198            end
     199          end
     200          if !ss.nil?
     201            if !self.valid_second?(ss.to_i)
     202              return false
     203            end
     204          end
     205          if !tz.nil? and tz != "Z"
     206            if /[+-](\d{2}):?(\d{2})/ =~ tz
     207              h = $1; m = $2
     208              if h.to_i < 0 or h.to_i >= HOURS_IN_DAY
     209                return false
     210              end
     211              if m.to_i < 0 or m.to_i >= MINUTES_IN_HOUR
     212                return false
     213              end
     214            else
     215              return false
     216            end
     217          end
     218          return true
     219        else
     220          return false
     221        end
     222      end
    163223    end # end of ISO8601_TIME
    164224
  • ruby/trunk/lib/models/tests/test_assumed_library_types.rb

    r80 r81  
    182182    assert !@iso8601_date.is_partial?
    183183    assert_equal "2008-06-14", @iso8601_date.as_string
    184     assert @iso8601_date.valid_iso8601_date("2006-06-14")
    185     assert !@iso8601_date.valid_iso8601_date("ABCDEFG")
     184    assert OpenEHR::Assumed_Library_Types::ISO8601_DATE.valid_iso8601_date?("2006-06-14")
     185    assert !OpenEHR::Assumed_Library_Types::ISO8601_DATE.valid_iso8601_date?("ABCDEFG")
    186186  end
    187187
    188188  def test_iso8601_time
    189 
     189    assert OpenEHR::Assumed_Library_Types::ISO8601_TIME.valid_iso8601_time?("01:01:01")
     190    assert !OpenEHR::Assumed_Library_Types::ISO8601_TIME.valid_iso8601_time?("ABCDEFG")
    190191  end
    191192  def test_iso8601_timezone
     
    197198    @iso8601_timezone.hour = 9
    198199    assert_equal "Z+0900", @iso8601_timezone.as_string
     200    assert !@iso8601_timezone.is_gmt?
    199201    @iso8601_timezone.sign = "-1"
    200202    @iso8601_timezone.hour = 4
Note: See TracChangeset for help on using the changeset viewer.