From 0fa53a9c28c6be6ff54a4276aca1bf0afde2e82f Mon Sep 17 00:00:00 2001 From: Elias Perez Date: Sat, 10 Jun 2023 09:09:47 -0400 Subject: [PATCH] Create iso8601_converter.cr --- src/schema/ext/time/iso8601_converter.cr | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/schema/ext/time/iso8601_converter.cr diff --git a/src/schema/ext/time/iso8601_converter.cr b/src/schema/ext/time/iso8601_converter.cr new file mode 100644 index 0000000..83b3c3c --- /dev/null +++ b/src/schema/ext/time/iso8601_converter.cr @@ -0,0 +1,23 @@ +require "../../annotations" + +@[Schema::Definition::Scalar] +struct Time + module ISO8601Converter + # Put *value* as a time timestamp into the *builder* at *key*. + def self.to_http_param(value : Time, builder : HTTP::Params::Builder, key : String) + builder.add(key, to_http_param(value)) + end + + # Return *value* as a time timestamp string. + def self.to_http_param(value : Time) + value.to_rfc3339 + end + + # Parse `Time` from an HTTP param as time timestamp. + def self.from_http_param(value : String) : Time + Time.parse_iso8601(value) + rescue ArgumentError + raise TypeCastError.new + end + end +end