r/SQLServer • • 22d ago

Discussion Is this overkill for storing current universal time when INSERTing or UPDATEing a row?

Below are the columns I would be using for all my tables so that each INSERT and UPDATE against a row gives me timezone independent information about when the operation occurred. Is this too much? Is there anything I can simplify to save on space? The goal is complete time information since this is for a database that could potentially be filled by global users in wildly different timezones.

CreateDateSysteDateTimeOffset datetimeoffset(7) not null default sysdatetimeoffset()

CreateDateUTC datetime2(7) not null default sysutcdatetime()

CreateDateTimezone nvarchar(100) not null default current_timezone_id()

UpdateDateSysteDateTimeOffset datetimeoffset(7) not null default sysdatetimeoffset()

UpdateDateUTC datetime2(7) not null default sysutcdatetime()

UpdateDateTimezone nvarchar(100) not null default current_timezone_id()

0 Upvotes

15 comments sorted by

12

u/TheCumCopter 21d ago

Store in UTC, read in local Timezone. This will save you lots of headaches

0

u/[deleted] 21d ago

[deleted] — view deleted comment

1

u/dbrownems ‪ ‪Microsoft Employee ‪ 20d ago

This. datetimeoffset doesn't store the timezone, only the offset, so you don't have enough information to do anything useful.

7

u/mandragongamer 21d ago

Best practice would dictate you store all your times in one timezone, in this case UTC. In your example I would recommend CreateDate and UpdateDate. Then for the purposes of the user viewing those fields, you could either convert the date time from UTC to a set time zone (stored in profile) or get the timezone from the computer and convert it based on the computers timezone.

1

u/alinroc 4 21d ago

Why store both datetimeoffset and the UTC date + timezone? datetimeoffset covers the whole thing.

1

u/Reasonable-Job4205 20d ago

Makes sense. How do I convert datetimeoffset into local time for each user's timezone?

1

u/alinroc 4 20d ago

Let it happen on the client end. For example, in JavaScript:

const theDate = new Date(Date.parse('2026-11-11T22:26:28.1402070-05:00'));
console.log("Local date Time: " + theDate.toLocaleString());

1

u/Lost_Term_8080 21d ago

I would just store in UTC. I used to be a big fan of including offset, but have had enough situations now where the client sent a bad offset that was so thoroughly confusing I think its probably better to be forced to do the work to figure out what the offset was rather than be given bad offset landmines to have to contend with. The local time can always be reconstructed from UTC, but an inconsistent bad offset can really mess up time calculations and ultimately the local time only matters contextually

2

u/wasabiiii 21d ago

Local time can only be certainly reconstructed if you have a tzdb and geo location. Worth knowing. And usually not that important. But still true.

1

u/Administrative_Bug63 21d ago

The most perfect thing you want is data that is consistent globally so you can query it. The easier it is to query, the easier it is to do reporting and use it for application decision support. Everyone says UTC, and that may well be true, but, you have to keep in mind that most business rules involving time are in local time.

The correct answer is, you need both. You have your main table in UTC. You have a buddy table that can map a time to dateparts in respective time zones so you can index them. You can put this buddy table entirely in memory if you need to.

Something like this.

Table A - DataSourceID - FK int

DataRecordedUTC - UTC DateTime

DataRecordedId - ID for DateTime

Reading Amount(s) - double

Table B - DateRecordedID - ID PK

TimeZone

UTC Time,

Year, Month, Day, Hour, Minute

This would be the mininum you'd use, for example, if you were capturing utility meter readings across time zones, because, the time zone matters for billing.

So you can

select max(ReadingAmount) * DemandCharge, sum(ReadingAmount) * UsageCharge

from TableA A join TableB B on A.DateRecordedId = B.DateRecordedId

where B.Month Between 5 and 9 and hour between 6 and 9

group by Year, Month, Day

To get you a daily electric bill for summer in a state that has a summer day rate from June to October.

1

u/davidbrit2 21d ago

Store UTC only. If you need other time zones, you can add them as calculated columns (persisted if you need to index/search them) with the simple AT TIME ZONE syntax, e.g.

CreateDatePT AS (CreateDateUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time')

Or simply do the conversion in your queries/views/etc.

1

u/Better-Credit6701 21d ago

How about epoch time stored as an integer with time zone in a different column? Also depends on the database. Yeah, I had to work with epoch, it sucks to convert it to 'human' time

1

u/user0987234 21d ago

Include the offset. With changes to time zones in Canada, several new time zones were created. Those new time zones haven’t shown up in some SQL applications. Placeholders are currently used. Problem is, historical data will use the new timezone which has a different UTC offset than the previous one. The offset column is needed to show the correct local time for a past event.