Skip to content

Commit fc40b3a

Browse files
authored
Create local_converter.cr
1 parent b527568 commit fc40b3a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "../../annotations"
2+
3+
@[Schema::Definition::Scalar]
4+
struct Time
5+
module LocalConverter
6+
FORMAT = "%Y-%m-%dT%H:%M"
7+
8+
# Put *value* as a time timestamp into the *builder* at *key*.
9+
def self.to_http_param(value : Time, builder : HTTP::Params::Builder, key : String)
10+
builder.add(key, to_http_param(value))
11+
end
12+
13+
# Return *value* as a time timestamp string.
14+
def self.to_http_param(value : Time)
15+
value.to_s(format)
16+
end
17+
18+
# Parse `Time` from an HTTP param as time timestamp.
19+
def self.from_http_param(value : String) : Time
20+
Time.parse(time, FORMAT)
21+
rescue ArgumentError
22+
raise TypeCastError.new
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)