Skip to content

Commit

Permalink
Fix rounding error in CachedResponse.expires_unix property
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Mar 19, 2024
1 parent 320d5b8 commit 95d9368
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# History

## 1.2.1 (2024-02-xx)
## Unreleased

🪲 **Bugfixes:**
* Fix `normalize_headers` not accepting header values in bytes
* Fix rounding error in `CachedResponse.expires_unix` property

## 1.2.0 (2024-02-17)

Expand Down
12 changes: 6 additions & 6 deletions requests_cache/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from datetime import datetime, timedelta
from logging import getLogger
from time import time
from typing import TYPE_CHECKING, Dict, List, Optional, Union

import attr
Expand Down Expand Up @@ -144,8 +143,9 @@ def expires_delta(self) -> Optional[int]:
@property
def expires_unix(self) -> Optional[int]:
"""Get expiration time as a Unix timestamp"""
seconds = self.expires_delta
return round(time() + seconds) if seconds is not None else None
if self.expires is None:
return None
return round(self.expires.timestamp())

@property
def from_cache(self) -> bool:
Expand Down Expand Up @@ -187,10 +187,10 @@ def __setstate__(self, state):

def __str__(self):
return (
f'<CachedResponse [{self.status_code}]: '
f'created: {format_datetime(self.created_at)}, '
f"<CachedResponse [{self.status_code}]: "
f"created: {format_datetime(self.created_at)}, "
f'expires: {format_datetime(self.expires)} ({"stale" if self.is_expired else "fresh"}), '
f'size: {format_file_size(self.size)}, request: {self.request}>'
f"size: {format_file_size(self.size)}, request: {self.request}>"
)


Expand Down

0 comments on commit 95d9368

Please sign in to comment.