diff options
author | joe <32424163+joekohlsdorf@users.noreply.github.com> | 2022-08-02 06:53:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-02 13:53:50 +0300 |
commit | 4ed8aba8441ae841e2c8e698b84ebda1da8208f9 (patch) | |
tree | 7a158efd29e52b4cfdc14d3cdc1f935fa568c331 /redis/commands/core.py | |
parent | fd9fea6bc07bf0970a5a42c5ec1788272446910c (diff) | |
download | redis-py-4ed8aba8441ae841e2c8e698b84ebda1da8208f9.tar.gz |
Fix timezone handling for datetime to unixtime conversions (#2213)
* Fix timezone handling for datetime to unixtime conversions
datetime objects are supported to set expire, these can have timezones.
mktime was used to convert these to unixtime. mktime in Python however is not timezone aware, it expects the input to be UTC and redis-py did not convert the datetime timestamps to UTC before calling mktime.
This can lead to:
1) Setting incorrect expire times because the input datetime object has a timezone but is passed to mktime without converting to UTC first.
2) When the datetime timestamp is within DST, mktime fails with "OverflowError: mktime argument out of range" because UTC doesn't have DST. This depends on libc versions.
* linters
Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
Diffstat (limited to 'redis/commands/core.py')
-rw-r--r-- | redis/commands/core.py | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py index 4243679..6cf0457 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2,7 +2,6 @@ import datetime import hashlib -import time import warnings from typing import ( TYPE_CHECKING, @@ -1674,7 +1673,7 @@ class BasicKeyCommands(CommandsProtocol): For more information see https://redis.io/commands/expireat """ if isinstance(when, datetime.datetime): - when = int(time.mktime(when.timetuple())) + when = int(when.timestamp()) exp_option = list() if nx: @@ -1769,14 +1768,12 @@ class BasicKeyCommands(CommandsProtocol): if exat is not None: pieces.append("EXAT") if isinstance(exat, datetime.datetime): - s = int(exat.microsecond / 1000000) - exat = int(time.mktime(exat.timetuple())) + s + exat = int(exat.timestamp()) pieces.append(exat) if pxat is not None: pieces.append("PXAT") if isinstance(pxat, datetime.datetime): - ms = int(pxat.microsecond / 1000) - pxat = int(time.mktime(pxat.timetuple())) * 1000 + ms + pxat = int(pxat.timestamp() * 1000) pieces.append(pxat) if persist: pieces.append("PERSIST") @@ -1995,8 +1992,7 @@ class BasicKeyCommands(CommandsProtocol): For more information see https://redis.io/commands/pexpireat """ if isinstance(when, datetime.datetime): - ms = int(when.microsecond / 1000) - when = int(time.mktime(when.timetuple())) * 1000 + ms + when = int(when.timestamp() * 1000) exp_option = list() if nx: exp_option.append("NX") @@ -2197,14 +2193,12 @@ class BasicKeyCommands(CommandsProtocol): if exat is not None: pieces.append("EXAT") if isinstance(exat, datetime.datetime): - s = int(exat.microsecond / 1000000) - exat = int(time.mktime(exat.timetuple())) + s + exat = int(exat.timestamp()) pieces.append(exat) if pxat is not None: pieces.append("PXAT") if isinstance(pxat, datetime.datetime): - ms = int(pxat.microsecond / 1000) - pxat = int(time.mktime(pxat.timetuple())) * 1000 + ms + pxat = int(pxat.timestamp() * 1000) pieces.append(pxat) if keepttl: pieces.append("KEEPTTL") |