Skip to content

Instantly share code, notes, and snippets.

@jstr
Created October 19, 2017 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstr/0c2fd9cf686469843e0e11fc7c70c1ba to your computer and use it in GitHub Desktop.
Save jstr/0c2fd9cf686469843e0e11fc7c70c1ba to your computer and use it in GitHub Desktop.
Fix for Calendar package -- unhandled exception when RFC3339 string is missing time zone offset
diff --git a/lib/calendar/date_time/parse.ex b/lib/calendar/date_time/parse.ex
index 914247f..5d22596 100644
--- a/lib/calendar/date_time/parse.ex
+++ b/lib/calendar/date_time/parse.ex
@@ -233,6 +233,10 @@ defmodule Calendar.DateTime.Parse do
iex> rfc3339_utc("fooo")
{:bad_format, nil}
+ # Missing time zone offset
+ iex> rfc3339_utc("1996-12-19T16:39:57")
+ {:bad_format, nil}
+
iex> rfc3339_utc("1996-12-19T16:39:57Z")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}
@@ -308,7 +312,7 @@ defmodule Calendar.DateTime.Parse do
defp parse_rfc3339_as_utc_parsed_string(mapped, _z, offset_hours, offset_mins) when offset_hours == "00" and offset_mins == "00" do
Calendar.DateTime.from_erl(erl_date_time_from_regex_map(mapped), "Etc/UTC", parse_fraction(mapped["fraction"]))
end
- defp parse_rfc3339_as_utc_parsed_string(mapped, _z, offset_hours, offset_mins) do
+ defp parse_rfc3339_as_utc_parsed_string(mapped, _z, offset_hours, offset_mins) when offset_hours != "" and offset_mins != "" do
offset_in_secs = hours_mins_to_secs!(offset_hours, offset_mins)
offset_in_secs = case mapped["offset_sign"] do
"-" -> offset_in_secs*-1
@@ -317,6 +321,9 @@ defmodule Calendar.DateTime.Parse do
erl_date_time = erl_date_time_from_regex_map(mapped)
parse_rfc3339_as_utc_with_offset(offset_in_secs, erl_date_time, parse_fraction(mapped["fraction"]))
end
+ defp parse_rfc3339_as_utc_parsed_string(_mapped, _z, _offset_hours, _offset_mins) do
+ {:bad_format, nil}
+ end
defp parse_fraction("." <> frac), do: parse_fraction(frac)
defp parse_fraction("," <> frac), do: parse_fraction(frac)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment