Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support datetime-local time parsing #603

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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