Sha256: cf658c45cdae622979119fb5f1d0b1a402b79a4b7acc44f18196ca12cd14301a

Contents?: true

Size: 654 Bytes

Versions: 1

Compression:

Stored size: 654 Bytes

Contents

# frozen_string_literal: true

module TJSON
  class DataType
    # RFC3339 timestamp (Z-normalized)
    class Timestamp < Scalar
      # Regular expression for matching timestamps
      TIMESTAMP_REGEX = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\z/

      def tag
        "t"
      end

      def convert(str)
        raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
        raise TJSON::ParseError, "invalid timestamp: #{str.inspect}" unless str =~ TIMESTAMP_REGEX

        ::Time.iso8601(str)
      end

      def generate(timestamp)
        timestamp.to_time.utc.iso8601
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tjson-0.4.0 lib/tjson/datatype/timestamp.rb