我将数据存储为 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
发布评论
评论(1)
将 UTC 格式日期转换为 CST 或 EST(例如)的标准时间非常简单。我们必须将应用程序中需要的每个时区的偏移值存储在单独的列或表中。
例如:UTC 到 CST 是 -6 小时。同样,UTC 到 EST 是 -5 小时。
一旦我们将夏令时纳入计算中,事情就会变得复杂。但如果给定日期属于夏令时,则上述计算将不起作用。
那么夏令时是如何计算的?
如果年份 <= 2006,则夏令时介于:
4 月第一个星期日凌晨 2 点至 10 月最后一个星期日凌晨 2 点
如果年份 >= 2007,则夏令时介于:
3 月第二个星期日凌晨 2 点至 11 月第一个星期日凌晨 2 点
UTC 至 CST(标准时间)= -6
UTC 至 CDT(夏令时)时间)= -5
UTC 至 EST(标准时间)= -5
在此处查看处理夏令时的解决方案 - 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.
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?
If the year <= 2006 then daylight saving is between:
2 am on First Sunday in April till 2 am on Last Sunday in October
If the year >= 2007 then daylight saving is between:
2 am on Second Sunday in March till 2 am on First Sunday in November
UTC to CST (Standard Time) = -6
UTC to CDT (Daylight Time) = -5
UTC to EST (Standard Time) = -5
Check out the solution for handling daylight saving here - http://vadivel.blogspot.com/2011/10/timezone-conversion-utc-to-cst-with.html