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

Returns a new instance of DateFormat.



6
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
# File 'lib/csvlint/csvw/date_format.rb', line 6

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 /[GyYuUrQqMLlwWdDFgEecahHKkjJmsSAzZOvVXx]/.match?(test_pattern)

    @regexp = DATE_PATTERN_REGEXP[@pattern]
    @type = @regexp.nil? ? "http://www.w3.org/2001/XMLSchema#time" : "http://www.w3.org/2001/XMLSchema#date"
    @regexp ||= TIME_PATTERN_REGEXP[@pattern]
    @type = @regexp.nil? ? "http://www.w3.org/2001/XMLSchema#dateTime" : @type
    @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 /ss\.S+/.match?(@pattern)
        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 /yyyy/.match?(regexp)
        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.



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

def pattern
  @pattern
end

Instance Method Details

#match(value) ⇒ Object



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

def match(value)
  value&.match?(@regexp) ? true : false
end

#parse(value) ⇒ Object



66
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/csvlint/csvw/date_format.rb', line 66

def parse(value)
  match = @regexp.match(value)
  return nil if match.nil?
  # STDERR.puts(@regexp)
  # STDERR.puts(value)
  # STDERR.puts(match.inspect)
  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 /:/.match?(tz)
        value[:timezone] = tz
      when "second"
        value[:second] = match["second"].to_f
      else
        value[field.to_sym] = match[field].to_i
      end
    end
  end
  case @type
  when "http://www.w3.org/2001/XMLSchema#date"
    begin
      value[:dateTime] = 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
      value[:dateTime] = 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[:dateTime] = DateTime.new(value[:year] || 0, value[:month] || 1, value[:day] || 1, value[:hour] || 0, value[:minute] || 0, value[:second] || 0, value[:timezone] || "+00:00")
  end
  value[:string] = if value[:year]
    if value[:month]
      if value[:day]
        if value[:hour]
          # dateTime
          "#{format("%04d", value[:year])}-#{format("%02d", value[:month])}-#{format("%02d", value[:day])}T#{format("%02d", value[:hour])}:#{format("%02d", value[:minute] || 0)}:#{format("%02g", value[:second] || 0)}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
        else
          # date
          "#{format("%04d", value[:year])}-#{format("%02d", value[:month])}-#{format("%02d", value[:day])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
        end
      else
        # gYearMonth
        "#{format("%04d", value[:year])}-#{format("%02d", value[:month])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
      end
    else
      # gYear
      "#{format("%04d", value[:year])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
    end
  elsif value[:month]
    if value[:day]
      # gMonthDay
      "--#{format("%02d", value[:month])}-#{format("%02d", value[:day])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
    else
      # gMonth
      "--#{format("%02d", value[:month])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
    end
  elsif value[:day]
    # gDay
    "---#{format("%02d", value[:day])}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
  else
    "#{format("%02d", value[:hour])}:#{format("%02d", value[:minute])}:#{format("%02g", value[:second] || 0)}#{value[:timezone] ? value[:timezone].sub("+00:00", "Z") : ""}"
  end
  value
end