Skip to content
This repository has been archived by the owner on May 14, 2018. It is now read-only.

Commit

Permalink
Merge pull request quantum-elixir#81 from kaiatgithub/master
Browse files Browse the repository at this point in the history
modify Quantum.Timer to use correct timezone function. fixes quantum-elixir#73
  • Loading branch information
c-rack committed Feb 29, 2016
2 parents adb0f4b + e59b22b commit f982870
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/quantum/timer.ex
Expand Up @@ -2,17 +2,19 @@ defmodule Quantum.Timer do

@moduledoc false

now = case Application.get_env(:quantum, :timezone, :utc) do
:utc ->
&:calendar.now_to_universal_time/1
:local ->
&:calendar.now_to_local_time/1
timezone ->
raise "Unsupported timezone: #{timezone}"
def timezone_function do
case Application.get_env(:quantum, :timezone, :utc) do
:utc ->
&:calendar.now_to_universal_time/1
:local ->
&:calendar.now_to_local_time/1
timezone ->
raise "Unsupported timezone: #{timezone}"
end
end

def tick do
{d, {h, m, s}} = unquote(now).(:os.timestamp)
{d, {h, m, s}} = timezone_function.(:os.timestamp)
Process.send_after(self, :tick, (60 - s) * 1000)
{d, h, m}
end
Expand Down
31 changes: 31 additions & 0 deletions test/timer_test.exs
@@ -0,0 +1,31 @@
defmodule Quantum.TimerTest do
use ExUnit.Case

test "tick function returns correct time when timezone is set" do
current_time_zone = Application.get_env(:quantum, :timezone, :utc)

Application.put_env(:quantum, :timezone, :utc)
{d_utc, {h_utc, m_utc, _}} = :calendar.now_to_universal_time(:os.timestamp)
assert Quantum.Timer.tick == {d_utc, h_utc, m_utc}

Application.put_env(:quantum, :timezone, :local)
{d_local, {h_local, m_local, _}} = :calendar.now_to_local_time(:os.timestamp)
assert Quantum.Timer.tick == {d_local, h_local, m_local}

Application.put_env(:quantum, :timezone, current_time_zone)
end

test "timezone_function function raises error when timezone is set incorrectly" do
current_time_zone = Application.get_env(:quantum, :timezone, :utc)

try do
Application.put_env(:quantum, :timezone, "test-time-zone")
assert_raise RuntimeError, "Unsupported timezone: test-time-zone", fn ->
Quantum.Timer.timezone_function
end
after
Application.put_env(:quantum, :timezone, current_time_zone)
end
end

end

0 comments on commit f982870

Please sign in to comment.