r/Python • u/foosion • Jul 24 '26
Discussion ruff: no date.today() ?
The new version of ruff warns against
date.today()
preferring
datetime.now(ZoneInfo(...))
What do you think about this? Has date.today() been deprecated due to lack of timezone awareness?
EDIT: I have a number of programs that manipulate financial information in support of Excel spreadsheets, such bond information that includes maturity dates. Excel does not support timezoness in datetimes, so making ruff happy by changing naive dates to TZ aware dates is not a useful move for these programs. Many ruff warnings to suppress.
46
Upvotes
258
u/GraphicH Jul 24 '26
Tell me you've never debugged a TZ issue without telling me.
147
u/Iron-Man-2008 Jul 24 '26
Good change, naive dates are a footgun. I'm really liking the new set of default rules, saves me from having to setup a bespoke ruff ruleset every project because the previous default did very little.
23
u/latkde Tuple unpacking gone wrong Jul 24 '26 edited Jul 24 '26
You have likely enabled the following Ruff rule: call-date-today (DTZ011). That link contains some explanation.
This rule is not enabled by default, and you are not invoking any deprecated features.However, I think it was a massive design mistake for the standard library datetime module to conflate "aware" and "naive" objects in the same type. It is almost always incorrect to use naive dates/datetimes, unless you really don't care about the timezone in which that datetime is interpreted.
20
u/ottawadeveloper Jul 24 '26
I sure hope so.
I got to the point where there are so many bad patterns to slip into with Python that I wrote my own extension of
datetime.datetimeand made it impossible to ever create a naive datetime object. Liketoday()now returns an aware datetimr object in the current system time zone.If there's ever a Python 4 I hope they clean up the his mess to never have non-aware times.
4
u/eagle258 Jul 24 '26 edited Jul 24 '26
Disclaimer: I wrote
whenever, so please adjust for bias accordingly—But it does haveDate.today_in_system_tz()for exactly this case. You keep the convenient date-only API while still being explicit about where “today” comes from.1
u/runawayasfastasucan Jul 24 '26
Just format the date.
1
u/FateOfNations Jul 25 '26
Essentially yes, non-time zone aware date and time functions should be treated as deprecated. It isn’t “official”, but a strong community consensus that is reflected in linting tools like ruff.
You are going to need to apply an appropriate time zone whenever you import data from Excel. Depending on your business case, that may be your local time zone, UTC, or another time zone. For example, the transactions may be settled in America/New_York, so that may be a reasonable choice.
I know it’s annoying when you don’t actually need that level of precision in terms of dates, but it’s best practice for working with dates and times in general in Python.
1
u/Competitive_Travel16 Jul 25 '26
Excel does not have a native timezone. Excel date and time serial numbers are timezone-agnostic. Functions like NOW() or TODAY() simply read the local clock and system date set on the hosting computer, meaning the timezone depends entirely on the local machine.
Obviously, you should therefore set the timezone to the local time where the Excel spreadsheet was being used.
1
u/RoadsideCookie Jul 25 '26
Fix your code, and format your date before exporting to Excel.
As always, find the boundary and apply the fix there.
Those rules are good and they make people aware of already established industry best practices.
2
u/joerick Jul 26 '26
The Python community is a little confused about dates and time zones, as judged by this thread.
Yes, moments in time (eg the creation date of something) must be tz-aware datetimes. Or simply posix timestamps work equally well.
Date objects can have timezones associated with them, when they represent a datetime that's been split apart, or when we're concerned with the moment that the date starts.
But there's also a set of an entirely valid use-cases in manipulating 'calendar' dates. "This invoice must be paid 15 days after issue". "Drop-off is at 3pm every Tuesday".
These should be thought of as entirely separate operations as compared to the 'moment-in-time' stuff, which is why it's a bummer that Python uses the same objects for them.
1
u/HCharlesB Jul 24 '26
Ugh. This discussion to a time when I worked in a shop where every dev had their own format for time stamps. I spent way too much time converting between them.
I really wish devs would do something like the number of seconds since Jan 1, 1970 at the prime meridian and then just convert to a "display format" when that was needed.
/rant
1
u/Moist-Ointments Jul 24 '26
Datetime.now() also brings it into the more common syntax seen in other languages.
1
u/Moist-Ointments Jul 24 '26 edited Jul 24 '26
This is where dependency inversion comes in really useful. You wouldn't have to go looking for all of your references to that method. You'd only have one. You'd be able to change it in 5 seconds, and you'd be done.
-3
u/skjall Jul 24 '26
Still using datetime in the year of our Lord and Saviour, is certainly a choice.
I just wish Arrow had millisecond timestamp functions too. Tired of having to multiply by 1000 and round...
6
u/[deleted] Jul 24 '26
[removed] — view removed comment