Skip to content

Commit

Permalink
update: uses UTC for timestamps
Browse files Browse the repository at this point in the history
- Makes timestamps consistent, uses UTC timezone

Ref: #554

Co-authored-by: Viraj Kanwade <viraj.kanwade@forgeahead.io>
  • Loading branch information
namsnath and mavwolverine committed Feb 9, 2025
1 parent f1b9103 commit 5a7daa5
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.28.0]
- Updates timestamps to use UTC instead of GMT as the timezone

## [0.27.0] - 2024-12-30

- Added OAuth2Provider recipe
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

setup(
name="supertokens_python",
version="0.27.0",
version="0.28.0",
author="SuperTokens",
license="Apache 2.0",
author_email="team@supertokens.com",
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from __future__ import annotations

SUPPORTED_CDI_VERSIONS = ["5.2"]
VERSION = "0.27.0"
VERSION = "0.28.0"
TELEMETRY = "/telemetry"
USER_COUNT = "/users/count"
USER_DELETE = "/user/remove"
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/framework/django/django_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def set_cookie(
key=key,
value=value,
expires=datetime.fromtimestamp(ceil(expires / 1000)).strftime(
"%a, %d %b %Y %H:%M:%S GMT"
"%a, %d %b %Y %H:%M:%S UTC"
),
path=path,
domain=domain,
Expand Down
2 changes: 1 addition & 1 deletion tests/Django/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async def test_login_handle(self):

try:
datetime.strptime(
cookies["sAccessToken"]["expires"], "%a, %d %b %Y %H:%M:%S GMT"
cookies["sAccessToken"]["expires"], "%a, %d %b %Y %H:%M:%S UTC"
)
except ValueError:
assert False, "cookies expiry time doesn't have the correct format"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import sys
from datetime import datetime as real_datetime
from datetime import datetime as real_datetime, timezone
from unittest import TestCase
from unittest.mock import MagicMock, patch

Expand Down Expand Up @@ -43,7 +43,7 @@ def teardown_method(self, _):
@patch("supertokens_python.logger.datetime", wraps=real_datetime)
def test_1_json_msg_format(self, datetime_mock: MagicMock):
enable_debug_logging()
datetime_mock.now.return_value = real_datetime(2000, 1, 1)
datetime_mock.now.return_value = real_datetime(2000, 1, 1, tzinfo=timezone.utc)

with self.assertLogs(level="DEBUG") as captured:
log_debug_message("API replied with status 200")
Expand All @@ -52,7 +52,7 @@ def test_1_json_msg_format(self, datetime_mock: MagicMock):
out = json.loads(record.msg)

assert out == {
"t": "2000-01-01T00:00Z",
"t": "2000-01-01T00:00:00+00Z",
"sdkVer": VERSION,
"message": "API replied with status 200",
"file": "../tests/test_logger.py:49",
Expand Down
30 changes: 13 additions & 17 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.

import asyncio
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional
from unittest.mock import MagicMock

Expand Down Expand Up @@ -663,15 +663,13 @@ async def test_token_cookie_expires(
for c in response.cookies.jar:
if c.name == "sAccessToken": # 100 years (set by the SDK)
# some time must have elasped since the cookie was set. So less than current time
assert (
datetime.fromtimestamp(c.expires or 0) - timedelta(days=365.25 * 100)
< datetime.now()
)
assert datetime.fromtimestamp(c.expires or 0, tz=timezone.utc) - timedelta(
days=365.25 * 100
) < datetime.now(tz=timezone.utc)
if c.name == "sRefreshToken": # 100 days (set by the core)
assert (
datetime.fromtimestamp(c.expires or 0) - timedelta(days=100)
< datetime.now()
)
assert datetime.fromtimestamp(c.expires or 0, tz=timezone.utc) - timedelta(
days=100
) < datetime.now(tz=timezone.utc)

assert response.headers["anti-csrf"] != ""
assert response.headers["front-token"] != ""
Expand All @@ -693,15 +691,13 @@ async def test_token_cookie_expires(
for c in response.cookies.jar:
if c.name == "sAccessToken": # 100 years (set by the SDK)
# some time must have elasped since the cookie was set. So less than current time
assert (
datetime.fromtimestamp(c.expires or 0) - timedelta(days=365.25 * 100)
< datetime.now()
)
assert datetime.fromtimestamp(c.expires or 0, tz=timezone.utc) - timedelta(
days=365.25 * 100
) < datetime.now(tz=timezone.utc)
if c.name == "sRefreshToken": # 100 days (set by the core)
assert (
datetime.fromtimestamp(c.expires or 0) - timedelta(days=100)
< datetime.now()
)
assert datetime.fromtimestamp(c.expires or 0, tz=timezone.utc) - timedelta(
days=100
) < datetime.now(tz=timezone.utc)

assert response.headers["anti-csrf"] != ""
assert response.headers["front-token"] != ""
Expand Down
6 changes: 3 additions & 3 deletions tests/thirdparty/test_thirdparty.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timezone
import json
from base64 import b64encode
from typing import Dict, Any, Optional
Expand Down Expand Up @@ -208,7 +208,7 @@ async def test_signinup_when_validate_access_token_throws(fastapi_client: TestCl
async def test_signinup_works_when_validate_access_token_does_not_throw(
fastapi_client: TestClient, mocker: MockerFixture
):
time = str(datetime.datetime.now())
time = str(datetime.now(tz=timezone.utc))
mocker.patch(
"supertokens_python.recipe.thirdparty.providers.custom.get_supertokens_user_info_result_from_raw_user_info",
return_value=UserInfo(
Expand Down Expand Up @@ -276,7 +276,7 @@ async def test_signinup_works_when_validate_access_token_does_not_throw(
async def test_signinup_android_without_redirect_uri(
fastapi_client: TestClient, mocker: MockerFixture
):
time = str(datetime.datetime.now())
time = str(datetime.now(tz=timezone.utc))
mocker.patch(
"supertokens_python.recipe.thirdparty.providers.custom.get_supertokens_user_info_result_from_raw_user_info",
return_value=UserInfo(
Expand Down
8 changes: 2 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
import asyncio
import json
from datetime import datetime, timezone
from datetime import datetime
from http.cookies import SimpleCookie
from os import environ, kill, remove, scandir
from pathlib import Path
Expand Down Expand Up @@ -313,11 +313,7 @@ def assert_info_clears_tokens(info: Dict[str, Any], token_transfer_method: str):


def get_unix_timestamp(expiry: str):
return int(
datetime.strptime(expiry, "%a, %d %b %Y %H:%M:%S GMT")
.replace(tzinfo=timezone.utc)
.timestamp()
)
return int(datetime.strptime(expiry, "%a, %d %b %Y %H:%M:%S UTC").timestamp())


def verify_within_5_second_diff(n1: int, n2: int):
Expand Down

0 comments on commit 5a7daa5

Please sign in to comment.