地板时间戳记秒 [英] Flooring timestamp to second

查看:93
本文介绍了地板时间戳记秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select current_timestamp(6),    current_timestamp(0)
----------------------------------------------------
2012-02-22 16:03:45.988418+13 | 2012-02-22 16:03:46+13
           -------^                   ------------^ (date from the future)

由于 current_timestamp(0),此查询可以轻松返回第二列的未来时间戳。 code>在数学上舍入。这可能会导致一些意想不到的错误,仅仅是因为检索到的时间是从将来到0.5s。

This query can easily return the timestamp from the future for the second column because of current_timestamp(0) is rounding "mathematically". This could lead to some unexpected errors, just because the retrieved time is from the future up to 0.5s.

所以我的问题是-最简单的舍入方法 current_timestamp(6)到最接近的秒数吗?

So my question is - what is the simplest way to round current_timestamp(6) to the nearest second down?

PS:我知道转换为秒数并返回的解决方案

PS: I know the solution with converting to seconds and back

PPS:结果应为时间戳类型,而不是字符串

PPS: The result should be a timestamp type, not a string

UPD

找到了解决方案

select current_timestamp(6), current_timestamp(0)::abstime


推荐答案

select current_timestamp(6),    date_trunc('second', current_timestamp(6))

这篇关于地板时间戳记秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆