Skip to content

Commit

Permalink
Fixes #402 (#403)
Browse files Browse the repository at this point in the history
* Fix bug where some offsets were expressed with a dash
  between hours and minutes, and the format/2 function would
  crash while formatting the offset
  • Loading branch information
rozap authored and bitwalker committed Dec 7, 2017
1 parent c86a96a commit 0a736dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/format/datetime/formatter.ex
Expand Up @@ -651,8 +651,12 @@ defmodule Timex.Format.DateTime.Formatter do
{:error, _} = err -> err
"" -> ""
offset ->
[qualifier, <<hour::binary-size(2), min::binary-size(2)>>] = String.split(offset, "", [trim: true, parts: 2])
<<qualifier::binary, hour::binary, ?:, min::binary>>
case String.split(offset, "", [trim: true, parts: 2]) do
[qualifier, <<hour::binary-size(2), min::binary-size(2)>>] ->
<<qualifier::binary, hour::binary, ?:, min::binary>>
[qualifier, <<hour::binary-size(2), "-", min::binary-size(2)>>] ->
<<qualifier::binary, hour::binary, ?:, min::binary>>
end
end
end
def format_token(locale, :zoffs_sec, %{std_offset: std, utc_offset: utc} = date, modifiers, flags, width) do
Expand Down
11 changes: 11 additions & 0 deletions test/format_test.exs
Expand Up @@ -32,4 +32,15 @@ defmodule DateFormatTest.GeneralFormatting do
assert "" = Timex.format!(t, "%f", :strftime)
assert "000" = Timex.format!(t, "%03f", :strftime)
end

test "issue #402 - formatting a Time sometimes crashes for timezones" do
assert Timex.to_datetime({{2016,2,8}, {12,0,0}})
|> Timex.Timezone.convert("Canada/Newfoundland")
|> Timex.format!("{ISO:Extended}") == "2016-02-08T08:30:00-03:30"

assert Timex.to_datetime({{2016,2,8}, {12,0,0}})
|> Timex.Timezone.convert("Pacific/Marquesas")
|> Timex.format!("{ISO:Extended}") == "2016-02-08T02:30:00-09:30"

end
end

0 comments on commit 0a736dc

Please sign in to comment.