[GH-ISSUE #892] is_token_expired buggy on non-UTC system #542

Open
opened 2026-02-27 23:23:14 +03:00 by kerem · 1 comment
Owner

Originally created by @joeb1415 on GitHub (Nov 23, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/892

Describe the bug
is_token_expired() uses time.time() which returns system local time, which, when running on a non-UTC system, can return incorrectly.

Your code

token_info = auth_manager.cache_handler.get_cached_token()

print(f"Token expires_at:     {token_info['expires_at']}")
print(f"Local Time (Pacific): {int(time.time())}")
print(f"UTC Time:             {int(time.mktime(time.gmtime()))}")

# Token shows not expired, but it is expected that it is expired
print(auth_manager.is_token_expired(token_info))
Token expires_at:     1669239147
Local Time (Pacific): 1669229727
UTC Time:             1669247727
False

Expected behavior
Replace time.time() with, e.g., time.mktime(time.gmtime()) wherever used.

Environment:

  • OS: Mac
  • Python version: 3.6.8
  • spotipy version: 2.21.0
  • IDE: VSCode

Additional context
Add any other context about the problem here.

Originally created by @joeb1415 on GitHub (Nov 23, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/892 **Describe the bug** `is_token_expired()` uses `time.time()` which returns system local time, which, when running on a non-UTC system, can return incorrectly. **Your code** ```py token_info = auth_manager.cache_handler.get_cached_token() print(f"Token expires_at: {token_info['expires_at']}") print(f"Local Time (Pacific): {int(time.time())}") print(f"UTC Time: {int(time.mktime(time.gmtime()))}") # Token shows not expired, but it is expected that it is expired print(auth_manager.is_token_expired(token_info)) ``` ``` Token expires_at: 1669239147 Local Time (Pacific): 1669229727 UTC Time: 1669247727 False ``` **Expected behavior** Replace `time.time()` with, e.g., `time.mktime(time.gmtime())` wherever used. **Environment:** - OS: Mac - Python version: 3.6.8 - spotipy version: 2.21.0 - IDE: VSCode **Additional context** Add any other context about the problem here.
Author
Owner

@stephanebruckert commented on GitHub (Dec 13, 2022):

Great catch, would love a PR for it if you get a chance!

<!-- gh-comment-id:1349932139 --> @stephanebruckert commented on GitHub (Dec 13, 2022): Great catch, would love a PR for it if you get a chance!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#542
No description provided.