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

Use aware datetimes to represent UTC #3728

Merged
merged 1 commit into from
Jun 10, 2023
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

<!-- Changes to Black's terminal output and error messages -->

- Use aware UTC datetimes internally, avoids deprecation warning on Python 3.12 (#3728)

### _Blackd_

<!-- Changes to blackd -->
Expand Down
4 changes: 2 additions & 2 deletions docs/usage_and_configuration/the_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ If you'd like colored diffs, you can enable them with `--color`.

```console
$ black test.py --diff
--- test.py 2021-03-08 22:23:40.848954 +0000
+++ test.py 2021-03-08 22:23:47.126319 +0000
--- test.py 2021-03-08 22:23:40.848954+00:00
+++ test.py 2021-03-08 22:23:47.126319+00:00
@@ -1 +1 @@
-print ( 'hello, world' )
+print("hello, world")
Expand Down
18 changes: 9 additions & 9 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import traceback
from contextlib import contextmanager
from dataclasses import replace
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
from json.decoder import JSONDecodeError
from pathlib import Path
Expand Down Expand Up @@ -807,7 +807,7 @@ def format_file_in_place(
elif src.suffix == ".ipynb":
mode = replace(mode, is_ipynb=True)

then = datetime.utcfromtimestamp(src.stat().st_mtime)
then = datetime.fromtimestamp(src.stat().st_mtime, timezone.utc)
header = b""
with open(src, "rb") as buf:
if mode.skip_source_first_line:
Expand All @@ -828,9 +828,9 @@ def format_file_in_place(
with open(src, "w", encoding=encoding, newline=newline) as f:
f.write(dst_contents)
elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
now = datetime.utcnow()
src_name = f"{src}\t{then} +0000"
dst_name = f"{src}\t{now} +0000"
now = datetime.now(timezone.utc)
src_name = f"{src}\t{then}"
dst_name = f"{src}\t{now}"
if mode.is_ipynb:
diff_contents = ipynb_diff(src_contents, dst_contents, src_name, dst_name)
else:
Expand Down Expand Up @@ -868,7 +868,7 @@ def format_stdin_to_stdout(
write a diff to stdout. The `mode` argument is passed to
:func:`format_file_contents`.
"""
then = datetime.utcnow()
then = datetime.now(timezone.utc)

if content is None:
src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
Expand All @@ -893,9 +893,9 @@ def format_stdin_to_stdout(
dst += "\n"
f.write(dst)
elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
now = datetime.utcnow()
src_name = f"STDIN\t{then} +0000"
dst_name = f"STDOUT\t{now} +0000"
now = datetime.now(timezone.utc)
src_name = f"STDIN\t{then}"
dst_name = f"STDOUT\t{now}"
d = diff(src, dst, src_name, dst_name)
if write_back == WriteBack.COLOR_DIFF:
d = color_diff(d)
Expand Down
10 changes: 5 additions & 5 deletions src/blackd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
from concurrent.futures import Executor, ProcessPoolExecutor
from datetime import datetime
from datetime import datetime, timezone
from functools import partial
from multiprocessing import freeze_support
from typing import Set, Tuple
Expand Down Expand Up @@ -138,7 +138,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
req_bytes = await request.content.read()
charset = request.charset if request.charset is not None else "utf8"
req_str = req_bytes.decode(charset)
then = datetime.utcnow()
then = datetime.now(timezone.utc)

header = ""
if skip_source_first_line:
Expand All @@ -165,9 +165,9 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
# Only output the diff in the HTTP response
only_diff = bool(request.headers.get(DIFF_HEADER, False))
if only_diff:
now = datetime.utcnow()
src_name = f"In\t{then} +0000"
dst_name = f"Out\t{now} +0000"
now = datetime.now(timezone.utc)
src_name = f"In\t{then}"
dst_name = f"Out\t{now}"
loop = asyncio.get_event_loop()
formatted_str = await loop.run_in_executor(
executor,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def test_piping(self) -> None:

def test_piping_diff(self) -> None:
diff_header = re.compile(
r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d "
r"\+\d\d\d\d"
r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d"
r"\+\d\d:\d\d"
)
source, _ = read_data("simple_cases", "expression.py")
expected, _ = read_data("simple_cases", "expression.diff")
Expand Down Expand Up @@ -300,7 +300,7 @@ def test_expression_diff(self) -> None:
tmp_file = Path(black.dump_to_file(source))
diff_header = re.compile(
rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
)
try:
result = BlackRunner().invoke(
Expand Down Expand Up @@ -411,7 +411,7 @@ def test_skip_magic_trailing_comma(self) -> None:
tmp_file = Path(black.dump_to_file(source))
diff_header = re.compile(
rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
)
try:
result = BlackRunner().invoke(
Expand Down Expand Up @@ -1750,7 +1750,7 @@ def test_bpo_2142_workaround(self) -> None:
tmp_file = Path(black.dump_to_file(source, ensure_final_newline=False))
diff_header = re.compile(
rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
)
try:
result = BlackRunner().invoke(black.main, ["--diff", str(tmp_file)])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blackd.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async def test_blackd_pyi(self) -> None:
@unittest_run_loop
async def test_blackd_diff(self) -> None:
diff_header = re.compile(
r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
)

source, _ = read_data("miscellaneous", "blackd_diff")
Expand Down