Skip to content

Commit

Permalink
Parse dates in brazilian format
Browse files Browse the repository at this point in the history
  • Loading branch information
thiamsantos committed Aug 21, 2023
1 parent e4c574c commit 0442ec9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
File renamed without changes.
28 changes: 19 additions & 9 deletions lib/xler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,25 @@ defmodule Xler do

@day_in_seconds 86400
defp format_cell(cell, :datetime) do
case Float.parse(cell) do
{time, _rest} ->
NaiveDateTime.add(
~N[1900-01-01 00:00:00],
round(time * @day_in_seconds) - 2 * @day_in_seconds
)

:error ->
cell
if Regex.match?(~r/^\d+\.\d+$/, cell) or Regex.match?(~r/^\d+$/, cell) do
case Float.parse(cell) do
{time, _rest} ->
NaiveDateTime.add(
~N[1900-01-01 00:00:00],
round(time * @day_in_seconds) - 2 * @day_in_seconds
)

:error ->
cell
end
else
case Datix.Date.parse(cell, "%d/%m/%Y") do
{:ok, date} ->
NaiveDateTime.new!(date, ~T[00:00:00])

_ ->
cell
end
end
end

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Xler.MixProject do
[
{:rustler, "~> 0.27.0"},
{:rustler_precompiled, "~> 0.6.0"},
{:datix, "~> 0.3.1"},
{:ex_doc, "~> 0.29", only: :dev, runtime: false}
]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"castore": {:hex, :castore, "1.0.0", "c25cd0794c054ebe6908a86820c8b92b5695814479ec95eeff35192720b71eec", [:mix], [], "hexpm", "577d0e855983a97ca1dfa33cbb8a3b6ece6767397ffb4861514343b078fc284b"},
"datix": {:hex, :datix, "0.3.1", "c3f89aca53d09953db8b2f97ae2239a6e57096bf6d9b43ba0379f1a26b35ea9e", [:mix], [], "hexpm", "93fb61b9065ef9d053278571d184b9c43dd563b43cd5a6d8e4743fc6ac1687f2"},
"earmark_parser": {:hex, :earmark_parser, "1.4.30", "0b938aa5b9bafd455056440cdaa2a79197ca5e693830b4a982beada840513c5f", [:mix], [], "hexpm", "3b5385c2d36b0473d0b206927b841343d25adb14f95f0110062506b300cd5a1b"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
Expand Down
Binary file added test/data/date-in-text.xlsx
Binary file not shown.
32 changes: 32 additions & 0 deletions test/xler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ defmodule XlerTest do
]
end

test "date in text" do
fixture = Path.join(__DIR__, "/data/date-in-text.xlsx")

assert {:ok, data} =
Xler.parse(fixture, "Página1",
format: %{
skip_rows: [],
columns: [
%{column: 0, data_type: :date}
]
}
)

assert data == [["Data"], [~D[2023-08-21]]]
end

test "datetime in text" do
fixture = Path.join(__DIR__, "/data/date-in-text.xlsx")

assert {:ok, data} =
Xler.parse(fixture, "Página1",
format: %{
skip_rows: [],
columns: [
%{column: 0, data_type: :datetime}
]
}
)

assert data == [["Data"], [~N[2023-08-21 00:00:00]]]
end

def custom_formatter(value) do
"#{value}customformatter"
end
Expand Down

0 comments on commit 0442ec9

Please sign in to comment.