I want to use UTC timestamps exclusively in an MSSQL database. When writing the current timestamp I can use system methods like GETDATEUTC
and we have agreed with suppliers that timestamps sent from external sources will also be provided as implicit UTC - but when writing these to the DB do I need to tell MSSQL that it is a UTC date somehow? Or is it my responsibility to know what timezone I'm using?
Does DATETIME type include timezone information?
Asked Answered
To provide an answer, in short Neither Datetime
nor Datetime2
encodes timezone information, only the raw date/time data specified. It is up to the developers/DBAs/users to agree what the data means.
"It is up to the developers/DBAs/users to agree what the data means." note that a
DATETIME
is independent of a time zone, so it's not like they're agreeing on a time zone. It is simply an abstract representation of instant in time; that instant represents different times/dates in different time zones, but it is the same instant in time in all of them, and thus the same DATETIME
. You can convert it to a specific time zone using AT TIME ZONE
. –
Jacobs No, a DATETIME
is independent of any time zone.
It is simply an abstract representation of instant in time; that instant represents different times/dates in different time zones, but it is the same instant in time in all of them, and thus the same DATETIME
.
You can convert it to a specific time zone using AT TIME ZONE
. That will give you a datetimeoffset
representing the time and date in a particular time zone that corresponds to that instant in time.
It is worth noting that AT_TIME_ZONE isn't deterministic because it relies on timezone info provided by the OS. I still feel that agreeing on storing only UTC based datetimes is preferred and then let the application layer work out what timezone that UTC datetime needs to be displayed in. –
Detestation
© 2022 - 2024 — McMap. All rights reserved.
datetimeoffset
. – Vampiredatetimeoffset
is not DST-aware. But if you trust the source, no, you don't have to tell SQL Server it's UTC. – Lilydatetimeoffset
. Don't usedatetime
, that's an obsolete legacy type. Usedatetime2
instead. Neitherdatetime
nordatetime2
holds any kind of timezone information though – Continualgetdate()
slipping past code reviews. – Lilydatetimeoffset
if you want roundtripping. Also if you care about having the correct type. Storing UTC isn't enough - timezone offsets change quite frequently – ContinualEurope/London
and convert as needed. Airlines publish schedules using both offsets and IANA tz names but while offsets can change, the TZ name will never change. Unfortunately, SQL Server doesn't support this - yet. TheAT TIME ZONE
clause only uses Windows timezone names which are ... non-standard. – ContinualGETUTCDATE
but the webserver uses the local time - that's a farce to dig through – Stefanysteffanedatetimeoffset
or UTC values. By storing the local time and airport/tz name, I didn't have to change anything – Continualfrom UTC to {some timezone that may or may not support DST}
than it is to changefrom {some timezone that may or may not support DST} to {some other timezone that may or may not support DST}
. – LilyI want to use UTC timestamps exclusively...
so that further reinforces why I lean toward UTC.) – Lily