LoginSignup
2
2

More than 5 years have passed since last update.

ElixirでMySQLのdatetime型をUNIX TIMESTAMPに変換する

Posted at

date形式をUNIX TIMESTAMPに変換するのは英語の記事があったけど
MySQLのdatetimeをElixirで扱うとmsecまで入ってたので改良。

defmodule UnixTime do
  def convert_date_to_unixtime(created_at) do
    #JSTの場合9Hにしておく
    epoch = {{1970, 1, 1}, {9, 0, 0}}
    epoch_gs = :calendar.datetime_to_gregorian_seconds(epoch)

    {{year, month, day}, {hour, minute, second, msec}} = created_at
    gs = :calendar.datetime_to_gregorian_seconds({{year, month, day}, {hour, minute, second}})
    gs - epoch_gs
  end
end

利用方法

UnixTime.convert_date_to_unixtime(updated_at)

1432364949 みたいなのが返却される

参考:
http://michal.muskala.eu/2015/07/30/unix-timestamps-in-elixir.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2