summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorjoe <32424163+joekohlsdorf@users.noreply.github.com>2022-08-02 06:53:50 -0400
committerGitHub <noreply@github.com>2022-08-02 13:53:50 +0300
commit4ed8aba8441ae841e2c8e698b84ebda1da8208f9 (patch)
tree7a158efd29e52b4cfdc14d3cdc1f935fa568c331 /tests
parentfd9fea6bc07bf0970a5a42c5ec1788272446910c (diff)
downloadredis-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 'tests')
-rw-r--r--tests/test_asyncio/test_commands.py7
-rw-r--r--tests/test_commands.py6
2 files changed, 6 insertions, 7 deletions
diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py
index eaae185..4c25277 100644
--- a/tests/test_asyncio/test_commands.py
+++ b/tests/test_asyncio/test_commands.py
@@ -4,7 +4,6 @@ Tests async overrides of commands from their mixins
import binascii
import datetime
import re
-import time
from string import ascii_letters
import pytest
@@ -750,7 +749,7 @@ class TestRedisCommands:
async def test_expireat_unixtime(self, r: redis.Redis):
expire_at = await redis_server_time(r) + datetime.timedelta(minutes=1)
await r.set("a", "foo")
- expire_at_seconds = int(time.mktime(expire_at.timetuple()))
+ expire_at_seconds = int(expire_at.timestamp())
assert await r.expireat("a", expire_at_seconds)
assert 0 < await r.ttl("a") <= 61
@@ -875,8 +874,8 @@ class TestRedisCommands:
async def test_pexpireat_unixtime(self, r: redis.Redis):
expire_at = await redis_server_time(r) + datetime.timedelta(minutes=1)
await r.set("a", "foo")
- expire_at_seconds = int(time.mktime(expire_at.timetuple())) * 1000
- assert await r.pexpireat("a", expire_at_seconds)
+ expire_at_milliseconds = int(expire_at.timestamp() * 1000)
+ assert await r.pexpireat("a", expire_at_milliseconds)
assert 0 < await r.pttl("a") <= 61000
@skip_if_server_version_lt("2.6.0")
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 715d18c..f9134d8 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1185,7 +1185,7 @@ class TestRedisCommands:
def test_expireat_unixtime(self, r):
expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)
r["a"] = "foo"
- expire_at_seconds = int(time.mktime(expire_at.timetuple()))
+ expire_at_seconds = int(expire_at.timestamp())
assert r.expireat("a", expire_at_seconds) is True
assert 0 < r.ttl("a") <= 61
@@ -1428,8 +1428,8 @@ class TestRedisCommands:
def test_pexpireat_unixtime(self, r):
expire_at = redis_server_time(r) + datetime.timedelta(minutes=1)
r["a"] = "foo"
- expire_at_seconds = int(time.mktime(expire_at.timetuple())) * 1000
- assert r.pexpireat("a", expire_at_seconds) is True
+ expire_at_milliseconds = int(expire_at.timestamp() * 1000)
+ assert r.pexpireat("a", expire_at_milliseconds) is True
assert 0 < r.pttl("a") <= 61000
@skip_if_server_version_lt("7.0.0")