/
date_time.cr
66 lines (55 loc) · 1.8 KB
/
date_time.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Marten
abstract class Schema
module Field
# Represents a date time schema field.
class DateTime < Base
def deserialize(value) : Time?
return if empty_value?(value)
date_time = case value
when Nil
value
when ::Time
value
when ::String
parse_date_time(value) || raise_unexpected_field_value(value)
when ::JSON::Any
deserialize(value.raw)
else
raise_unexpected_field_value(value)
end
date_time.in(Marten.settings.time_zone) unless date_time.nil?
end
def serialize(value) : ::String?
value.try(&.to_s)
end
private def fetch_date_time_format(index)
I18n.t!("marten.schema.field.date_time.input_formats.#{index}")
rescue I18n::Errors::MissingTranslation
nil
end
private def invalid_error_message(_schema)
I18n.t("marten.schema.field.date_time.errors.invalid")
end
private def parse_date_time(value)
result = begin
Time.parse_iso8601(value)
rescue Time::Format::Error
nil
end
return result unless result.nil?
attempt = 0
format = I18n.t("marten.schema.field.date_time.input_formats.#{attempt}")
while result.nil? && (format = fetch_date_time_format(attempt))
result = begin
Time.parse(value, format, Marten.settings.time_zone)
rescue Time::Format::Error
nil
end
attempt += 1
end
result
end
end
end
end
end