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

Bwtime fix #77

Merged
merged 4 commits into from
Apr 1, 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
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
Loading