bitwalker / timex

A complete date/time library for Elixir projects.
https://hexdocs.pm/timex
MIT License
1.75k stars 375 forks source link

Argument error on binary_part #272

Closed alex88 closed 7 years ago

alex88 commented 7 years ago

I'm using this function in my code

Timex.format!(Timex.now, "{YYYY}-{0M}-{0D}-{h24}-{m}-{s}{ss}")

however sometimes I get this exception

** (ArgumentError) argument error
    :erlang.binary_part("95416", {0, 6})
    lib/format/datetime/formatter.ex:523: Timex.Format.DateTime.Formatter.format_token/6
    lib/format/datetime/formatters/default.ex:182: Timex.Format.DateTime.Formatters.Default.do_format/4
    lib/format/datetime/formatter.ex:54: Timex.Format.DateTime.Formatter.lformat!/4

I've tried to manually run the function may times on iex console and after 10-15 tries I got the same exception.

What could be the issues here?

alex88 commented 7 years ago

The issue seems with the microseconds, you just need to run Timex.format!(Timex.now, "{ss}") that it generates that exception

bitwalker commented 7 years ago

The issue is that binary_part takes 3 arguments, the 2nd is the start index, the 3rd is the number of bytes to take. It looks like something is passing a tuple to it instead of the correct arguments. That tuple is a microsecond tuple I'm almost positive, which means there is a bug in the {ss} formatter. I'll take a look!

alex88 commented 7 years ago

What I've noticed is that every time it happens, the microseconds has 5 digits instead of 6

bitwalker commented 7 years ago

Pushed in 3.1.9