GET UTC过去日期的日期 [英] GET UTC Date of a past date

查看:120
本文介绍了GET UTC过去日期的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库的日期值以GMT格式保存在GMT时区。我正在尝试将日期转换为当地时区,但是当日期是过去的日期时,会出现问题,例如在2012年12月1日的日期为-5日的情况下,2010年6月15日将为-4到夏令时。我现在在EST。

My database have date values saved in GMT time zone in int format. I am trying to convert the date to local timezone, but the issue arises when the date is a past date, For instance in my case offset for date Dec 1, 2012 will be -5 and for June 15, 2010 will be -4 due to daylight savings. I am currently in EST.

所以我需要知道前一个日期的UTC日期是能够确定是否将-4或-5的偏移量

So i need to know what was the UTC date of a previous date to be able to determine whether -4 or -5 will be the offset for that date.

SELECT Test_Number,
Last_Test_Date, dateAdd(hour, 
        datediff(hour, GETUTCDATE(), getdate()), --UTC offset
        dateadd(second, Last_Test_Date, '1/1/1970 12:00 AM'))
FROM TestTable

我不完全确定是否有可能。任何意见?

I am not entirely sure if it is even possible. Any opinion ?

推荐答案

这听起来像是您实际上正在将存储在数据库中的UTC时间转换为本地日期/时间正确的偏移量(-5或-4)。

It sounds like you are actually looking to convert a UTC time stored in the database to a local date/time with the correct offset (-5 or -4).

在SQL Server中没有好的方法。 至少,还没有。

我可以提供的最好的建议是在数据库的转换中不要这样做。相反,将UTC值按原样传递到您的应用程序代码,并在那里进行转换。

The best advice I can offer is to not do this in conversion in the database. Instead, pass the UTC value as-is back to your application code, and do the conversion there.

例如,如果您的应用程序是用.NET编写的,您可以使用 TimeZoneInfo Noda时间,以处理您希望的任何时区的转化。如果您正在使用别的东西,请通知我,我会更新此问题。

For example, if your application is written in .NET, you can use TimeZoneInfo or Noda Time to handle the conversions to any time zone you wish. If you're using something else, let me know and I will update the question.

这篇关于GET UTC过去日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆