Changeset 1923:7b4b21f5da09


Ignore:
Timestamp:
Apr 10, 2020, 8:18:40 PM (16 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
amend_source:
f3a284c63b66712d605e06683b44bd0e70a190fd
Message:
Subtract time shift only once

The new code in `_Stat.parse_unix_time` returns the server time
already corrected by the time shift. Therefore it's a bug to subtract
the time shift again in `RemoteFile.mtime`.

ticket: 134
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file_transfer.py

    r1848 r1923  
    1 # Copyright (C) 2013-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2013-2020, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    8080        # Convert to client time zone (see definition of time
    8181        # shift in docstring of `FTPHost.set_time_shift`).
    82         return self._path.getmtime(self.name) - self._host.time_shift()
     82        return self._path.getmtime(self.name)
    8383
    8484    def mtime_precision(self):
  • test/test_file_transfer.py

    r1919 r1923  
    1 # Copyright (C) 2010-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2010-2020, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
    44
     5import datetime
    56import io
    67import random
     
    1011import ftputil.file_transfer
    1112import ftputil.stat
     13
     14from test import test_base
     15from test import scripted_session
     16
     17
     18Call = scripted_session.Call
    1219
    1320
     
    2431    def mtime_precision(self):
    2532        return self._mtime_precision
     33
     34
     35class TestRemoteFile:
     36    def test_time_shift_subtracted_only_once(self):
     37        """
     38        Test whether the time shift value is subtracted from the initial server
     39        timestamp only once.
     40
     41        This subtraction happens in `stat._Stat.parse_unix_time`, so it must
     42        _not_ be done a second time in `file_transfer.RemoteFile`.
     43        """
     44        utcnow = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
     45        # 3 hours
     46        time_shift = 3 * 60 * 60
     47        dir_line = test_base.dir_line(
     48            datetime_=utcnow + datetime.timedelta(seconds=time_shift), name="dummy_name"
     49        )
     50        script = [
     51            Call("__init__"),
     52            Call("pwd", result="/"),
     53            Call("cwd", args=("/",)),
     54            Call("cwd", args=("/",)),
     55            Call("dir", args=("",), result=dir_line),
     56            Call("cwd", args=("/",)),
     57            Call("close"),
     58        ]
     59        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     60            host.set_time_shift(3 * 60 * 60)
     61            remote_file = ftputil.file_transfer.RemoteFile(host, "dummy_name", 0o644)
     62            remote_mtime = remote_file.mtime()
     63        # The remote mtime should be corrected by the time shift, so the
     64        # calculated UTC time is the same as for the client. The 60.0 (seconds)
     65        # is the timestamp precision.
     66        assert remote_mtime <= utcnow.timestamp() <= remote_mtime + 60.0
    2667
    2768
Note: See TracChangeset for help on using the changeset viewer.