Skip to content

Commit

Permalink
Create local_converter.cr
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Jun 10, 2023
1 parent b527568 commit fc40b3a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/schema/ext/time/local_converter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "../../annotations"

@[Schema::Definition::Scalar]
struct Time
module LocalConverter
FORMAT = "%Y-%m-%dT%H:%M"

# 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_s(format)
end

# Parse `Time` from an HTTP param as time timestamp.
def self.from_http_param(value : String) : Time
Time.parse(time, FORMAT)
rescue ArgumentError
raise TypeCastError.new
end
end
end

0 comments on commit fc40b3a

Please sign in to comment.