Skip to content
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

fix: datetime parsing with non-UTC timezones #2322

Merged
merged 4 commits into from
Mar 12, 2024
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
16 changes: 12 additions & 4 deletions playwright/_impl/_js_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

import collections.abc
import datetime
import math
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from urllib.parse import ParseResult, urlparse, urlunparse
Expand Down Expand Up @@ -128,8 +128,13 @@ def serialize_value(
return dict(v="-0")
if math.isnan(value):
return dict(v="NaN")
if isinstance(value, datetime):
return dict(d=value.isoformat() + "Z")
if isinstance(value, datetime.datetime):
# Node.js Date objects are always in UTC.
return {
"d": datetime.datetime.strftime(
value.astimezone(datetime.timezone.utc), "%Y-%m-%dT%H:%M:%S.%fZ"
)
}
if isinstance(value, bool):
return {"b": value}
if isinstance(value, (int, float)):
Expand Down Expand Up @@ -205,7 +210,10 @@ def parse_value(value: Any, refs: Optional[Dict[int, Any]] = None) -> Any:
return a

if "d" in value:
return datetime.fromisoformat(value["d"][:-1])
# Node.js Date objects are always in UTC.
return datetime.datetime.strptime(
Copy link
Member

Choose a reason for hiding this comment

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

It's unfortunate that we send an iso string over the wire, parsing millis since epoch (with datetime.fromtimestamp) would be more straightforward. But that's a big change.

value["d"], "%Y-%m-%dT%H:%M:%S.%fZ"
).replace(tzinfo=datetime.timezone.utc)

if "o" in value:
o: Dict = {}
Expand Down
8 changes: 6 additions & 2 deletions tests/async/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

import asyncio
import datetime
import re
from datetime import datetime

import pytest

Expand Down Expand Up @@ -183,7 +183,11 @@ async def test_assertions_locator_to_have_js_property(
)
await expect(page.locator("div")).to_have_js_property(
"foo",
{"a": 1, "b": "string", "c": datetime.utcfromtimestamp(1627503992000 / 1000)},
{
"a": 1,
"b": "string",
"c": datetime.datetime.fromtimestamp(1627503992000 / 1000),
},
)


Expand Down
27 changes: 24 additions & 3 deletions tests/async/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import math
from datetime import datetime
from datetime import datetime, timedelta, timezone
from typing import Optional
from urllib.parse import ParseResult, urlparse

Expand Down Expand Up @@ -216,17 +216,38 @@ async def test_evaluate_evaluate_date(page: Page) -> None:
result = await page.evaluate(
'() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
)
assert result == {"date": datetime.fromisoformat("2020-05-27T01:31:38.506")}
assert result == {
"date": datetime.fromisoformat("2020-05-27T01:31:38.506").replace(
tzinfo=timezone.utc
)
}


async def test_evaluate_roundtrip_date_without_tzinfo(page: Page) -> None:
date = datetime.fromisoformat("2020-05-27T01:31:38.506")
result = await page.evaluate("date => date", date)
assert result.timestamp() == date.timestamp()
mxschmitt marked this conversation as resolved.
Show resolved Hide resolved


async def test_evaluate_roundtrip_date(page: Page) -> None:
date = datetime.fromisoformat("2020-05-27T01:31:38.506").replace(
tzinfo=timezone.utc
)
result = await page.evaluate("date => date", date)
assert result == date


async def test_evaluate_roundtrip_date_with_tzinfo(page: Page) -> None:
date = datetime.fromisoformat("2020-05-27T01:31:38.506")
date = date.astimezone(timezone(timedelta(hours=4)))
result = await page.evaluate("date => date", date)
assert result == date


async def test_evaluate_jsonvalue_date(page: Page) -> None:
date = datetime.fromisoformat("2020-05-27T01:31:38.506")
date = datetime.fromisoformat("2020-05-27T01:31:38.506").replace(
tzinfo=timezone.utc
)
result = await page.evaluate(
'() => ({ date: new Date("2020-05-27T01:31:38.506Z") })'
)
Expand Down
6 changes: 4 additions & 2 deletions tests/async/test_jshandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import json
import math
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Dict

from playwright.async_api import Page
Expand Down Expand Up @@ -180,7 +180,9 @@ async def test_jshandle_json_value_work(page: Page) -> None:
async def test_jshandle_json_value_work_with_dates(page: Page) -> None:
handle = await page.evaluate_handle('() => new Date("2020-05-27T01:31:38.506Z")')
json = await handle.json_value()
assert json == datetime.fromisoformat("2020-05-27T01:31:38.506")
assert json == datetime.fromisoformat("2020-05-27T01:31:38.506").replace(
tzinfo=timezone.utc
)


async def test_jshandle_json_value_should_work_for_circular_object(page: Page) -> None:
Expand Down
8 changes: 6 additions & 2 deletions tests/sync/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import re
from datetime import datetime

import pytest

Expand Down Expand Up @@ -163,7 +163,11 @@ def test_assertions_locator_to_have_js_property(page: Page, server: Server) -> N
)
expect(page.locator("div")).to_have_js_property(
"foo",
{"a": 1, "b": "string", "c": datetime.utcfromtimestamp(1627503992000 / 1000)},
{
"a": 1,
"b": "string",
"c": datetime.datetime.fromtimestamp(1627503992000 / 1000),
},
)


Expand Down
Loading