Sql Server 中的夏令时 [英] Daylight savings time in Sql Server
问题描述
我们正在使用以 C/Unix 格式存储日期的旧应用程序.C 时间基本上是自 1970 年 1 月 1 日以来的秒数.日期作为整数存储在 SQL Server 数据库中.我正在为使用这些日期的报告编写视图.
We're using an old application that stores dates in C / Unix format. C time is basically the number of seconds since Jan 1st, 1970. The dates are stored as an integer in a SQL Server database. I am writing a view for a report that uses these dates.
到目前为止,我正在从 UNIX 时间转换为本地日期时间:
So far, I'm converting from the UNIX time to a native datetime with:
DateAdd(s,3600+unix_time,'1/1/1970')
3600 是从 UTC 转换为我们当地的 GMT+1 时区.这在冬天是准确的,但在夏天,由于夏令时,它会关闭一小时.
The 3600 is to convert from UTC to our local GMT+1 timezone. This is accurate in the winter, but in the summer it's one hour off due to daylight savings time.
在 SQL Server 中是否有将 UTC 转换为本地时间的内置方法?
Is there a built-in way to convert from UTC to local time in SQL Server?
推荐答案
你需要做 DateDiff(s, getutcdate(), getdate())+unix_time,而不是 3600,它会给你正确的偏移量UTC 时间.
Instead of 3600, you'll want to do DateDiff(s, getutcdate(), getdate())+unix_time, which will give you the correct offset from the UTC time.
干杯,
埃里克
这篇关于Sql Server 中的夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!