From bc51b8014070e3372653a8715564841f28c63276 Mon Sep 17 00:00:00 2001 From: kraktus <56031107+kraktus@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:50:12 +0200 Subject: [PATCH] fix ``wbtime`` and ``btime`` for all endpoints returning a ``GameState``. * I changed the broken datetime _from_millis into timedelta_from_milis. Still must be testet, but womm * fixed typo * fix tests and docs * Add changelog and format following fix of ``wbtime`` and ``btime`` for all endpoints returning a ``GameState``. --------- Co-authored-by: Nicolas Vaagen Co-authored-by: Trevor Fitzgerald Co-authored-by: kraktus --- CHANGELOG.rst | 4 ++++ berserk/models.py | 8 ++++---- berserk/utils.py | 8 +++++++- tests/test_models.py | 16 +++++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c2dc730..79e38cd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,9 +4,13 @@ Changelog To be released -------------- +* Fixed ``wbtime`` and ``btime`` for all endpoints returning a ``GameState``. * Added ``sheet`` optional parameter to ``Tournaments.stream_results``, and fix returned typed dict. * Added ``studies.import_pgn`` to import PGN to study + +Thanks to @nicvagn, @tors42 and @fitztrev for their contributions to this release. + v0.13.2 (2023-12-04) -------------------- diff --git a/berserk/models.py b/berserk/models.py index 6828dce..7273394 100644 --- a/berserk/models.py +++ b/berserk/models.py @@ -68,10 +68,10 @@ class Game(Model): class GameState(Model): createdAt = utils.datetime_from_millis - wtime = utils.datetime_from_millis - btime = utils.datetime_from_millis - winc = utils.datetime_from_millis - binc = utils.datetime_from_millis + wtime = utils.timedelta_from_millis + btime = utils.timedelta_from_millis + winc = utils.timedelta_from_millis + binc = utils.timedelta_from_millis class Tournament(Model): diff --git a/berserk/utils.py b/berserk/utils.py index fd1732d..92b3069 100644 --- a/berserk/utils.py +++ b/berserk/utils.py @@ -2,7 +2,7 @@ import dateutil.parser -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta from typing import Any, Callable, Dict, List, NamedTuple, Tuple, TypeVar, Union, cast from .types.broadcast import BroadcastPlayer @@ -15,6 +15,12 @@ def to_millis(dt: datetime) -> int: return int(dt.timestamp() * 1000) +def timedelta_from_millis(millis: float) -> timedelta: + """Return a timedelta (A duration expressing the difference between two datetime or + date instances to microsecond resolution.) for a given milliseconds.""" + return timedelta(milliseconds=millis) + + def datetime_from_seconds(ts: float) -> datetime: """Return the datetime for the given seconds since the epoch. diff --git a/tests/test_models.py b/tests/test_models.py index 211848a..b8122cc 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,4 +1,4 @@ -from berserk import models +from berserk import models, utils def test_conversion(): @@ -8,3 +8,17 @@ class Example(models.Model): original = {"foo": "5", "bar": 3, "baz": "4"} modified = {"foo": 5, "bar": 3, "baz": "4"} assert Example.convert(original) == modified + + +def test_time_delta(): + """test timedelta_from millis""" + test_data = 1000.0 + dt1 = utils.datetime_from_millis(test_data) + dt2 = utils.datetime_from_millis(2 * test_data) + + delta_1 = utils.timedelta_from_millis(test_data) + + # time delta dt1 dt2 + delta_2 = dt2 - dt1 + + assert delta_1 == delta_2