热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLServer时间同步问题:精准实现UTC与本地时间的双向转换

usr..7我单独使用T-SQL无法找到任何方法.我用SQLCLR解决了它:publicstaticclassDateTimeFunctions{[SqlFunction(IsDe

usr..

7

我单独使用T-SQL无法找到任何方法.我用SQL CLR解决了它:

public static class DateTimeFunctions

{

[SqlFunction(IsDeterministic = true, IsPrecise = true)]

public static DateTime? ToLocalTime(DateTime? dateTime)

{

if (dateTime == null) return null;

return dateTime.Value.ToLocalTime();

}

[SqlFunction(IsDeterministic = true, IsPrecise = true)]

public static DateTime? ToUniversalTime(DateTime? dateTime)

{

if (dateTime == null) return null;

return dateTime.Value.ToUniversalTime();

}

}

以下注册脚本:

CREATE FUNCTION ToLocalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToLocalTime;

GO

CREATE FUNCTION ToUniversalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToUniversalTime;

被迫转换到UTC时间的努力是一种耻辱.

请注意,这些函数将本地时间解释为服务器的本地时间.建议将客户端和服务器设置为相同的时区,以避免混淆.



推荐阅读
author-avatar
mongcheng
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有