I am trying to get a file's last modified time using Python 3.8 in Cygwin.
So if I do stat .profile I get:
File: .profile
Size: 1236 Blocks: 4 IO Block: 65536 regular file
Device: 46e61a95h/1189485205d Inode: 8162774324632653 Links: 1
Access: (0755/-rwxr-xr-x) Uid: (197609/ pepol) Gid: (197609/ pepol)
Access: 2020-09-14 15:16:04.773101900 +0700
Modify: 2020-09-14 15:15:21.977809000 +0700
Change: 2020-09-14 15:16:04.055602500 +0700
Birth: 2020-09-14 15:16:04.052652900 +0700
But if I try getting the file's timestamp using Python:
from pathlib import Path
from datetime import datetime
p1 = Path(".profile")
p1st = p1.stat()
dts = datetime.fromtimestamp(p1st.st_mtime)
print(str(dts))
I got this 'naive' (timezoneless) instead:
2020-09-14 09:15:21.977809
Now here's where I get confused:
stat output, my timezone is UTC+07:0015:15:21.977809000 +0700 is equivalent to 08:15:21.977809000 +0000Why is the timestamp as fetched by pathlib.Path().stat() is 1 hour ahead of what the UTC timestamp should be? What timezone is it actually using?
Make sure that in cygwin, you use Cygwin's Python. You can check which Python version cygwin uses with $which python3. That should return e.g. /usr/bin/python3.
Side note, since the timestamps returned by pathlib.Path().stat() are POSIX timestamps, so you could use e.g. datetime.fromtimestamp(p1st.st_mtime, tz=timezone.utc) to get UTC straight away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With