Storing DateTime (UTC) vs. storing DateTimeOffset

前端 未结 3 505
挽巷
挽巷 2020-12-04 06:09

I usually have an \"interceptor\" that right before reading/writing from/to the database does DateTime conversion (from UTC to local time, and from local time to UTC), so I

3条回答
  •  误落风尘
    2020-12-04 06:35

    A DATETIMEOFFSET gives you the ability to store local time and UTC time in one field.

    This allows for very simple and efficient reporting in local or UTC time without the need to process the data for display in any way.

    These are the two most common requirements - local time for local reports and UTC time for group reports.

    The local time is stored in the DATETIME portion of the DATETIMEOFFSET and the OFFSET from UTC is stored in the OFFSET portion, thus conversion is simple and, since it requires no knowledge of the timezone the data came from, can all be done at database level.

    If you don't require times down to milliseconds, e.g. just to minutes or seconds, you can use DATETIMEOFFSET(0). The DATETIMEOFFSET field will then only require 8 bytes of storage - the same as a DATETIME.

    Using a DATETIMEOFFSET rather than a UTC DATETIME therefore gives more flexibility, efficiency and simplicity for reporting.

提交回复
热议问题