Use local timezone for filename dates #3
Loading…
Reference in New Issue
No description provided.
Delete Branch "makeworld/gemfeed:master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes #1
Source for converting to local timezone: https://stackoverflow.com/a/13287083/7361270
This solution only supports Python 3.3+. Please let me know if that's a problem.
Also, I'd appreciate if a new version of gemfeed could be released if this gets merged.
Thanks for this. I was just looking at the
datetime
docs to make sure I understood and agreed with what this code did, and now I'm a little confused. I think the call toreplace
is redundant, but the bigger problem is thatastimezone
doesn't seem to work like it should for me?That's weird, right? It's clearly aware that I'm in CET and not UTC, and adds the
tzinfo
accordingly, but it doesn't adjust the hour value accordingly, even though it is supposed to take care of "adjusting the date and time data so the result is the same UTC time as self, but in tz’s local time". If it worked properly, there should be no difference between the following - the first is just the current time in UTC, and the latter converts from that to local time and then back to UTC - but I get different times:Is this working properly on your end?
After some experimentation of my own, I've determined that the
replace
is not redundant particularly for situations like the one you've presented. It turns out thatdatetime.utcnow()
is a bad function to use in general, because it doesn't set the timezone to UTC, it just leaves it asNone
:As the documentation for this function notes:
Using
datetime.now(timezone.utc)
as the docs recommend works as expected:And when you use
.replace(tzinfo=timezone.utc)
as my code does, even usingdatetime.utcnow()
works fine, because the timezone is set to UTC explicitly first.This was all in Python 3.8.6, but should apply to Python 3.3+ in general.
You can also test the code overall by creating a dummy file named
2020-12-06-test.gmi
or something, and when you look at the feed gemfeed creates, you'll see the timestamps have your local timezone in them.Ah, thanks a lot for clarifying! Shame on
utcnow
! However, the use ofreplace
in your commit is still redundant, I think, because the initialdatetime
object is created by callingstrptime
on a string which ends in "Z", so it gets a proper UTC timezone assigned. Am I right in thinking that if we removed the "Z" suffix fromdate
thenreturn datetime.datetime.strptime(date, "%Y-%m-%d").astimezone()
would be sufficient for the desired behaviour?No criticism intended in any of this, by the way! I appreciate you helping out with this, I'm just trying to keep things short and clear, and chaining three different methods together in a single long line seems less than ideal.
That's correct.
No, I'm not sure if you miswrote something. As you mentioned earlier, by calling
strptime
on a string that ends in "Z", a proper UTC timezone is assigned. If you don't have the timezone, as you mention here, then timezone would would default toNone
, andastimezone()
will change the zone but not actually adjust the datetime values.This basically leaves two options for changing the code. Either use the "Z" and theline becomes
datetime.datetime.strptime(date, "%Y-%m-%d %z").astimezone(tz=None)
, or leave off the "Z" and the line isdatetime.datetime.strptime(date, "%Y-%m-%d").replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)
.Which one do you prefer?
@solderpunk bump :)
I believe I've answered your most recent comment, and now I have a question about which version of the line you like best, as you can see above. Once you let me know I'll update the file, and this PR will be ready to merge. Of course, it works fine the way it is now as well.
Step 1:
From your project repository, check out a new branch and test the changes.Step 2:
Merge the changes and update on Gitea.