News for hackers. Stuff that matters.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by ahoka on Sunday November 19, 2023 @04:29PM (#38338291)
Then accept that UTC is a timezone and just use that everywhere. There are also a lot of things that can’t be easily stored in UTC, like opening hours.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by narrator on Sunday November 19, 2023 @04:35PM (#38338373)
Yeah, the stock market opens at what time EST? Including daylight saving time, etc. I agree that any time time goes to a serialized format over a socket or to a database or file it should always be UTC.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by briHass on Sunday November 19, 2023 @06:20PM (#38339684)
Ignoring holidays/weekends: always 9:30AM or (0930) EST. Same with EDT: 9:30AM.

The key here is that the timezone info includes the daylight/standard designation. Or, in the ISO format -4:00 or -5:00 from UTC.

I think you meant to put ET (Eastern Time), which is still 9:30AM, but without a date associated with that time, there's no way to convert that to UTC or other timezones that don't have the same daylight saving time schedule.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by tsimionescu on Sunday November 19, 2023 @06:20PM (#38339691)
UTC (or Unix timestamp, or any other non-localized date-time format) doesn't work for future localized events. It's fundamentally impossible to say what will be the UTC timestamp which corresponds to 9AM in some particular location in the future: the timezone of that location could arbitrarily change between now and then. Countries and smaller areas often experiment with such changes, wars happen and conquerors/liberators change the time etc.

So, when possible, it's best to store (future) human events in a localized datetime string. For things like physical events, the opposite is true of course - you can't say what will be the local date and time when the Sun will hit high noon over Athens 10 years from now, but you can certainly calculate the UTC timestamp for that.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by icholy on Sunday November 19, 2023 @04:33PM (#38338352)
Why can't opening hours be stored in UTC?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by RichieAHB on Sunday November 19, 2023 @04:39PM (#38338443)
A store’s opening hours will probably remain 9am-5pm regardless of any time zone changes around it. If you run a bunch of stores across time zones you need to know where each store is and whether it’s summer or winter to work backwards to find out 9-5 in UTC.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by jomoho on Sunday November 19, 2023 @04:37PM (#38338403)
Every heard of daylight savings time (DST)?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by capableweb on Sunday November 19, 2023 @04:39PM (#38338427)
Can be adjust client-side, or wherever you need to present it, rather than adjusting stored data.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @04:47PM (#38338542)
This is only sometimes true. Cases like opening hours and daily batch processing rely on local time. Persisting the values in UTC mean best case you are wrong half of the year and worst case all of it.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by capableweb on Sunday November 19, 2023 @05:17PM (#38338939)
Just because you're storing the time in UTC doesnt mean you can't also store the timezone, and lots of other related data too.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @05:48PM (#38339322)
6am in my time zone is two different times in UTC. Right now it is 14:00 UTC. But when DST starts it will be 13:00. So what UTC value do you insert into your database for the opening time of the coffee shop? And how do you convert?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by pjot on Sunday November 19, 2023 @04:37PM (#38338400)
Seems like the ideal way to store them to me!
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @04:48PM (#38338557)
Have you thought about this? How do you store shop hours in UTC for shops in timezones with DST?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by midasuni on Sunday November 19, 2023 @05:44PM (#38339282)
Then you would be wrong, because they open at 9am local time - if the government changes local time with 2 days notice then opening time changes.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @04:42PM (#38338481)
The coffee shop in my neighborhood is open from 6am to 2pm Pacific. We observe daylight saving time. The shop hours do not change. So, what UTC value do you store?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by bilsbie on Sunday November 19, 2023 @05:10PM (#38338852)
I guess it would change based on local time changes.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by rtpg on Sunday November 19, 2023 @05:18PM (#38338951)
Something like “9AM-5PM EST” can’t be stored in a datetime object even with a timezone slot! Datetimes with a timezone represent specific points in time, not vague “5PM” like concepts.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by MrJohz on Monday November 20, 2023 @09:56AM (#38349154)
I feel like every time a discussion on timezones comes up, people compete to come up with different situations where "see, your perfect system can't work, we should give up on timezones entirely".

Here, I'm not even sure what your point is. A datetime object cannot capture a range, no, just like a number can't capture a range of values. But through the magic of having two of them, we can get easily create a data structure containing two datetimes to represent a range. 5pm is not a vague concept, it is the time 5pm, the hour that typically comes after 4pm and before 6pm. If your point is that you can't store only a time in a datetime object, that's true, that's why the standard library also provides the `time` object which represents a time.

Where things might get a bit more complex is if you want to store the time of a recurring event that should occur at the same time on multiple days for a given timezone. In this situation, you can typically use a naive time object to represent a wall clock time, plus the timezone that the user has requested. This way you still have all the raw information to decide when the event will happen in the future. (Note however, that the same time can occur multiple times on the same day, for example during daylight savings changes.)

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by rtpg on Tuesday November 21, 2023 @08:37AM (#38363413)
"every day, from 9AM to 5PM EST" is a thing that can be represented through calendar RRULEs. Usually libraries exist to help with this (Python has one, conveniently called rrule!). This is a thing to describe recurring events, has a spec and everything. So there is a thing.

And you're totally right about `time` existing, it slipped my mind that time has a tzinfo slot. I do think you should still take care and I would avoid having any naive time objects just floating around beyond the serialization layer though.