可以在PostgreSQL中进行时区操作吗? PostgreSQL可以访问当前用户的时区吗?

发布于 2025-02-10 09:26:08 字数 831 浏览 0 评论 0原文

我的日期存储为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2025-02-17 09:26:08

类似:


select 
   ('06-JUN-2022'::date + time '9:00') 
at time zone 'America/New_York' AT TIME ZONE 'America/Denver';
      timezone       
---------------------
 06/06/2022 07:00:00


select 
   ('06-JUN-2022'::date + time '9:00') 
at time zone 'America/New_York' AT TIME ZONE 'America/Los_Angeles';
      timezone       
---------------------
 06/06/2022 06:00:00

请参阅更多信息。

Something like:


select 
   ('06-JUN-2022'::date + time '9:00') 
at time zone 'America/New_York' AT TIME ZONE 'America/Denver';
      timezone       
---------------------
 06/06/2022 07:00:00


select 
   ('06-JUN-2022'::date + time '9:00') 
at time zone 'America/New_York' AT TIME ZONE 'America/Los_Angeles';
      timezone       
---------------------
 06/06/2022 06:00:00

See AT TIME ZONE for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文