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