Skip to content

Commit 364ac01

Browse files
committed
compat: Use more compatible datetime definitions
1 parent eb7db61 commit 364ac01

3 files changed

Lines changed: 23 additions & 59 deletions

File tree

.pre-commit-config.yaml

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
11
repos:
2-
- repo: https://github.com/asottile/pyupgrade
3-
rev: v3.2.0
4-
hooks:
5-
- id: pyupgrade
6-
args: [--py36-plus]
7-
- repo: https://github.com/psf/black
8-
rev: 22.10.0
9-
hooks:
10-
- id: black
11-
args:
12-
- --safe
13-
- --quiet
14-
files: ^((xbox|tests)/.+)?[^/]+\.py$
15-
- repo: https://gitlab.com/pycqa/flake8
16-
rev: 5.0.4
17-
hooks:
18-
- id: flake8
19-
additional_dependencies:
20-
# - flake8-docstrings==1.5.0
21-
- pydocstyle==5.1.1
22-
files: ^(xbox)/.+\.py$
23-
- repo: https://github.com/PyCQA/bandit
24-
rev: 1.7.4
25-
hooks:
26-
- id: bandit
27-
args:
28-
- --quiet
29-
- --format=custom
30-
- --configfile=bandit.yaml
31-
files: ^(xbox|tests)/.+\.py$
32-
- repo: https://github.com/PyCQA/isort
33-
rev: 5.10.1
34-
hooks:
35-
- id: isort
36-
- repo: https://github.com/pre-commit/pre-commit-hooks
37-
rev: v3.2.0
38-
hooks:
39-
- id: check-executables-have-shebangs
40-
stages: [manual]
41-
- id: check-json
42-
- repo: https://github.com/prettier/prettier
43-
rev: 2.0.4
44-
hooks:
45-
- id: prettier
46-
stages: [manual]
47-
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.1.6
5+
hooks:
6+
# Run the linter.
7+
- id: ruff
8+
# Run the formatter.
9+
- id: ruff-format

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, UTC
1+
from datetime import datetime, timezone
22
import uuid
33

44
from ecdsa.keys import SigningKey, VerifyingKey
@@ -29,7 +29,9 @@
2929
async def auth_mgr(event_loop):
3030
session = SignedSession()
3131
mgr = AuthenticationManager(session, "abc", "123", "http://localhost")
32-
mgr.oauth = OAuth2TokenResponse.model_validate_json(get_response("auth_oauth2_token"))
32+
mgr.oauth = OAuth2TokenResponse.model_validate_json(
33+
get_response("auth_oauth2_token")
34+
)
3335
mgr.user_token = XAUResponse.model_validate_json(get_response("auth_user_token"))
3436
mgr.xsts_token = XSTSResponse.model_validate_json(get_response("auth_xsts_token"))
3537
yield mgr
@@ -77,4 +79,4 @@ def synthetic_request_signer(ecdsa_signing_key) -> RequestSigner:
7779

7880
@pytest.fixture(scope="session")
7981
def synthetic_timestamp() -> datetime:
80-
return datetime.fromtimestamp(1586999965, UTC)
82+
return datetime.fromtimestamp(1586999965, timezone.utc)

xbox/webapi/common/filetimes.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,35 @@
2525
"""Tools to convert between Python datetime instances and Microsoft times.
2626
"""
2727
from calendar import timegm
28-
import datetime
28+
from datetime import datetime, timezone, tzinfo, timedelta
2929

3030
# http://support.microsoft.com/kb/167296
3131
# How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
3232
EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970 as MS file time
3333
HUNDREDS_OF_NANOSECONDS = 10000000
3434

3535

36-
ZERO = datetime.timedelta(0)
37-
HOUR = datetime.timedelta(hours=1)
36+
ZERO = timedelta(0)
37+
HOUR = timedelta(hours=1)
3838

3939

40-
class UTC(datetime.tzinfo):
40+
class UTC(tzinfo):
4141
"""UTC"""
4242

43-
def utcoffset(self, dt):
43+
def utcoffset(self, dt: datetime) -> timedelta:
4444
return ZERO
4545

46-
def tzname(self, dt):
46+
def tzname(self, dt: datetime) -> str:
4747
return "UTC"
4848

49-
def dst(self, dt):
49+
def dst(self, dt: datetime) -> timedelta:
5050
return ZERO
5151

5252

5353
utc = UTC()
5454

5555

56-
def dt_to_filetime(dt: datetime.datetime):
56+
def dt_to_filetime(dt: datetime) -> int:
5757
"""Converts a datetime to Microsoft filetime format. If the object is
5858
time zone-naive, it is forced to UTC before conversion.
5959
@@ -75,7 +75,7 @@ def dt_to_filetime(dt: datetime.datetime):
7575
return ft + (dt.microsecond * 10)
7676

7777

78-
def filetime_to_dt(ft: int):
78+
def filetime_to_dt(ft: int) -> datetime:
7979
"""Converts a Microsoft filetime number to a Python datetime. The new
8080
datetime object is time zone-naive but is equivalent to tzinfo=utc.
8181
@@ -91,7 +91,7 @@ def filetime_to_dt(ft: int):
9191
# Get seconds and remainder in terms of Unix epoch
9292
(s, ns100) = divmod(ft - EPOCH_AS_FILETIME, HUNDREDS_OF_NANOSECONDS)
9393
# Convert to datetime object
94-
dt = datetime.datetime.fromtimestamp(s, datetime.UTC)
94+
dt = datetime.fromtimestamp(s, timezone.utc)
9595
# Add remainder in as microseconds. Python 3.2 requires an integer
9696
dt = dt.replace(microsecond=(ns100 // 10))
9797
return dt

0 commit comments

Comments
 (0)