Search code examples
pythondatetimedstdatetimeoffsetftputil

Time of last file modifcation returns a one year offset since DST change


To get the modification time of a file I use path.getmtime from ftputil, but since the DST-change the returnvalue of recently created files has an offset of one year, saying the last modification time of new files is from last year. Actually the file was modified one hour in the "future".
The reason for this is the OS, where the file is stored, is running on DST, but where the script is running its non-DST.
Overall I want do delete files older than a threshold, but right now it's also deleting the newest file, because it says it's from 2018.

I tried converting the timestamp with datetime.utcfromtimestamp into UTC time but obviously getmtime does not return the actual timestamp of the file. It already sets it to 2018, last year, cause it may look like the value is from the future.

Lets say its 2019-04-04 10:00:00 UTC. A file on a host running on DST (UTC+1) is created at 10:45

import ftputil
import datetime
import time

host = ftputil.FTPHost(ftp, user, pwd)

modtime = host.path.getmtime(file)
print datetime.datetime.utcfromtimestamp(modtime)

Output should be 2019-04-04 09:45:00, but actually it's 2018-04-04 09:45:00. I can do whatever I want with datetime formatting, as long as getmtime passes a 2018-timestamp, it never gets right.

Is there a way to pass the right timestamp with or without getmtime? Honestly I don't want to manipulate it manually. Or what does change it to a one-year offset?

#firsttimeposter


Solution

  • If you have write access in a directory on the FTP server, you can call FTPHost.synchronize_times(). This will temporarily create a file on the server, ftputil will parse the file's timestamp, delete the file and then adjust the time offset between server and client for this FTPHost instance.

    If you don't have write access on the server, you can still set the offset "manually" with FTPHost.set_time_shift(time_shift), where time_shift is defined as "server_time - client_time in seconds".

    See https://ftputil.sschwarzer.net/documentation , section "Time zone correction".