Changeset 1931:834c3a213ba7


Ignore:
Timestamp:
Apr 11, 2020, 9:17:31 PM (16 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use `datetime` API instead of `time` API

Using `datetime` usually is far less convoluted.

The reason that `time` was used here is that this ftputil code likely
was written before the `datetime` module was added to Python's
standard library.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/host.py

    r1925 r1931  
    99"""
    1010
     11import datetime
    1112import ftplib
    1213import stat
     
    437438        # shift will be off by a year as well (see ticket #55).
    438439        if time_shift < -360 * 24 * 60 * 60:
    439             # Re-add one year and re-calculate the time shift. We don't
     440            # Readd one year and recalculate the time shift. We don't
    440441            # know how many days made up that year (it might have been
    441             # a leap year), so go the route via `time.localtime` and
    442             # `time.mktime`.
    443             server_time_struct = time.localtime(server_time)
    444             server_time_struct = (server_time_struct.tm_year + 1,) + server_time_struct[
    445                 1:
    446             ]
    447             server_time = time.mktime(server_time_struct)
    448             time_shift = server_time - now
     442            # a leap year), so go the route via `datetime.replace`.
     443            server_datetime = datetime.datetime.fromtimestamp(
     444                server_time, tz=datetime.timezone.utc
     445            )
     446            server_datetime = server_datetime.replace(year=server_datetime.year + 1)
     447            time_shift = server_datetime.timestamp() - now
    449448        # Do some sanity checks.
    450449        self.__assert_valid_time_shift(time_shift)
Note: See TracChangeset for help on using the changeset viewer.