Convert historic dates from utc to local time
我有一个包含这样的数据的SQL表:
1 2 3 4 5 6 | | theDate (datetime) | theValue (INT) | ---------------------------------------- | 2010-05-17 02:21:10 | 5 | | 2009-03-12 04:11:35 | 23 | | 2010-02-19 18:16:53 | 52 | | 2008-07-07 22:54:11 | 30 | |
日期以UTC格式存储在日期时间列中,如何将它们转换为当地时间(挪威)? 请记住,由于冬季/夏季,UTC全年的UTC偏移量并不相同。
更新:我正在使用Microsoft SQL Server 2005,我需要从SQL执行此操作,因为数据将在稍后显示给报告。
此声明将对您列出的表格起作用。 使用逻辑,您可以将其应用于任何日期字段。
1 2 3 4 5 6 7 8 9 10 | SELECT CASE WHEN [theDate] BETWEEN CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR([theDate])) + '-03-' + CONVERT(VARCHAR(2), (31 - (5 * YEAR([theDate])/4 + 4) % 7)) + ' 02:00:00', 20) AND CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR([theDate])) + '-10-' + CONVERT(VARCHAR(2), (31 - (5 * YEAR([theDate])/4 + 1) % 7)) + ' 03:00:00', 20) THEN Dateadd(hh, 2, [theDate]) ELSE Dateadd(hh, 1, [theDate]) END AS [theDate], [theValue] FROM [YOURTABLE] |
写下CLR函数,将你的SqlDateTime从UTC转换为你的CLR本地,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 | [Microsoft.SqlServer.Server.SqlFunction] public static SqlDateTime ToLocalTime(SqlDateTime dt) { try { RETURN TimeZone.CurrentTimeZone.ToLocalTime(dt.Value); } catch { RETURN SqlDateTime.Null; } } |
这是我从当地时间返回当地冬季时间的功能
根据3月的最后一个星期日的2:00和10月的3月的最后一个星期日的时间变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | USE your_DB GO CREATE FUNCTION [dbo].[ConvertToLocalWinterTime](@instime datetime) RETURNS datetime AS BEGIN DECLARE @localtm datetime = @instime DECLARE @localwintertm datetime DECLARE @lastmarchsunday datetime DECLARE @lastoctobersunday datetime DECLARE @tmpmd datetime = '03.31.' + CONVERT(CHAR(4),YEAR(@localtm)) DECLARE @tmpod datetime = '10.31.' + CONVERT(CHAR(4),YEAR(@localtm)) --- last sunday in march DECLARE @DOFWEEKM INT = datepart (weekday,@tmpmd-1) IF @DOFWEEKM <7 BEGIN SET @lastmarchsunday = @tmpmd - @DOFWEEKM END ELSE BEGIN SET @lastmarchsunday = @tmpmd END ----last sunday in October DECLARE @DOFWEEKO INT = datepart (weekday,@tmpod-1) IF @DOFWEEKO <7 BEGIN SET @lastoctobersunday = @tmpod - @DOFWEEKO END ELSE BEGIN SET @lastoctobersunday = @tmpod END ------------------------------- IF (@localtm > dateadd(hh,2,@lastmarchsunday)) AND (@localtm < dateadd(hh,3,@lastoctobersunday)) BEGIN SET @localwintertm = dateadd(hh,-1, @localtm) END ELSE BEGIN SET @localwintertm = @localtm END RETURN @localwintertm END |
如果可以的话,使用PHP ...
1 2 3 4 5 6 7 8 9 10 | $dateFormat = 'r'; $tz = 'Australia/Perth'; FUNCTION convertDate($epochDate) { global $dateFormat, $tz; $tzOld = getenv("TZ"); putenv("TZ=$tz"); $ret = DATE($dateFormat, ($epochDate*1)); putenv("TZ" . ($tzOld ?"=$tzOld" :"")); RETURN $ret; } |