Skip to content

Commit

Permalink
Add RDF.DateTime.tz/1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotto committed Aug 10, 2018
1 parent 2332387 commit ec55b37
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rdf/datatypes/date_time.ex
Expand Up @@ -54,4 +54,20 @@ defmodule RDF.DateTime do
NaiveDateTime.to_iso8601(value)
end

def tz(datetime_literal) do
if valid?(datetime_literal) do
lexical = lexical(datetime_literal)
case Regex.run(~r/([+-])(\d\d:\d\d)/, lexical) do
[_, sign, zone] ->
sign <> zone
_ ->
if String.ends_with?(lexical, "Z") do
"Z"
else
""
end
end
end
end

end
45 changes: 45 additions & 0 deletions test/unit/datatypes/date_time_test.exs
Expand Up @@ -61,4 +61,49 @@ defmodule RDF.DateTimeTest do
end
end

describe "tz/1" do
test "with timezone" do
[
{"2010-01-01T00:00:00-23:00", "-23:00"},
{"2010-01-01T00:00:00+23:00", "+23:00"},
{"2010-01-01T00:00:00+00:00", "+00:00"},
]
|> Enum.each(fn {dt, tz} ->
assert dt |> DateTime.new() |> DateTime.tz() == tz
end)

end

test "without any specific timezone" do
[
"2010-01-01T00:00:00Z",
"2010-01-01T00:00:00.0000Z",
]
|> Enum.each(fn dt ->
assert dt |> DateTime.new() |> DateTime.tz() == "Z"
end)
end

test "without any timezone" do
[
"2010-01-01T00:00:00",
"2010-01-01T00:00:00.0000",
]
|> Enum.each(fn dt ->
assert dt |> DateTime.new() |> DateTime.tz() == ""
end)
end

test "with invalid timezone literals" do
[
DateTime.new("2010-01-01T00:0"),
"2010-01-01T00:00:00.0000",
]
|> Enum.each(fn dt ->
assert DateTime.tz(dt) == nil
end)

end
end

end

0 comments on commit ec55b37

Please sign in to comment.