Changeset 1906:f5ee6970ff95


Ignore:
Timestamp:
Jan 3, 2020, 6:21:39 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
amend_source:
5cbd3b835343138411b6c7b30e364efdbe6793d5
Message:
Fix: consider time shift in MS parser

Like in the unix parser, the "time shift" between client and server
wasn't considered.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/stat.py

    r1905 r1906  
    270270        `time_shift` parameter in the `parse_line` interface.
    271271
    272         If `with_precision` is true (default: false), return a
    273         two-element tuple consisting of the floating point number as
    274         described in the previous paragraph and the precision of the
    275         time in seconds. The default is `False` for backward
    276         compatibility with custom parsers.
     272        If `with_precision` is true return a two-element tuple
     273        consisting of the floating point number as described in the
     274        previous paragraph and the precision of the time in seconds.
     275        The default is `False` for backward compatibility with custom
     276        parsers.
    277277
    278278        The precision value takes into account that, for example, a
     
    344344            return st_mtime
    345345
    346     def parse_ms_time(self, date, time_, time_shift):
     346    def parse_ms_time(self, date, time_, time_shift, with_precision=False):
    347347        """
    348348        Return a floating point number, like from `time.mktime`, by
     
    354354        and can be set as the `time_shift` parameter in the
    355355        `parse_line` interface.
     356
     357        If `with_precision` is true return a two-element tuple
     358        consisting of the floating point number as described in the
     359        previous paragraph and the precision of the time in seconds.
     360        The default is `False` for backward compatibility with custom
     361        parsers.
     362
     363        The precision value takes into account that, for example, a
     364        time string like "10-23-2001 03:25PM" has only a precision of
     365        one minute. This information is important for the
     366        `upload_if_newer` and `download_if_newer` methods in the
     367        `FTPHost` class.
     368
     369        Usually, the returned precision is `MINUTE_PRECISION`, except
     370        when the date is before the epoch, in which case the returned
     371        `st_mtime` value is set to 0.0 and the precision to
     372        `UNKNOWN_PRECISION`.
    356373
    357374        Times in MS-style directory listings typically have the
     
    393410        if hour != 12 and am_pm == "P":
    394411            hour += 12
    395         st_mtime = self._mktime((year, month, day, hour, minute, 0, 0, 0, -1))
    396         return st_mtime
     412        server_datetime = self._datetime(year, month, day, hour, minute, 0)
     413        client_datetime = server_datetime - datetime.timedelta(seconds=time_shift)
     414        st_mtime = client_datetime.timestamp()
     415        if st_mtime < 0.0:
     416            st_mtime_precision = UNKNOWN_PRECISION
     417            st_mtime = 0.0
     418        else:
     419            st_mtime_precision = MINUTE_PRECISION
     420        if with_precision:
     421            return st_mtime, st_mtime_precision
     422        else:
     423            return st_mtime
    397424
    398425
     
    545572        st_atime = None
    546573        # st_mtime
    547         st_mtime = self.parse_ms_time(date, time_, time_shift)
     574        st_mtime, st_mtime_precision = self.parse_ms_time(date, time_, time_shift, with_precision=True)
    548575        # st_ctime
    549576        st_ctime = None
     
    569596        stat_result._st_target = None
    570597        # mtime precision in seconds
    571         #  If we had a datetime before the epoch, the resulting value
    572         #  0.0 doesn't tell us anything about the precision.
    573         if st_mtime == 0.0:
    574             stat_result._st_mtime_precision = UNKNOWN_PRECISION
    575         else:
    576             stat_result._st_mtime_precision = MINUTE_PRECISION
     598        stat_result._st_mtime_precision = st_mtime_precision
    577599        return stat_result
    578600
  • test/test_stat.py

    r1903 r1906  
    344344                None,
    345345                None,
    346                 (2001, 7, 27, 11, 16, 0),
     346                (2001, 7, 27, 6, 16, 0),
    347347                None,
    348348                MINUTE_PRECISION,
     
    359359                None,
    360360                None,
    361                 (1995, 10, 23, 15, 25, 0),
     361                (1995, 10, 23, 10, 25, 0),
    362362                None,
    363363                MINUTE_PRECISION,
     
    374374                12266720,
    375375                None,
    376                 (2000, 7, 17, 14, 8, 0),
     376                (2000, 7, 17, 9, 8, 0),
    377377                None,
    378378                MINUTE_PRECISION,
     
    389389                12266720,
    390390                None,
    391                 (2009, 7, 17, 0, 8, 0),
     391                (2009, 7, 16, 19, 8, 0),
    392392                None,
    393393                MINUTE_PRECISION,
     
    404404                12266720,
    405405                None,
    406                 (2009, 7, 17, 12, 8, 0),
     406                (2009, 7, 17, 7, 8, 0),
    407407                None,
    408408                MINUTE_PRECISION,
     
    430430                None,
    431431                None,
    432                 (2012, 10, 19, 15, 13, 0),
     432                (2012, 10, 19, 10, 13, 0),
    433433                None,
    434434                MINUTE_PRECISION,
     
    445445                None,
    446446                None,
    447                 (2012, 10, 19, 15, 13, 0),
     447                (2012, 10, 19, 10, 13, 0),
    448448                None,
    449449                MINUTE_PRECISION,
Note: See TracChangeset for help on using the changeset viewer.