Class: Csvlint::Csvw::DateFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/csvlint/csvw/date_format.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, datatype = nil) ⇒ DateFormat



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/csvlint/csvw/date_format.rb', line 7

def initialize(pattern, datatype=nil)
  @pattern = pattern

  if @pattern.nil?
    @regexp = DEFAULT_REGEXP[datatype]
    @type = datatype
  else
    test_pattern = pattern.clone
    test_pattern.gsub!(/S+/, "")
    FIELDS.keys.sort_by{|f| -f.length}.each do |field|
      test_pattern.gsub!(field, "")
    end
    raise Csvw::DateFormatError, "unrecognised date field symbols in date format" if test_pattern =~ /[GyYuUrQqMLlwWdDFgEecahHKkjJmsSAzZOvVXx]/

    @regexp = DATE_PATTERN_REGEXP[@pattern]
    @type = @regexp.nil? ? "http://www.w3.org/2001/XMLSchema#time" : "http://www.w3.org/2001/XMLSchema#date"
    @regexp = @regexp || TIME_PATTERN_REGEXP[@pattern]
    @type = @regexp.nil? ? "http://www.w3.org/2001/XMLSchema#dateTime" : @type
    @regexp = @regexp || DATE_TIME_PATTERN_REGEXP[@pattern]

    if @regexp.nil?
      regexp = @pattern

      @type = "http://www.w3.org/2001/XMLSchema#date" if !(regexp =~ /HH/) && regexp =~ /yyyy/
      @type = "http://www.w3.org/2001/XMLSchema#time" if regexp =~ /HH/ && !(regexp =~ /yyyy/)
      @type = "http://www.w3.org/2001/XMLSchema#dateTime" if regexp =~ /HH/ && regexp =~ /yyyy/

      regexp = regexp.sub("HH", FIELDS["HH"].to_s)
      regexp = regexp.sub("mm", FIELDS["mm"].to_s)
      if @pattern =~ /ss\.S+/
        max_fractional_seconds = @pattern.split(".")[-1].length
        regexp = regexp.sub(/ss\.S+$/, "(?<second>#{FIELDS["ss"]}(\.[0-9]{1,#{max_fractional_seconds}})?)")
      else
        regexp = regexp.sub("ss", "(?<second>#{FIELDS["ss"]})")
      end

      if regexp =~ /yyyy/
        regexp = regexp.sub("yyyy", FIELDS["yyyy"].to_s)
        regexp = regexp.sub("MM", FIELDS["MM"].to_s)
        regexp = regexp.sub("M", FIELDS["M"].to_s)
        regexp = regexp.sub("dd", FIELDS["dd"].to_s)
        regexp = regexp.sub(/d(?=[-T \/\.])/, FIELDS["d"].to_s)
      end

      regexp = regexp.sub("XXX", FIELDS["XXX"].to_s)
      regexp = regexp.sub("XX", FIELDS["XX"].to_s)
      regexp = regexp.sub("X", FIELDS["X"].to_s)
      regexp = regexp.sub("xxx", FIELDS["xxx"].to_s)
      regexp = regexp.sub("xx", FIELDS["xx"].to_s)
      regexp = regexp.sub(/x(?!:)/, FIELDS["x"].to_s)

      @regexp = Regexp.new("^#{regexp}$")
    end
  end
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/csvlint/csvw/date_format.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#match(value) ⇒ Object



63
64
65
# File 'lib/csvlint/csvw/date_format.rb', line 63

def match(value)
  value =~ @regexp ? true : false
end

#parse(value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/csvlint/csvw/date_format.rb', line 67

def parse(value)
  match = @regexp.match(value)
  return nil if match.nil?
  # STDERR.puts(@regexp)
  # STDERR.puts(value)
  # STDERR.puts(match.inspect)
  case @type
  when "http://www.w3.org/2001/XMLSchema#date"
    begin
      return Date.new(match["year"].to_i, match["month"].to_i, match["day"].to_i)
    rescue ArgumentError
      return nil
    end
  when "http://www.w3.org/2001/XMLSchema#dateTime"
    begin
      return DateTime.new(match["year"].to_i, match["month"].to_i, match["day"].to_i, match["hour"].to_i, match["minute"].to_i, (match.names.include?("second") ? match["second"].to_f : 0), match.names.include?("timezone") && match["timezone"] ? match["timezone"] : '')
    rescue ArgumentError
      return nil
    end
  else
    value = {}
    match.names.each do |field|
      unless match[field].nil?
        case field
        when "timezone"
          tz = match["timezone"]
          tz = "+00:00" if tz == 'Z'
          tz += ':00' if tz.length == 3
          tz = "#{tz[0..2]}:#{tz[3..4]}" unless tz =~ /:/
          value["timezone"] = tz
        when "second"
          value["second"] = match["second"].to_f
        else
          value[field] = match[field].to_i
        end
      end
    end
    return value
  end
end