4 ms·
You should store dates with timezone or you can't make calculations like how old is this post. You could base it off system time, but then the calculation have
by z3t4 6y ago
You should store dates with timezone or you can't make calculations like how old is this post. You could base it off system time, but then the calculation have to take into account summer/winter time.
- Someone1234 6y ago> You should store dates with timezone or you can't make calculations like how old is this post. I think you misread the post you responded to. It is discussing the benefit of Date Vs. DateTime data types. In Postgres, by design, a date has a 1 day resolution (i.e. no concept of hours/minutes, within that date). This is hugely useful for scenarios where you intentionally want and only want a 1 day resolution. It not being timezone adjustable is a feature, not a bug. Storing a DateTime with 12:00 and then an offset of e.g. 0, is a needlessly complicated work-around when a data type that doesn't even have the concept of hours or timezones exists.
- brianwawok 6y agoEverything in the DB should be UTC. Anything else is crazy.
- growse 6y agoThis isn't true. For example: If a user schedules an event to happen at 15:35 on July 29th 2021 IST, you cannot know with certainty what the equivalent UTC value is. You know what it would be assuming the relationship between IST and UTC stays the same between now and next year. If India suddenly decides to implement some sort of DST, or make any other changes to the definition to IST, the UTC value you stored is now wrong. Timezones are political, but they're also how people organise their lives. The user's local timezone is context that you can't just discard.
- deleted 6y ago[deleted]
- int_19h 6y ago> If India suddenly decides to implement some sort of DST, or make any other changes to the definition to IST, the UTC value you stored is now wrong. It is not wrong. When timezones change, the database that contains that info is updated - but it still retains information about historical rules, precisely so that earlier dates can still be converted reliably. On the other hand, if you use local times, then you are not able to distinguish between pre- and post-clock change, when the same time repeats twice.
- tchaffee 6y agoHow are you going to know to look for changes to IST when all you have is a UTC date?
- deleted 6y ago[deleted]
- growse 6y agoYou're not converting a historic date though, you're converting a future date. And that's an inherently uncertain process. It is literally impossible to know what UTC time 2021-07-29T15:35:00 IST refers to until it happens. The Indian government is free to redefine IST as they wish, and could choose to do that at 15:34:59.
- tchaffee 6y agoSome parts of Brasil decided to not observe daylight savings this year. I had to file a bug with Chrome which was converting dates in JS off by one hour for some Brazilian cities. Firefox got it right. If a couple of years ago you had stored my future appointment in your db using UTC, your db would now have the wrong time for my Brasil appointment.
- erikpukinskis 6y agoTimes, perhaps. But dates should never be stored with a timezone at all, except in extremely rare cases where a day for some reason specific to a timezone. But even then, it's much more likely that you are trying to specify a time range that happens to start and end on a certain day in a certain place. Dates, almost by definition, are independent of timezone. January 1st, 2020 began at several different times in different timezones. It should be represented as a date stamp: 2020-01-01 without any timezone. If you really are trying to represent the day that starts at 12:00 AM PST on January 1, 2020 and ends at 12:00 PM PST the same day, then you probably just want two timestamps representing those instants and not a date at all. But again, that would be quite rare. In most cases you would just want to store 2020-01-01.
- raphaelj 6y agoSome times it does not make sense to associate a time component to a date, just because it happens on a day, not at a certain time. For example, in a lot of countries, Labor Day is on May 1, Christmas on Dec 25. These do not happen at a certain time, and there is no timezone to be associated with these events. If I'm requesting some user's birthday, I'm going to store it as a date. I don't have a time component. I don't know the time zone. In some cases, dates are just dates ...
- z3t4 6y agoFor those born around new year the hour is important as it will decide what year they are born. It will also be important for birthdays, You don't want to send happy birthday on the wrong day - which can be different depending on what timezone they are in.