Skip to content

Fix timezone handling for datetime to unixtime conversions #2213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix timezone handling for datetime to unixtime conversions
* Fix start_id type for XAUTOCLAIM
* Remove verbose logging from cluster.py
* Add retry mechanism to async version of Connection
Expand Down
18 changes: 6 additions & 12 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import datetime
import hashlib
import time
import warnings
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1674,7 +1673,7 @@ def expireat(
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:
Expand Down Expand Up @@ -1769,14 +1768,12 @@ def getex(
if exat is not None:
pieces.append("EXAT")
if isinstance(exat, datetime.datetime):
s = int(exat.microsecond / 1000000)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed because the maxvalue for microsecond is 999999 resulting in s being 0 for any valid value.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed because timestamp() returns a fractional result and multiplying with 1000 will therefore already consider ms.

pxat = int(time.mktime(pxat.timetuple())) * 1000 + ms
pxat = int(pxat.timestamp() * 1000)
pieces.append(pxat)
if persist:
pieces.append("PERSIST")
Expand Down Expand Up @@ -1995,8 +1992,7 @@ def pexpireat(
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")
Expand Down Expand Up @@ -2197,14 +2193,12 @@ def set(
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")
Expand Down
7 changes: 3 additions & 4 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import binascii
import datetime
import re
import time
from string import ascii_letters

import pytest
Expand Down Expand Up @@ -750,7 +749,7 @@ async def test_expireat_no_key(self, r: redis.Redis):
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

Expand Down Expand Up @@ -875,8 +874,8 @@ async def test_pexpireat_no_key(self, r: redis.Redis):
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")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ def test_expireat_no_key(self, r):
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

Expand Down Expand Up @@ -1428,8 +1428,8 @@ def test_pexpireat_no_key(self, r):
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")
Expand Down