如何正确存储 UTC 日期时间并使用本地偏移量?

发布于 2024-12-28 10:02:56 字数 317 浏览 3 评论 0 原文

我将数据存储为 UTC,但我对如何正确检索它感到非常困惑。我将该值作为 UTC 存储在 MSSQL 的日期时间列中。目前我检索它的方法是将 GMT 偏移设置为开始日期时间范围和结束日期时间范围。我还需要将聚合应用于该日期时间字段(分组依据)并将其显示在用户正确的本地时区中。我遇到的问题是,如果我今天查找一个范围,它会提取正确匹配的列,但这些列中的日期时间值可能是不同的 UTC 天;我是按天分组的。例如,当有人选择一天的开始和结束(今天)的范围时,他们会看到昨天和今天的 UTC 日期时间值;但如果我尝试将 GMT 偏移量应用于显示值,它仍然可能显示为 2 个不同的日期。如何正确协调范围和显示?

谢谢

I am storing data as UTC, but I am very confused on how to properly retrieve it. I store the value as UTC in a datetime column in MSSQL. Currently the way I retrieve it is by setting the GMT offset to the start datetime range and end datetime range. I am also needing to apply aggregation to that datetime field (group by) and display it in the user's proper local timezone. The issue I am having is say if I look for a range today it will pull up columns that match correctly, but the datetime value in those columns can be different UTC days; I am grouping by day. So for example when someone selects a range for the beginning of the day and the end (for today) they are presented with UTC datetime values from yesterday and today; but if I try to apply the GMT offset to the display value it still comes out as possibly 2 different dates. How can I properly coordinate the range and the display?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

念﹏祤嫣 2025-01-04 10:02:56

将 UTC 格式日期转换为 CST 或 EST(例如)的标准时间非常简单。我们必须将应用程序中需要的每个时区的偏移值存储在单独的列或表中。

例如:UTC 到 CST 是 -6 小时。同样,UTC 到 EST 是 -5 小时。

DECLARE @UTC_Date DATETIME
SET @UTC_Date = GETUTCDATE()

SELECT
 @UTC_Date AS [UTC],
 DATEADD(hh, -6, @UTC_Date) AS [CST - Standard Time],
 DATEADD(hh, -5, @UTC_Date) AS [EST - Standard Time]

一旦我们将夏令时纳入计算中,事情就会变得复杂。但如果给定日期属于夏令时,则上述计算将不起作用。

那么夏令时是如何计算的?

  1. 如果年份 <= 2006,则夏令时介于:
    4 月第一个星期日凌晨 2 点至 10 月最后一个星期日凌晨 2 点

  2. 如果年份 >= 2007,则夏令时介于:
    3 月第二个星期日凌晨 2 点至 11 月第一个星期日凌晨 2 点

  3. UTC 至 CST(标准时间)= -6

  4. UTC 至 CDT(夏令时)时间)= -5

  5. UTC 至 EST(标准时间)= -5

  6. UTC 至EDT(夏令时)= -4

在此处查看处理夏令时的解决方案 - http://vadivel.blogspot.com/2011/10/timezone-conversion-utc-to-cst-with.html

Converting UTC format date to Standard time of CST or EST (for example) is straight forward. We have to store the offset value in a separate column or table for each timezone which we need in our application.

For example: UTC to CST is -6 hrs. Similarly UTC to EST is -5 hrs.

DECLARE @UTC_Date DATETIME
SET @UTC_Date = GETUTCDATE()

SELECT
 @UTC_Date AS [UTC],
 DATEADD(hh, -6, @UTC_Date) AS [CST - Standard Time],
 DATEADD(hh, -5, @UTC_Date) AS [EST - Standard Time]

It gets complicated once we bring in Daylight saving into calculation. But if the given date falls under daylight saving then the above calculation won't work.

So how is Daylight saving calculated?

  1. If the year <= 2006 then daylight saving is between:
    2 am on First Sunday in April till 2 am on Last Sunday in October

  2. If the year >= 2007 then daylight saving is between:
    2 am on Second Sunday in March till 2 am on First Sunday in November

  3. UTC to CST (Standard Time) = -6

  4. UTC to CDT (Daylight Time) = -5

  5. UTC to EST (Standard Time) = -5

  6. UTC to EDT (DayLight Time) = -4

Check out the solution for handling daylight saving here - http://vadivel.blogspot.com/2011/10/timezone-conversion-utc-to-cst-with.html

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