Ignore:
Timestamp:
Apr 27, 2009, 10:52:33 PM (15 years ago)
Author:
KOBAYASHI, Shinji
Message:

refs #36, #49

File:
1 edited

Legend:

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

    r83 r120  
    22require 'date'
    33require 'time'
     4require 'parsedate'
    45
    56module OpenEHR
     
    109110    end # end of TIME_DEFINITIONS
    110111
    111     class ISO8601_DATE < TIME_DEFINITIONS
     112    module ISO8601_DATE_MODULE
    112113      attr_reader :year, :month, :day
    113       def initialize(year = nil, month = nil, day = nil)
    114         @year = @month = @day = nil
    115         if !year.nil?
    116           self.year = year
    117         end
    118         if !month.nil?
    119           self.month = month
    120         end
    121         if !day.nil?
    122           self.day = day
    123         end
    124       end
    125114      def year=(year)
    126         raise ArgumentError, "Year is not valid" if !ISO8601_DATE.valid_year?(year)
     115        raise ArgumentError, "Year is not valid" unless ISO8601_DATE.valid_year?(year)
    127116        @year = year
    128117      end
    129118      def month=(month)
    130         raise ArgumentError, "Month is not valid" if !ISO8601_DATE.valid_month?(month)
     119        raise ArgumentError, "Month is not valid" unless month.nil? or ISO8601_DATE.valid_month?(month)
    131120        @month = month
    132121      end
    133122      def day=(day)
    134         raise ArgumentError, "Day is not valid" if !ISO8601_DATE.valid_day?(@year, @month, day)
     123        raise ArgumentError, "Day is not valid" unless day.nil? or ISO8601_DATE.valid_day?(@year, @month, day)
    135124        @day = day
    136125      end
     
    156145        month_unknown? or day_unknown?
    157146      end
     147    end
     148
     149    class ISO8601_DATE < TIME_DEFINITIONS
     150      include ISO8601_DATE_MODULE
     151      def initialize(string)
     152        /(\d{4})(?:-(\d{2})(?:-(\d{2})?)?)?/ =~ string
     153        if $1.nil?
     154          raise ArgumentError, 'data invalid'
     155        else
     156          self.year = $1.to_i
     157        end
     158        if $2.nil?
     159          self.month = nil
     160        else
     161          self.month = $2.to_i
     162        end
     163        if $3.nil?
     164          self.day = nil
     165        else
     166          self.day = $3.to_i
     167        end
     168      end
    158169      def self.valid_iso8601_date?(string)
    159170        begin
     
    164175        true
    165176      end
    166 
    167177    end # end of ISO8601_DATE
    168178
    169     class ISO8601_TIME < TIME_DEFINITIONS
     179    module ISO8601_TIME_MODULE
    170180      attr_reader :hour, :minute, :second, :fractional_second, :timezone
    171       def initialize(hh, mm = nil, ss = nil, msec = nil, tz = nil)
    172         raise ArgumentError, "Not valid hour format" if !ISO8601_TIME.valid_hour?(hh,mm,ss)
    173         @hour = hh; @minute = mm; @second = ss
    174         @fractional_second = msec; @timezone = tz
    175       end
     181
    176182      def hour=(hour)
    177183        raise ArgumentError, "hour is not valid" if !ISO8601_TIME.valid_hour?(hour, @minute, @second)
     
    189195      end
    190196      def second=(second)
    191         raise ArgumentError, "minute not defined" if @minute.nil?
     197        raise ArgumentError, "minute not defined" if @minute.nil? and !second.nil?
    192198        raise ArgumentError, "second is not valid" if !second.nil? and !ISO8601_TIME.valid_second?(second)
    193199        @second = second
    194200      end
    195201      def fractional_second=(fractional_second)
    196         raise ArgumentError, "minute not defined" if minute_unknown?
    197         raise ArgumentError, "second not defined" if second_unknown?
    198         raise ArgumentError, "fractional second should be lower than 1.0" if fractional_second >= 1.0
     202        raise ArgumentError, "minute not defined" if minute_unknown? and !fractional_second.nil?
     203        raise ArgumentError, "second not defined" if second_unknown? and !fractional_second.nil?
     204        raise ArgumentError, "fractional second should be lower than 1.0" if !fractional_second.nil? and fractional_second >= 1.0
    199205        @fractional_second = fractional_second
    200206      end
     
    204210        else
    205211          return true
     212        end
     213      end
     214      def timezone=(timezone)
     215        unless timezone.nil? or timezone == 'Z'
     216          if /[+-](\d{2}):?(\d{2})/ =~ timezone
     217            @timezone = timezone
     218          else
     219            raise ArgumentError, "timezone invalid"
     220          end
     221        else
     222          @timezone = nil
    206223        end
    207224      end
     
    231248        s
    232249      end
     250    end
     251
     252    class ISO8601_TIME < TIME_DEFINITIONS
     253      include ISO8601_TIME_MODULE
     254      def initialize(string)
     255        /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ string
     256        if $2.nil?
     257          self.minute = nil
     258        else
     259          self.minute = $2.to_i
     260        end
     261        if $4.nil?
     262          self.second = nil
     263        else
     264          self.second = $4.to_i
     265        end
     266        if $1.nil?
     267          raise ArgumentError, 'data invalid'
     268        else
     269          self.hour = $1.to_i
     270        end
     271        if $7.nil?
     272          self.fractional_second = nil
     273        else
     274          self.fractional_second = ("0." + $7).to_f
     275        end
     276        if $8.nil?
     277          self.timezone = nil
     278        else
     279          self.timezone = $8
     280        end
     281      end
    233282      def self.valid_iso8601_time?(s)
    234283        if /(\d{2}):?(\d{2})?(:?)(\d{2})?((\.|,)(\d+))?(Z|([+-](\d{2}):?(\d{2})))?/ =~ s
     
    273322    end # end of ISO8601_TIME
    274323
     324    class ISO8601_DATE_TIME < ISO8601_DATE
     325      include ISO8601_DATE_MODULE, ISO8601_TIME_MODULE
     326      def initialize(string)
     327        /(\d{4})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d))?)?(Z|([+-]\d{2}):(\d{2}))?)?)?)?/ =~ string
     328        if $1.empty?
     329          raise ArgumentError, 'format invalid'
     330        else
     331          self.year = $1.to_i
     332        end
     333        if $2.nil?
     334          self.month = nil
     335        else
     336          self.month = $2.to_i
     337        end
     338        if $3.nil?
     339          self.day = nil
     340        else
     341          self.day = $3.to_i
     342        end
     343        if $5.nil?
     344          self.minute = nil
     345        else
     346          self.minute = $5.to_i
     347        end
     348        if $6.nil?
     349          self.second = nil
     350        else
     351          self.second = $6.to_i
     352        end
     353        if $4.nil?
     354          self.hour = nil
     355        else
     356          self.hour = $4.to_i
     357        end
     358        if $7.nil?
     359          self.fractional_second = nil
     360        else
     361          self.fractional_second = ("0."+$7).to_f
     362        end
     363        if $8.nil?
     364          self.timezone = nil
     365        else
     366          self.timezone = $8+$9+$10
     367        end
     368      end
     369    end
     370
    275371    class ISO8601_TIMEZONE
    276372      attr_accessor :sign, :hour, :minute
Note: See TracChangeset for help on using the changeset viewer.