1

I was parsing a Reddit RSS feed and noticed that the time was in UTC. The server is located in the EST. The server is located in the timezone that is -5hrs from the UTC. How do I convert the UTC timecode to EST?

Note: I also read that UTC doesn't follow daylight savings time (DST), I'll figure out whether to adjust the hour difference later on by using date ranges.

Reddit item node in rss feed

<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>

I came up with this so far:

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
4

3 に答える 3

3

私の例は、私が読んだことからの答えのようです。サーバーの UTC 時間オフセットによって UTC 時間をオフセットすると、サーバーのローカル時間が得られます。この特定のケースでは、サーバーは UTC -05:00 です。

この例では、pubDate のノードは次のとおりです。

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

Reddit RSS フィードを解析し、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)
于 2012-09-20T23:08:29.607 に答える
1

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

于 2014-03-07T11:36:33.017 に答える
1

組み込みの SQL Server 関数 GETDATE() と GETUTCDATE() の結果を比較し、その結果を使用して着信 RSS フィードの日付を調整できます。詳細については、次のブログ記事を参照してください: http://sqlserverperformance.wordpress.com/2007/04/25/one-way-to-convert-from-utc-time-to-local-time/

于 2012-09-17T03:58:21.110 に答える