Module: CouchRest::Model::CoreExtensions::TimeParsing

Defined in:
lib/couchrest/model/core_extensions/time_parsing.rb

Instance Method Summary collapse

Instance Method Details

#parse_iso8601(string) ⇒ Object

Attemtps to parse a time string in ISO8601 format. If no match is found, the standard time parse will be used.

Times, unless provided with a time zone, are assumed to be in UTC.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/couchrest/model/core_extensions/time_parsing.rb', line 13

def parse_iso8601(string)
  if (string =~ /(\d{4})[\-|\/](\d{2})[\-|\/](\d{2})[T|\s](\d{2}):(\d{2}):(\d{2}(\.\d+)?)(Z| ?([\+|\s|\-])?(\d{2}):?(\d{2}))?/)
    # $1 = year
    # $2 = month
    # $3 = day
    # $4 = hours
    # $5 = minutes
    # $6 = seconds (with $7 for fraction)
    # $8 = UTC or Timezone
    # $9 = time zone direction
    # $10 = tz difference hours
    # $11 = tz difference minutes

    if $8 == 'Z' || $8.to_s.empty?
      utc($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_f)
    else
      new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_f, "#{$9 == '-' ? '-' : '+'}#{$10}:#{$11}")
    end
  else
    parse(string)
  end
end