Skip to content

Commit

Permalink
fix wbtime and btime for all endpoints returning a ``GameStat…
Browse files Browse the repository at this point in the history
…e``.

* 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 <[email protected]>
Co-authored-by: Trevor Fitzgerald <[email protected]>
Co-authored-by: kraktus <[email protected]>
  • Loading branch information
4 people authored Apr 1, 2024
1 parent 7acdd4b commit bc51b80
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
--------------------

Expand Down
8 changes: 4 additions & 4 deletions berserk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion berserk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from berserk import models
from berserk import models, utils


def test_conversion():
Expand All @@ -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

0 comments on commit bc51b80

Please sign in to comment.