~sschwarzer/ftputil

ab3b197facdafabcdb39be99634a302a5588bdcd — Stefan Schwarzer 2 years ago a7ab48c
Change `utc_local_time_shift` to `expected_time_shift`

Replace the function `utc_local_time_shift` with
`expected_time_shift`. The purpose of both functions is very similar,
to set the constant `EXPECTED_TIME_SHIFT`. I changed the name because
the time shift is now relative to UTC, not to the local time of the
client. Therefore, the function old name would be confusing.

ticket: 134
1 files changed, 20 insertions(+), 21 deletions(-)

M test/test_real_ftp.py
M test/test_real_ftp.py => test/test_real_ftp.py +20 -21
@@ 29,30 29,29 @@ import ftputil.stat_cache
import test


def utc_local_time_shift():
def expected_time_shift():
    """
    Return the expected time shift in seconds assuming the server
    uses UTC in its listings and the client uses local time.
    Return the expected time shift in seconds.

    This is needed because Pure-FTPd meanwhile seems to insist that
    the displayed time for files is in UTC.
    Unfortunately, the calculation may depend on the timezone of the server,
    i. e. the timezone used in directory listings coming from the server.

    So, depending on your test environment, you may need to change this
    function for your environment. _If_ you need an adapted
    `expected_time_shift`, please contact me (my e-mail address is in the
    ftputil documentation).

    In my particular case, I use Pure-FTPd as FTP server for the integration
    tests. At some point, it returned listings in the local timezone of the
    server, later it used UTC time, and now it uses the local timezone again.
    I wasn't able to find out why or how I can control this.
    """
    utc_tuple = time.gmtime()
    localtime_tuple = time.localtime()
    # To calculate the correct times shift, we need to ignore the
    # DST component in the localtime tuple, i. e. set it to 0.
    localtime_tuple = localtime_tuple[:-1] + (0,)
    time_shift_in_seconds = time.mktime(utc_tuple) - time.mktime(localtime_tuple)
    # To be safe, round the above value to units of 3600 s (1 hour).
    return round(time_shift_in_seconds / 3600.0) * 3600


# Difference between local times of server and client. If 0.0, server
# and client use the same timezone.
# EXPECTED_TIME_SHIFT = utc_local_time_shift()
# Pure-FTPd seems to have changed its mind (see docstring of
# `utc_local_time_shift`).
EXPECTED_TIME_SHIFT = 0.0
    raw_time_shift = (datetime.datetime.now() - datetime.datetime.utcnow()).seconds
    # To be safe, round the above value to units of 900 s (1/4 hours).
    return round(raw_time_shift / 900.0) * 900


EXPECTED_TIME_SHIFT = expected_time_shift()


class Cleaner: