SQL Server - Convert date field to UTC

前端 未结 12 684
故里飘歌
故里飘歌 2020-12-23 13:39

I have recently updated my system to record date/times as UTC as previously they were storing as local time.

I now need to convert all the local stored date/times to

12条回答
  •  無奈伤痛
    2020-12-23 13:59

    Unless I missed something above (possible), all of the methods above are flawed in that they don't take the overlap when switching from daylight savings (say EDT) to standard time (say EST) into account. A (very verbose) example:

    [1] EDT 2016-11-06 00:59 - UTC 2016-11-06 04:59
    [2] EDT 2016-11-06 01:00 - UTC 2016-11-06 05:00
    [3] EDT 2016-11-06 01:30 - UTC 2016-11-06 05:30
    [4] EDT 2016-11-06 01:59 - UTC 2016-11-06 05:59
    [5] EST 2016-11-06 01:00 - UTC 2016-11-06 06:00
    [6] EST 2016-11-06 01:30 - UTC 2016-11-06 06:30
    [7] EST 2016-11-06 01:59 - UTC 2016-11-06 06:59
    [8] EST 2016-11-06 02:00 - UTC 2016-11-06 07:00
    

    Simple hour offsets based on date and time won't cut it. If you don't know if the local time was recorded in EDT or EST between 01:00 and 01:59, you won't have a clue! Let's use 01:30 for example: if you find later times in the range 01:31 through 01:59 BEFORE it, you won't know if the 01:30 you're looking at is [3 or [6. In this case, you can get the correct UTC time with a bit of coding be looking at previous entries (not fun in SQL), and this is the BEST case...

    Say you have the following local times recorded, and didn't dedicate a bit to indicate EDT or EST:

                         UTC time         UTC time         UTC time
                         if [2] and [3]   if [2] and [3]   if [2] before
    local time           before switch    after switch     and [3] after
    [1] 2016-11-06 00:43     04:43         04:43           04:43
    [2] 2016-11-06 01:15     05:15         06:15           05:15
    [3] 2016-11-06 01:45     05:45         06:45           06:45
    [4] 2016-11-06 03:25     07:25         07:25           07:25
    

    Times [2] and [3] may be in the 5 AM timeframe, the 6 AM timeframe, or one in the 5 AM and the other in the 6 AM timeframe . . . In other words: you are hosed, and must throw out all readings between 01:00:00 and 01:59:59. In this circumstance, there is absolutely no way to resolve the actual UTC time!

提交回复
热议问题