繁体   English   中英

使用SQL 2012 Server将UTC时间转换为基准本地时间

[英]Use SQL 2012 Server to convert UTC time to base local time

我在解析Reddit RSS提要时发现,现在是UTC时间。 该服务器位于EST中。 服务器位于离UTC -5小时的时区。 如何将UTC时间码转换为EST?

注意:我还读到UTC并不遵循夏令时(DST),我将弄清楚以后是否通过使用日期范围来调整时差。

RSS Feed中的Reddit Item节点

<item>
<title>blah blah</title>
<link>http://blah.com</link>
<guid isPermaLink="true">http://www.reddit.com/r/blah/comments/blah</guid>
<pubDate>Sun, 16 Sep 2012 21:39:17 -0700</pubDate>
<description>blah description</description>
</item>

到目前为止,我想到了:

DECLARE @d DATETIMEOFFSET;
SET @d = 'Sep 2012 21:39:17 -07:00'

DECLARE @off datetime
SET @off = SWITCHOFFSET(@d, '-05:00')

DECLARE @dates TABLE (
converteddate DATETIME
);

insert into @dates (converteddate)
Values (@off)

select * from @dates

我的例子似乎是我所读内容的答案。 如果您用服务器的UTC时间偏移量偏移了UTC时间,则将获得服务器的本地时间。 在这种情况下,服务器为UTC -05:00

在此示例中,pubDate的节点为:

Sun, 16 Sep 2012 21:39:17 -0700

我解析Reddit RSS feed并将pubDate节点作为文本发送到SQL。 我将pubDate的字符串重新格式化为:

Sep 2012 21:39:17 -07:00

我想出的存储过程:

--@pubdate was sent to SQL from web app as 'Sep 2012 21:39:17 -07:00'

@pubdate varchar(50)

DECLARE @d DATETIMEOFFSET;
SET @d = @pubdate

DECLARE @off datetime
SET @off = SWITCHOFFSET(@d, '-05:00')

INSERT INTO feed (pubdate)
VALUES (@off)

您可以将内置SQL Server函数GETDATE()与GETUTCDATE()的结果进行比较,并使用该结果调整传入的RSS提要日期。 有关更多详细信息,请参见此博客文章: http : //sqlserverperformance.wordpress.com/2007/04/25/one-way-to-convert-from-utc-time-to-local-time/

DECLARE @Mydate SET @Mydate = DATEADD(分钟,DATEDIFF(分钟,GETDATE(),GETUTCDATE()),@DateToConvert)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM