可以在PostgreSQL中进行时区操作吗? PostgreSQL可以访问当前用户的时区吗?
我的日期存储为 date postgresql中的值:06-Jun-2022
。
我需要将其更改为时间戳记,并有时间在纽约上午9点在纽约的用户时区:
- 对于犹他州的用户(落后纽约2小时),时间将设置为7 AM,因为当犹他州上午7点时,是纽约上午9点。
- 对于加利福尼亚州(落后纽约3小时)的用户,时间将设置为凌晨6点,因为当加利福尼亚州上午6点在纽约,是上午9点。
我可以使用 date-fns-tz 软件包在前端的JavaScript中很容易地执行此操作 - 当我的计算机的时间手动设置为犹他州时间时,给定日期06-- 2022 Jun-2022
,返回的值是6月06日2022 07:00:00 GMT-0600
,这正是我想要的。代码如下:
const { zonedTimeToUtc } = require("date-fns-tz");
export function fixTimeZone(date) {
let currentTimeStamp = new Date((date += " 09:00::00"));
let correctTimeStamp = zonedTimeToUtc(currentTimeStamp, "America/New_York");
return correctTimeStamp;
}
是否有一种方法可以在PostgreSQL中获得类似的结果,因此日期将转换为时间戳,并在将其发送给客户端之前使用正确的时间? PostgreSQL甚至可以访问用户的时区,还是必须在前端完成?
I have dates stored as a DATE value in PostgreSQL like this: 06-JUN-2022
.
I am needing to change it to a time stamp and have the time be the equivalent of 9 AM in New York for the user's time zone:
- For users in Utah (2 hours behind New York), the time would be set to 7 AM because when it is 7 AM in Utah, it is 9 AM in New York.
- For users in California (3 hours behind New York), the time would be set to 6 AM because when it is 6 AM in California, it is 9 AM in New York.
I was easily able to do this in JavaScript on the front-end using the date-fns-tz package—when my computer's time is manually set to Utah time, when given the date 06-JUN-2022
, the returned value is Mon Jun 06 2022 07:00:00 GMT-0600
, which is exactly what I want. The code is as follows:
const { zonedTimeToUtc } = require("date-fns-tz");
export function fixTimeZone(date) {
let currentTimeStamp = new Date((date += " 09:00::00"));
let correctTimeStamp = zonedTimeToUtc(currentTimeStamp, "America/New_York");
return correctTimeStamp;
}
Is there a way to achieve a similar result in PostgreSQL, so the dates are converted to a timestamp with the correctly-altered time before they are sent to the client? Does PostgreSQL even have access to the user's time zone, or must it be done on the front-end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似:
请参阅更多信息。
Something like:
See AT TIME ZONE for more information.