SQL Server 2008 - 如何将GMT(UTC)日期时间转换为本地日期时间?

时间:2011-05-19 20:44:23

标签: sql sql-server tsql sql-server-2008 datetime

我有一个插入过程,它在GETDATE()中作为其中一个值传递,因为每个插入也会在插入时存储。它托管在SQL Azure上 - 它使用GMT。

现在,当我收到消息时,我在其时间戳列中为每个消息存储了GMT日期,当您访问我的页面时,如何将其转换为本地datetime

感谢。

5 个答案:

答案 0 :(得分:11)

你可以这样做:

declare @InputUtcDateTime datetime2 = '2011-05-20 06:30:18'

declare @LocalDateTime datetime2 = dateadd(minute, datepart(TZoffset, sysdatetimeoffset()), @InputUtcDateTime)
print @LocalDateTime

declare @InputUtcDateTime datetime2 = '2011-05-20 06:30:18'

declare @LocalDateTime datetime2 = dateadd(minute, datediff(minute, sysutcdatetime(), sysdatetime()), @InputUtcDateTime)
print @LocalDateTime

答案 1 :(得分:5)

除了夏令时问题,为什么不简化:

yourDateTime - getutcdate() + getdate()

答案 2 :(得分:1)

以MST为例......考虑到每个DTM已经存储在GMT中,这简化了事情。

SWITCHOFFSET(CONVERT(DATETIMEOFFSET, [ColumnName]), '-07:00')

现在,如果您的本地日期/时间不是GMT / UTC,您可能会想要使用以下内容......

SWITCHOFFSET(TODATETIMEOFFSET([ColumnName], datepart(tz,sysdatetimeoffset())),'+00:00')

这是故障。

  • SWITCHOFFSET - 将DateTimeOffset值转换为不同的时区,同时保留偏移量。
  • TODATETIMEOFFSET - 将DateTime值转换为指定时区的DateTimeOffset值。
  • DATEPART - 在这种情况下,获取本地日期时间的时区部分。
  • '+00:00' - 目标偏移量,在第二个例子中是UTC / GMT目标,来自本地...前一个例子是MST。

注意/警告:我不认为这会影响夏令时,这对您来说可能是个问题。如果不需要绝对保存,您可能只想添加一个二级列,进行粗略转换并安全前进。

您可能希望将逻辑抽象为函数调用,以便考虑DST的保留......但是,这不应该是非常困难的。

答案 3 :(得分:0)

这是一个适用于历史数据的功能。我是为英国夏令时写的 - 不幸的是发生在3月和10月的最后一个星期天,使逻辑变得有些复杂。

基本上硬编码的日期部分01/03正在寻找三月的最后一个星期日,而01/10正在寻找十月的最后一个星期日(这是时钟前进和后退的时间)。注意:如果您的服务器使用本地美国日期,请将这两个日期部分反映至03/01和10/01 !!!!

所以你给它一个UTC日期,它会自动计算出历史日期是BST还是GMT。不是在大数据集上使用的最好的东西,但它是一个解决方案。

运行此脚本以创建该函数并在您的select中将其调用。 SQL 2008有一个用户定义函数的问题,似乎它在代码下面放了一个红线,但只要你使用dbo前缀(SELECT dbo.UTCConvert(yourdate)来运行它)它仍会运行它。

CREATE FUNCTION [dbo].[UTCConvert] 
(

    @p1 datetime
)
RETURNS datetime
AS
BEGIN

    DECLARE @Result datetime


RETURN CASE 
WHEN
@p1 >
(DATEADD(day,DATEDIFF(day,'19000107',DATEADD(month,DATEDIFF(MONTH,0,'01/03/' + CAST(DATEPART(year,@p1) as CHAR)),30))/7*7,'19000107'))
AND
@p1<
(DATEADD(day,DATEDIFF(day,'19000107',DATEADD(month,DATEDIFF(MONTH,0,'01/10/' + CAST(DATEPART(year,@p1) as CHAR)),30))/7*7,'19000107'))
THEN (DATEADD(HH, 1, @p1)) 
ELSE @p1
END
END

答案 4 :(得分:0)

/*
=============================================
 Author:        Mark Griffiths
 Create date:   29/05/2018
 Description:   BST runs from 02:00AM on the last Sunday of March to the same time on the last Sunday of October.       
    The Series of DATEDIFFs and DATEADDS below function as follows
    1   ●   Count the number of months there have been between the given date and start of computer time
    2   ●   Add that number of months to the end of the first month to get the end of the given month
    3   ●   Count the number of days there have been between the end of the given month and the first Saturday
    4   ●   Add that number of days to the calculated end of the given month
    5   ●   Add Two hours to that time as the clocks go back at 02:00 in the morning

    I know that the tabbing below makes it all look odd, but the description above is the best way I could find to comment things, given the nesting...
    The comments in the code below should help find the nesting levels and the numbers refer to the bullet points above.
=============================================
-- Test Variables --
DECLARE @GMTime DATETIME2(3) = '2018-05-01 12:00:00.000'
*/

    DECLARE @RealTime As DATETIME2(3)
    DECLARE @Year VARCHAR(4)
    SET @Year = CONVERT(VARCHAR,DATEPART(YEAR,@GMTime))
    DECLARE @StartOfBST AS DATETIME
    DECLARE @EndOfBST AS DATETIME
    SELECT
        @StartOfBST =
        DATEADD     -----------------------------------------------------------------------------------------
            (                                                       --                                      |
             HOUR                                                   --                                      |
            ,2                                                      --                                      |
            ,DATEADD    -----------------------------------------------------------------------------       |
                (                                                   --                              |       |
                 DAY                                                --                              |       |
                ,DATEDIFF       -------------------------------------------------------------       |       |
                    (                                               --                      |       |       |
                     DAY                                            --                      |       |       |
                    ,'19000107'                                     --                      |       |       5
                    ,DATEADD            ---------------------------------------------       |       |       |
                        (                                           --              |       3       4       |
                         MONTH                                      --              |       |       |       |
                        ,DATEDIFF(MONTH,0,CONVERT(DATE,'03/01/' + @Year)) -- 1      2       |       |       |
                        ,CONVERT(DATE,'01/30/1900')                 --              |       |       |       |
                        )               ---------------------------------------------       |       |       |
                    )/7*7       -------------------------------------------------------------       |       |
                    ,'19000107'                                     --                              |       |
                )               ---------------------------------------------------------------------       |
            ),      -----------------------------------------------------------------------------------------
    @EndOfBST =
        DATEADD(HOUR,2,DATEADD(day,DATEDIFF(day,'19000107',DATEADD(month,DATEDIFF(MONTH,0,CONVERT(DATE,'10/01/' + @Year)),30))/7*7,'19000107'))
    SET @RealTime = CASE
                    WHEN @GMTime BETWEEN @StartOfBST AND @EndOfBST THEN DATEADD(HOUR,-1,@GMTime)
                    ELSE @GMTime
                END
RETURN @RealTime;
--SELECT @RealTime
END
相关问题