美文网首页
PostgreSQL和elixir项目使用时间戳时区

PostgreSQL和elixir项目使用时间戳时区

作者: 一剑无血冯稀饭 | 来源:发表于2019-07-19 11:03 被阅读0次

phoenix默认创建时间戳为

timestamps()

首先将数据时区设置为+8

set timezone to '+8';

然后将exs文件创建字段修改为

timestamps(type: :timestamptz)

将schema定义修改为

timestamps(type: :utc_datetime)

当phoenix项目需要格式化时间,使用calendar插件

# inserted_at = #DateTime<2016-05-01 12:38:59Z>
Calendar.DateTime.shift_zone!(inserted_at, "Asia/Shanghai")|> Calendar.Strftime.strftime!("%Y-%m-%d %H:%M:%S")
# "2016-05-01 20:38:59"

相关文章

网友评论

      本文标题:PostgreSQL和elixir项目使用时间戳时区

      本文链接:https://www.haomeiwen.com/subject/qerjlctx.html