在Postgres中获取当前本地时间的最干净的方法是什么? [英] What's the cleanest way to get the current local time in Postgres?

查看:76
本文介绍了在Postgres中获取当前本地时间的最干净的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Postgres中是否有更简洁的方法来写以下内容:

Is there a more succinct way to write the following in Postgres:

DATE(NOW() AT TIME ZONE time_zone)

ie要获取本地时区为 time_zone 的记录的时间?

i.e. to get the local time for a record with time zone time_zone?

理想情况下,我想执行以下操作

Ideally I'd like to do something like this:

NOW(time_zone)

那是行不通的,但是有什么可以使用的类似清洁的东西吗?

That doesn't work but is there anything similar and clean I can use?

推荐答案

=> SELECT DATE(NOW() AT TIME ZONE 'UTC');
2013-11-19

=> SELECT DATE(NOW() AT TIME ZONE 'PST');
2013-11-18

更新

如果您希望将此查询抽象为自定义函数,您可以按以下步骤创建一个:

If you're looking to abstract this query into a custom function, you can create one as follows:

=> CREATE FUNCTION date_with_time_zone() RETURNS date AS '
     SELECT DATE(NOW() AT TIME ZONE ''PST'');
     ' LANGUAGE SQL;
CREATE FUNCTION

=> SELECT date_with_time_zone();
2013-11-18

尽管您仍然需要发出 SELECT 命令,只需调用函数即可返回带时区的当前日期,而不是整个查询。

While you still need to issue the SELECT command, the current date with time zone will be returned simply by invoking the function, rather than the entire query.

这篇关于在Postgres中获取当前本地时间的最干净的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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