From ece7b0d9f26afb18171388e1eccea8de1cf37137 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 5 Jan 2025 10:49:53 +0100 Subject: [PATCH] feat: add format_ms_time_detailed view --- freqtrade/util/__init__.py | 2 ++ freqtrade/util/datetime_helpers.py | 9 +++++++++ tests/util/test_datetime_helpers.py | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/freqtrade/util/__init__.py b/freqtrade/util/__init__.py index a0b618b11..5a4857eea 100644 --- a/freqtrade/util/__init__.py +++ b/freqtrade/util/__init__.py @@ -9,6 +9,7 @@ from freqtrade.util.datetime_helpers import ( dt_utc, format_date, format_ms_time, + format_ms_time_det, shorten_date, ) from freqtrade.util.dry_run_wallet import get_dry_run_wallet @@ -36,6 +37,7 @@ __all__ = [ "dt_utc", "format_date", "format_ms_time", + "format_ms_time_det", "get_dry_run_wallet", "FtPrecise", "PeriodicCache", diff --git a/freqtrade/util/datetime_helpers.py b/freqtrade/util/datetime_helpers.py index 255779849..03ee3b6d9 100644 --- a/freqtrade/util/datetime_helpers.py +++ b/freqtrade/util/datetime_helpers.py @@ -107,3 +107,12 @@ def format_ms_time(date: int | float) -> str: : epoch-string in ms """ return dt_from_ts(date).strftime("%Y-%m-%dT%H:%M:%S") + + +def format_ms_time_det(date: int | float) -> str: + """ + convert MS date to readable format - detailed. + : epoch-string in ms + """ + # return dt_from_ts(date).isoformat(timespec="milliseconds") + return dt_from_ts(date).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] diff --git a/tests/util/test_datetime_helpers.py b/tests/util/test_datetime_helpers.py index d17d2ec5a..258c5a0b9 100644 --- a/tests/util/test_datetime_helpers.py +++ b/tests/util/test_datetime_helpers.py @@ -13,6 +13,7 @@ from freqtrade.util import ( dt_utc, format_date, format_ms_time, + format_ms_time_det, shorten_date, ) from freqtrade.util.datetime_helpers import dt_humanize_delta @@ -91,7 +92,7 @@ def test_dt_humanize() -> None: def test_format_ms_time() -> None: # Date 2018-04-10 18:02:01 - date_in_epoch_ms = 1523383321000 + date_in_epoch_ms = 1523383321132 date = format_ms_time(date_in_epoch_ms) assert isinstance(date, str) res = datetime(2018, 4, 10, 18, 2, 1, tzinfo=timezone.utc) @@ -111,3 +112,17 @@ def test_format_date() -> None: date = datetime(2021, 9, 30, 22, 59, 3, 455555, tzinfo=timezone.utc) assert format_date(date) == "2021-09-30 22:59:03" assert format_date(None) == "" + + +def test_format_ms_time_detailed() -> None: + # Date 2018-04-10 18:02:01 + date_in_epoch_ms = 1523383321132 + date = format_ms_time_det(date_in_epoch_ms) + assert isinstance(date, str) + res = datetime(2018, 4, 10, 18, 2, 1, 132145, tzinfo=timezone.utc) + assert date == res.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + assert date == "2018-04-10T18:02:01.132" + res = datetime(2017, 12, 13, 8, 2, 1, 512321, tzinfo=timezone.utc) + # Date 2017-12-13 08:02:01 + date_in_epoch_ms = 1513152121512 + assert format_ms_time_det(date_in_epoch_ms) == res.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]