如何在PostgreSQL中的其他时区中返回当前日期 [英] How to return current date in a different timezone in PostgreSQL

查看:86
本文介绍了如何在PostgreSQL中的其他时区中返回当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用东部时间并将数据库设置为太平洋时间的应用程序。这已经引起了一些问题,但是我们被告知无法采取其他任何方式,因此我们只能解决它。

I'm working on an application which uses Eastern time with a database set to Pacific time. This has been causing some issues, but we're told that it can't be any other way, so we just have to work around it.

无论如何,我遇到的麻烦之一就是获取今天的日期。由于数据库位于太平洋地区,如果我在例如凌晨1点使用 current_date 要求今天的日期,它将给我昨天的日期。我曾尝试设置时区和增加/减少间隔,但是它似乎从未像我期望的那样工作,因此我不想在凌晨1点进行大量测试以使其正常工作。

Anyway, one of the things I'm having trouble with is getting today's date. Since the database is in Pacific, if I ask for today's date using current_date at, say, 1AM, it'll give me yesterday's date. I've tried setting timezone and adding/subtracting intervals, but it never seems to work the way I expect it to, and I'd rather not have to do extensive testing at 1AM to get this to work.

是否存在某种简单的方法来返回PostgreSQL中给定时区中今天的日期的DATE?

Is there a somewhat simple way to return a DATE for today's date in a given time zone in PostgreSQL?

推荐答案

选择时区'UTC'的current_date,current_date :: timestamp;
或任何其他区域

select current_date at time zone 'UTC',current_date::timestamp ; or any other zone

更新

选择( UTC时区的当前日期):: date,current_date :: date

这篇关于如何在PostgreSQL中的其他时区中返回当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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