zoukankan      html  css  js  c++  java
  • Never use GetDate() when comparing date timesoffsets, use SYSDATETIMEOFFSET()

    -- my current uk time is 2014-01-09 10:43:00 +0
    DECLARE @userLocalTimeChina datetimeoffset(4) = '2014-01-09 18:43:00 +08:00';
    DECLARE @userLocalTimeLasVagas datetimeoffset(4) = '2014-01-09 02:43:00 -08:00';
    DECLARE @userUTCTime datetimeoffset(4) = '2014-01-09 10:43:00 +00:00';
    Declare @systemDateTimeOffsetWhereServerInUK datetimeoffset(4) = SYSDATETIMEOFFSET();
    Declare @systemDateTimeWhereServerInUK datetime = GETDATE();
    print @systemDateTimeOffsetWhereServerInUK
    print @systemDateTimeWhereServerInUK
    -- for demo, it gives this
    SET @systemDateTimeOffsetWhereServerInUK = '2014-01-09 10:43:00 +00:00';
    SET @systemDateTimeWhereServerInUK = '2014-01-09 10:43:00'
    Declare @systemDateTimeOffsetWhereServerInNY datetimeoffset(4) = '2014-01-09 05:43:00 -05:00';
    Declare @systemDateTimeWhereServerInNY datetime = '2014-01-09 05:43:00';
    -- ALL THESE TIMES should be the same.
    --Using HOURS here to show the difference, but if we usually use DAYS, this simply means people can 'see' things or 'where clauses' go wrong at certain hours of the day
    print 'Compare user LOCAL time to server offset and not offset date time, in uk then new york'
    print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeOffsetWhereServerInUK); -- Correct
    print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeWhereServerInUK); -- The result is correct, but only because server is in UK and no daylight savings time, so will be INCORRECT and CORRECT depending on time of year!!!
    print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeOffsetWhereServerInNY); -- CORRECT
    print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeWhereServerInNY); -- INCORRECT RESULT
    print 'Compare user UTC time to server offset and not offset date time, in uk then new york'
    -- compare Users UTC time offset with system offset produces CORRECT result most of the time, in the uk
    print DATEDIFF(HH, @userUTCTime, @systemDateTimeOffsetWhereServerInUK); -- CORRECT
    print DATEDIFF(HH, @userUTCTime, @systemDateTimeWhereServerInUK); -- The result is correct, but only because server is in UK and no daylight savings time, so will be INCORRECT and CORRECT depending on time of year!!!
    print DATEDIFF(HH, @userUTCTime, @systemDateTimeOffsetWhereServerInNY); -- CORRECT
    print DATEDIFF(HH, @userUTCTime, @systemDateTimeWhereServerInNY); -- INCORRECT RESULT
    --CONCLUSION: ALWAYS COMPARE OFFSET TO OFFSET. OR UTCDATETIME TO UTCDATETIME. otherwise it's totaly dependant on the deploy location time zone if the solution will work.
  • 相关阅读:
    Android Activity中获取当前焦点的控件,自动化输入EditText
    Java Android 二进制文件读写
    Delphi 动态数组、静态数组、TBytes 的区别
    IIS日志分析工具-Log Parser
    信息安全等级保护三级系统基线要求判分标准之应用安全
    通过TCPView工具查看foxmail用exchange方式连接exchange时用什么端口
    Windows2008R2操作系统日志清理
    批量IP自动netcat脚本
    批量IP自动ping脚本
    批量移动AD用户到指定OU
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/3514937.html
Copyright © 2011-2022 走看看