Skip to content

Commit

Permalink
Create iso8601_converter.cr
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Jun 10, 2023
1 parent 6df98f7 commit 0fa53a9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/schema/ext/time/iso8601_converter.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0fa53a9

Please sign in to comment.