Packages
verk
1.3.0
1.7.3
1.7.2
1.7.1
1.7.0
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.0
1.4.1
1.4.0
1.3.0
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.2
1.0.1
1.0.0
0.14.1
0.14.0
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.13
0.9.12
0.9.11
0.9.10
0.9.9
0.9.8
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
Verk is a job processing system backed by Redis.
Current section
Files
Jump to
Current section
Files
lib/verk/time.ex
defmodule Verk.Time do
@moduledoc """
Internal API for time management and comparison functions
"""
@doc false
@spec now() :: DateTime.t
def now do
DateTime.utc_now
end
@doc """
Moves DateTime supplied the indicated amount of time.
Supported units are those specified by `System.time_unit`
"""
@spec shift(DateTime.t, integer, System.time_unit) :: DateTime.t
def shift(%DateTime{} = datetime, amount, units \\ :seconds) when is_integer(amount) do
datetime
|> DateTime.to_unix(units)
|> Kernel.+(amount)
|> DateTime.from_unix!(units)
end
@doc """
Returns the number of indicated units that separate the two timestamps.
A positive result indicates that `datetime2` occurred after `datetime1`.
"""
@spec diff(DAteTime.t, DateTime.t, System.time_unit) :: integer
def diff(datetime1 = %DateTime{}, datetime2 = %DateTime{}, units \\ :seconds) do
unix_dt1 = DateTime.to_unix(datetime1, units)
unix_dt2 = DateTime.to_unix(datetime2, units)
unix_dt2 - unix_dt1
end
@doc """
returns true if the first argument occurred after the second argument
"""
@spec after?(DateTime.t, DateTime.t) :: boolean
def after?(datetime1, datetime2) do
diff(datetime1, datetime2) < 0
end
end