Skip to content

Commit

Permalink
Support datetime-local time parsing (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendolan authored Jan 12, 2021
1 parent b447938 commit 84e431c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 16 additions & 5 deletions spec/type_extensions/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ describe "Time column type" do
it "casts various formats successfully" do
time = Time.local
times = {
iso8601: time.to_s("%FT%X%z"),
rfc2822: time.to_rfc2822,
rfc3339: time.to_rfc3339,
space_separated_yaml: "2001-12-14 21:59:43.10 -5",
http_date: "Sun, 14 Feb 2016 21:00:00 GMT",
iso8601: time.to_s("%FT%X%z"),
rfc2822: time.to_rfc2822,
rfc3339: time.to_rfc3339,
datetime_html_input: time.to_s("%Y-%m-%dT%H:%M:%S"),
http_date: time.to_s("%a, %d %b %Y %H:%M:%S GMT"),
}
times.each do |_format, item|
result = Time.adapter.parse(item)
result.should be_a(Avram::Type::SuccessfulCast(Time))

unless result.is_a? Avram::Type::SuccessfulCast(Time)
next
end

result.value.year.should eq(time.year)
result.value.month.should eq(time.month)
result.value.day.should eq(time.day)
result.value.hour.should eq(time.hour)
result.value.minute.should eq(time.minute)
result.value.second.should eq(time.second)
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct Time
Time::Format::ISO_8601_DATE_TIME,
Time::Format::RFC_2822,
Time::Format::RFC_3339,
# HTML datetime-local inputs are basically RFC 3339 without the timezone:
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
Time::Format.new("%Y-%m-%dT%H:%M:%S", Time::Location.local),
# Dates and times go last, otherwise it will parse strings with both
# dates *and* times incorrectly.
Time::Format::HTTP_DATE,
Expand Down

0 comments on commit 84e431c

Please sign in to comment.