File: date_time.rb

package info (click to toggle)
ruby-kdl 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 6,667; yacc: 72; sh: 5; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
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
require 'time'

module KDL
  module Types
    class DateTime < Value
      def self.call(value, type = 'date-time')
        return nil unless value.is_a? ::KDL::Value::String

        time = ::Time.iso8601(value.value)
        new(time, type: type)
      end
    end
    MAPPING['date-time'] = DateTime

    class Time < Value
      # TODO: this is not a perfect ISO8601 time string
      REGEX = /^T?((?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?(?:Z|[+-]\d\d:\d\d)?)$/

      def self.call(value, type = 'time')
        return nil unless value.is_a? ::KDL::Value::String

        match = REGEX.match(value.value)
        raise ArgumentError, 'invalid time' if match.nil?

        time = ::Time.iso8601("#{::Date.today.iso8601}T#{match[1]}")
        new(time, type: type)
      end
    end
    MAPPING['time'] = Time

    class Date < Value
      def self.call(value, type = 'date')
        return nil unless value.is_a? ::KDL::Value::String

        date = ::Date.iso8601(value.value)
        new(date, type: type)
      end
    end
    MAPPING['date'] = Date
  end
end