4 ms·
I know I'm late to the party, but I have a Python script that creates a security.txt for one of my own project, and it sets the "Expires" date to one year in th
by hannob 3y ago
I know I'm late to the party, but I have a Python script that creates a security.txt for one of my own project, and it sets the "Expires" date to one year in the future.
Old code:
expires = datetime.datetime.now(tz)
expires = expires.replace(year=expires.year + 1)
It broke yesterday, throws an exception ("ValueError: day is out of range for month"). It's kinda obvious that it does.
Fixed version code:
expires = datetime.datetime.now(tz) + datetime.timedelta(days=365)
expires = expires.isoformat(timespec="seconds")
Now we're just going 365 days into the future. Of course, this has a slightly different meaning and outcome, we are not always ending up on the "same date next year". But in this use case it doesn't really matter.
- ryanjshaw 3y agoIn .NET there is an AddYears function that deals with all of this, and even gives you Feb 29 if the date is valid, otherwise Feb 28: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addyears?view=net-8.0 https://learn.microsoft.com/en-us/dotnet/api/system.datetime...
- willsmith72 3y agoThere's the same in python, but if you wanna write shitty code you can always get around it
- linehedonist 3y agoYup relativedelta https://dateutil.readthedocs.io/en/stable/relativedelta.html https://dateutil.readthedocs.io/en/stable/relativedelta.html
- wwilim 3y agoIs it just me or does .NET have the most sane standard library for dealing with dates? Until you get into timezones that is