SNTP (SNTP v0.2.2) View Source

SNTP v4 client RFC4330 for Elixir

Link to this section Summary

Functions

Returns the system time in milliseconds. If the SNTP.Retriever is running then it will return the adjusted system time.

Returns the latest retrieved offset from the SNTP.Retriever

Starts the SNTP.Retriever

Sends a new NTP request on an SNTP.Socket and gracefully closes the socket. Returns {:ok, %SNTP.Timestamp{}} or {:error, reason}

Link to this section Functions

Specs

now() :: pos_integer()

Returns the system time in milliseconds. If the SNTP.Retriever is running then it will return the adjusted system time.

Examples

iex> SNTP.now()
System.system_time(1000)

Specs

offset() :: {:ok, number()} | {:error, {Exception.t(), binary()}}

Returns the latest retrieved offset from the SNTP.Retriever

Examples

iex> SNTP.offset()
{:ok, 12}

iex> SNTP.offset()
{:error, {SNTP.RetrieverError, "SNTP Retriever is not started"}}

Specs

start(Enumerable.t()) :: {:ok, pid()}

Starts the SNTP.Retriever

  • options are an Enumerable.t() with these keys:
    • auto_start is a boolean() defaults to true
    • retreive_every is a non_neg_integer() defaults to 86400000 every 24 hour
    • host is binary() | charlist() defualts to 'pool.ntp.org'
    • port is an non_neg_integer() between 0..65535 defualts to 123
    • timeout is an non_neg_integer() defualts to :infinity
    • resolve_reference is a boolean() defualts to false

Examples

iex> SNTP.start()
{:ok, #PID<0.000.0>}
Link to this function

stop(pid \\ GenServer.whereis(Retriever))

View Source

Specs

stop(pid()) :: :ok

Stops the SNTP.Retriever

Examples

iex> SNTP.stop()
:ok

Specs

time(Enumerable.t()) :: {:ok, integer()} | {:error, term()}

Sends a new NTP request on an SNTP.Socket and gracefully closes the socket. Returns {:ok, %SNTP.Timestamp{}} or {:error, reason}

  • options an Enumerable.t() with these keys:
    • host is binary() | charlist() defualts to 'pool.ntp.org'
    • port is an non_neg_integer() between 0..65535 defualts to 123
    • timeout is an non_neg_integer() defualts to :infinity
    • resolve_reference is a boolean() defualts to false

Examples

iex> {:ok, timestamp} = SNTP.time()
iex> timestamp.is_valid?
true

iex> SNTP.time(host: 'ntp.exnet.com', port: 123, timeout: 100))
{:error, [timeout: "Server Timeout after 100"]}