ExKits.Utils.Time (ex_kits v0.2.8)

View Source

时间相关工具

Summary

Functions

china_now()

@spec china_now() :: {:ok, DateTime.t()}

china_today()

@spec china_today() :: Date.t()

date_to_datetime(date, tz \\ "Asia/Shanghai")

@spec date_to_datetime(Date.t(), binary()) :: DateTime.t()

datetime_to_naive(datetime)

@spec datetime_to_naive(DateTime.t()) :: NaiveDateTime.t()

datetime_to_str(datetime, format \\ "%Y-%m-%d %H:%M:%S")

@spec datetime_to_str(DateTime.t(), String.t()) :: String.t()

datetime_to_ts(datetime)

@spec datetime_to_ts(DateTime.t()) :: integer()

naive_now(typ \\ :second)

@spec naive_now(atom()) :: NaiveDateTime.t()

naive_to_datetime(ndt, tz \\ "Asia/Shanghai")

@spec naive_to_datetime(NaiveDateTime.t(), Calendar.time_zone()) ::
  {:ok, DateTime.t()}

naive_to_datetime!(ndt, tz \\ "Asia/Shanghai")

@spec naive_to_datetime!(NaiveDateTime.t(), Calendar.time_zone()) :: DateTime.t()

now(tz \\ "Etc/UTC")

@spec now(Calendar.time_zone()) :: {:ok, DateTime.t()}

now!(tz \\ "Etc/UTC")

@spec now!(Calendar.time_zone()) :: DateTime.t()

str_to_datetime(s, shift \\ "+08:00")

@spec str_to_datetime(String.t(), String.t()) :: {:ok, DateTime.t()}

timestamp(typ \\ :seconds)

@spec timestamp(atom()) :: integer()

get current timestamp

Example

iex> Common.TimeTool.timestamp(:seconds) 1534466694 iex> Common.TimeTool.timestamp(:milli_seconds) 1534466732335 iex> Common.TimeTool.timestamp(:micro_seconds) 1534466750683862 iex> Common.TimeTool.timestamp(:nano_seconds) 1534466778949821000

ts_to_datetime(ts, tz \\ "Asia/Shanghai")

@spec ts_to_datetime(integer(), Calendar.time_zone()) :: {:ok, DateTime.t()}