【问题标题】:Extract date,month,year and month name from the unix timestamp with postgresql使用 postgresql 从 unix 时间戳中提取日期、月份、年份和月份名称
【发布时间】:2015-05-23 18:15:03
【问题描述】:

我将 postgres 用于 rails 应用程序,并且我在 postgresql 数据库中有一个 unix timestamp。我需要选择 group by dd-mm-yyyymonth name

假设我有以下 unix 时间戳

1425148200

我需要将其更改为 datetime 并且我使用了返回的 to_timestamp

2015-02-28 18:30:00 UTC

我尝试使用

datetime 转换为 local timezone

::timestamp 没有时区 AT TIME ZONE 'IST'

但这并没有给出所需时区的时间,而是返回了

2015-02-28 16:30:00 UTC

我尝试使用返回的::date 获取日期部分

2015 年 2 月 28 日,星期六

所以请帮我从 unix 时间戳中获取指定 timezone 中的 dd-mm-yyyy 和月份名称(March)。

提前致谢!

【问题讨论】:

  • did not return time in required timezone - 这是什么意思?什么回报?
  • 我已经更新了 :timestamp without time zone AT TIME ZONE 'IST' 的结果
  • 这:select DATE(to_timestamp(1425148200)) 是你想要的吗?这将返回 2015-02-28,您要求的日期格式。
  • 欢迎来到 Ruby(Rails) 的 TimeZone 地狱

标签: sql ruby-on-rails postgresql datetime psql


【解决方案1】:

选择 to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')

2015 年 1 月 3 日,当然还有 3 月

我猜这是 postgres 的错误 根据http://www.postgresql.org/docs/7.2/static/timezones.html

【讨论】:

  • IST 中与 '1425148200' 对应的时间是“3/1/2015, 12:00:00 AM GMT+5:30”,但它给出的时间为 2015-02-28 20:30: 00 UTC
  • @Logesh 有四个带有 IST 缩写的时区。 postgres 了解以色列
  • 是的,我对此进行了调查,我可以通过您提供的解决方案以某种方式解决此问题,我还尝试获取偏移量并添加到时间戳,这也很有效。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2020-12-24
  • 2018-10-15
  • 1970-01-01
相关资源
最近更新 更多