将历史日期从 UTC 转换为本地时间

发布于 2024-09-01 22:15:55 字数 465 浏览 11 评论 0原文

我有一个包含如下数据的 sql 表:

| 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 偏移量全年并不相同。

更新:我使用的是 Microsoft SQL Server 2005,我需要从 SQL 执行此操作,因为数据稍后将显示在报表中。

I have an sql table with data like this:

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

The dates are stored in UTC format in a datetime column, how can I convert them to local time (Norway)? Remember that the UTC-offset is not the same all year because of winter/summer-time.

UPDATE: I am using Microsoft SQL Server 2005, and I need to do this from SQL because the data will be shown to a report later on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人疚 2024-09-08 22:15:55

该语句将针对您列出的表起作用。使用该逻辑,您可以将其应用于任何日期字段。

    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] 

This statement will work against the table you listed. Using the logic you can apply it to any date fields.

    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] 
许久 2024-09-08 22:15:55

编写 CLR 函数,将 SqlDateTime 从 UTC 转换为 CLR 本地时间,如下所示:

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime ToLocalTime(SqlDateTime dt)
{
    try
    {
         return TimeZone.CurrentTimeZone.ToLocalTime(dt.Value);
    }
    catch
    {
        return SqlDateTime.Null;
    }
}

Write you CLR function that will convert your SqlDateTime from UTC to your CLR local, something like this:

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime ToLocalTime(SqlDateTime dt)
{
    try
    {
         return TimeZone.CurrentTimeZone.ToLocalTime(dt.Value);
    }
    catch
    {
        return SqlDateTime.Null;
    }
}
苏大泽ㄣ 2024-09-08 22:15:55

如果可以的话,使用 PHP...

$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;
}

Using PHP if you can...

$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;
}
萌酱 2024-09-08 22:15:55

从当地时间返回当地冬季时间

这是我的函数,根据 3 月最后一个星期日 2:00 和 10 月最后一个星期日 3:00 的时间变化

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

here is my function to return local winter time from local time

based on time change on last Sunday in March at 2:00 and last Sunday in October at 3:00

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文