[elixir! #0048] 好玩的时间戳数据格式

逝者如斯夫, 不舍昼夜.

时间戳的处理恐怕是我们日常编程中经常要面对的问题, 尤其是涉及到国际服务的时候, 如何存储正确的时间, 就特别重要.

一般我们使用 UTC 时间, 也就是 0 时区的时间作为标准时间.

获取当前的 UTC 时间戳:

iex(1)> DateTime.utc_now
~U[2019-05-20 04:50:52.943370Z]

这得到的是个啥?其实是一个 struct:

iex(2)> Map.from_struct v()          
%{
  calendar: Calendar.ISO,
  day: 20,
  hour: 4,
  microsecond: {943370, 6},
  minute: 50,
  month: 5,
  second: 52,
  std_offset: 0,
  time_zone: "Etc/UTC",
  utc_offset: 0,
  year: 2019,
  zone_abbr: "UTC"
}

这里面有 calendar, 翻译成 历法 应该没错, 搜索了一下, 发现世界上有四十种历法呢, 真了不起.

clipboard.png

elixir 标准库里默认用的是最常用的 ISO 国际标准历法, 也就是我们使用的公历. 可以看到上面的 DateTime 表示的是 "2019 年 5 月 20 日 4 小时 50 分钟 52 秒, 时区 'Etc/UTC'". 注意到
microsecond 微秒有些特别, 它的元组的第二位表示精度, 也就是转换成其它格式的时候必须要显示几位数字, 这里 6 表示要显示 6 位.

Calendar 模块里面定义了一系列的 callback, 如果你想实现一个自己的历法模块, 就需要实现这些 callbacks.

我们看看标准的 Calendar.ISO 是如何实现这些 callbacks 的:

  @spec days_in_month(year, month) :: 28..31
  @impl true
  def days_in_month(year, month)

  def days_in_month(year, 2) do
    if leap_year?(year), do: 29, else: 28
  end

  def days_in_month(_, month) when month in [4, 6, 9, 11], do: 30
  def days_in_month(_, month) when month in 1..12, do: 31

某年的某月有多少天.

  @impl true
  @spec months_in_year(year) :: 12
  def months_in_year(_year) do
    @months_in_year
  end

某年有多少月.

  @spec leap_year?(year) :: boolean()
  @impl true
  def leap_year?(year) when is_integer(year) do
    rem(year, 4) === 0 and (rem(year, 100) !== 0 or rem(year, 400) === 0)
  end

某年是否是闰年.

  @spec day_of_week(year, month, day) :: 1..7
  @impl true
  def day_of_week(year, month, day)
      when is_integer(year) and is_integer(month) and is_integer(day) do
    iso_days_to_day_of_week(date_to_iso_days(year, month, day))
  end

  defp iso_days_to_day_of_week(iso_days) do
    Integer.mod(iso_days + 5, 7) + 1
  end

某年某月某日是星期几.

  @spec day_of_year(year, month, day) :: 1..366
  @impl true
  def day_of_year(year, month, day)
      when is_integer(year) and is_integer(month) and is_integer(day) do
    ensure_day_in_month!(year, month, day)
    days_before_month(month) + leap_day_offset(year, month) + day
  end

某年某月某日是这年的第几天.

  @spec quarter_of_year(year, month, day) :: 1..4
  @impl true
  def quarter_of_year(year, month, day)
      when is_integer(year) and is_integer(month) and is_integer(day) do
    div(month - 1, 3) + 1
  end

某年某月某日是这年的第几季度.

  @spec year_of_era(year) :: {year, era :: 0..1}
  @impl true
  def year_of_era(year) when is_integer(year) and year > 0 do
    {year, 1}
  end

  def year_of_era(year) when is_integer(year) and year < 1 do
    {abs(year) + 1, 0}
  end

某年的纪元(公元后, 公元前)

还有一些这里这就赘述了, 感兴趣的朋友可以看 elixir Calendar.ISO 模块的代码.

你可能感兴趣的:(elixir)