【问题标题】:Why do I need to divide the timestamp by 1 billion?为什么我需要将时间戳除以 10 亿?
【发布时间】:2022-10-25 19:36:44
【问题描述】:

我正在使用 NEAR 协议的这个公共 Postgres DB:https://github.com/near/near-indexer-for-explorer#shared-public-access

有一个名为 included_in_block_timestamp 的字段,其“数据类型”=“数字”,“长度/精度”=20。

此代码有效:

to_char(TO_TIMESTAMP("public"."receipts"."included_in_block_timestamp"/1000000000), 'YYYY-MM-DD HH:mm') as moment, 

这也是:

function convertTimestampDecimalToDayjsMoment(timestampDecimal: Decimal) {
  const timestampNum = Number(timestampDecimal) / 1_000_000_000; // Why is this necessary?
  console.log({ timestampNum });
  const moment = dayjs.unix(timestampNum); // https://day.js.org/docs/en/parse/unix-timestamp
  return moment;
}

例如,有时included_in_block_timestamp = 1644261932960444221。

我从未见过需要除以 10 亿的时间戳。刚刚弄清楚这一点是一个反复试验的问题。

这里发生了什么?这是常见的做法吗?这种精度水平是否有意义?

【问题讨论】:

  • 那是纳秒分辨率,文档说它应该只是 1us 分辨率。
  • 什么种类你认为可能会发生什么?要将纳秒转换为秒,您需要除以十亿。这不是神秘主义,它只是算术。
  • 1644261932960444221 不是一个“时间戳”开始 - 它只是一个数字

标签: postgresql timestamp unix-timestamp nearprotocol


【解决方案1】:

以纳秒为单位的时间戳度量单位似乎是在协议级别确定的,因为这出现在此处的文档中:https://docs.near.org/develop/contracts/environment/#environment-variables

在这里:https://nomicon.io/RuntimeSpec/Components/BindingsSpec/ContextAPI

所以是的,请在日期时间转换之前考虑到这一点。

【讨论】:

    猜你喜欢
    • 2011-12-27
    • 2011-06-08
    • 2018-07-14
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多