From 942f0b4fbd18be47880ca503ae1dbb66cf075806 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 6 Jul 2023 20:27:49 +0200 Subject: [PATCH] Move format_ms_time to datetime_helpers --- freqtrade/data/history/history_utils.py | 2 +- freqtrade/misc.py | 9 --------- freqtrade/plugins/pairlist/VolumePairList.py | 3 +-- freqtrade/util/__init__.py | 7 ++++--- freqtrade/util/datetime_helpers.py | 8 ++++++++ tests/test_misc.py | 16 +--------------- tests/utils/test_datetime_helpers.py | 17 +++++++++++++++-- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/freqtrade/data/history/history_utils.py b/freqtrade/data/history/history_utils.py index e61f59cfa..bd925d901 100644 --- a/freqtrade/data/history/history_utils.py +++ b/freqtrade/data/history/history_utils.py @@ -14,8 +14,8 @@ from freqtrade.data.history.idatahandler import IDataHandler, get_datahandler from freqtrade.enums import CandleType from freqtrade.exceptions import OperationalException from freqtrade.exchange import Exchange -from freqtrade.misc import format_ms_time from freqtrade.plugins.pairlist.pairlist_helpers import dynamic_expand_pairlist +from freqtrade.util import format_ms_time from freqtrade.util.binance_mig import migrate_binance_futures_data diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 1e84bba87..350ac5eef 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -3,7 +3,6 @@ Various tool function for Freqtrade and scripts """ import gzip import logging -from datetime import datetime from pathlib import Path from typing import Any, Dict, Iterator, List, Mapping, Optional, TextIO, Union from urllib.parse import urlparse @@ -123,14 +122,6 @@ def pair_to_filename(pair: str) -> str: return pair -def format_ms_time(date: int) -> str: - """ - convert MS date to readable format. - : epoch-string in ms - """ - return datetime.fromtimestamp(date / 1000.0).strftime('%Y-%m-%dT%H:%M:%S') - - def deep_merge_dicts(source, destination, allow_null_overrides: bool = True): """ Values from Source override destination, destination is returned (and modified!!) diff --git a/freqtrade/plugins/pairlist/VolumePairList.py b/freqtrade/plugins/pairlist/VolumePairList.py index 0d5e33847..9e4a4fca9 100644 --- a/freqtrade/plugins/pairlist/VolumePairList.py +++ b/freqtrade/plugins/pairlist/VolumePairList.py @@ -13,9 +13,8 @@ from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date from freqtrade.exchange.types import Tickers -from freqtrade.misc import format_ms_time from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter -from freqtrade.util import dt_now +from freqtrade.util import dt_now, format_ms_time logger = logging.getLogger(__name__) diff --git a/freqtrade/util/__init__.py b/freqtrade/util/__init__.py index bed65a54b..92c79b899 100644 --- a/freqtrade/util/__init__.py +++ b/freqtrade/util/__init__.py @@ -1,5 +1,5 @@ from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, - dt_utc, shorten_date) + dt_utc, format_ms_time, shorten_date) from freqtrade.util.ft_precise import FtPrecise from freqtrade.util.periodic_cache import PeriodicCache @@ -7,11 +7,12 @@ from freqtrade.util.periodic_cache import PeriodicCache __all__ = [ 'dt_floor_day', 'dt_from_ts', + 'dt_humanize', 'dt_now', 'dt_ts', 'dt_utc', - 'dt_humanize', - 'shorten_date', + 'format_ms_time', 'FtPrecise', 'PeriodicCache', + 'shorten_date', ] diff --git a/freqtrade/util/datetime_helpers.py b/freqtrade/util/datetime_helpers.py index 39d134e11..7f44cbdb0 100644 --- a/freqtrade/util/datetime_helpers.py +++ b/freqtrade/util/datetime_helpers.py @@ -61,3 +61,11 @@ def dt_humanize(dt: datetime, **kwargs) -> str: :param kwargs: kwargs to pass to arrow's humanize() """ return arrow.get(dt).humanize(**kwargs) + + +def format_ms_time(date: int) -> str: + """ + convert MS date to readable format. + : epoch-string in ms + """ + return datetime.fromtimestamp(date / 1000.0).strftime('%Y-%m-%dT%H:%M:%S') diff --git a/tests/test_misc.py b/tests/test_misc.py index 03a236d73..21c832c2c 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1,6 +1,5 @@ # pragma pylint: disable=missing-docstring,C0103 -import datetime from copy import deepcopy from pathlib import Path from unittest.mock import MagicMock @@ -9,7 +8,7 @@ import pandas as pd import pytest from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json, - file_load_json, format_ms_time, json_to_dataframe, pair_to_filename, + file_load_json, json_to_dataframe, pair_to_filename, parse_db_uri_for_logging, plural, render_template, render_template_with_fallback, round_coin_value, safe_value_fallback, safe_value_fallback2) @@ -91,19 +90,6 @@ def test_pair_to_filename(pair, expected_result): assert pair_s == expected_result -def test_format_ms_time() -> None: - # Date 2018-04-10 18:02:01 - date_in_epoch_ms = 1523383321000 - date = format_ms_time(date_in_epoch_ms) - assert type(date) is str - res = datetime.datetime(2018, 4, 10, 18, 2, 1, tzinfo=datetime.timezone.utc) - assert date == res.astimezone(None).strftime('%Y-%m-%dT%H:%M:%S') - res = datetime.datetime(2017, 12, 13, 8, 2, 1, tzinfo=datetime.timezone.utc) - # Date 2017-12-13 08:02:01 - date_in_epoch_ms = 1513152121000 - assert format_ms_time(date_in_epoch_ms) == res.astimezone(None).strftime('%Y-%m-%dT%H:%M:%S') - - def test_safe_value_fallback(): dict1 = {'keya': None, 'keyb': 2, 'keyc': 5, 'keyd': None} assert safe_value_fallback(dict1, 'keya', 'keyb') == 2 diff --git a/tests/utils/test_datetime_helpers.py b/tests/utils/test_datetime_helpers.py index 5aec0da54..222410027 100644 --- a/tests/utils/test_datetime_helpers.py +++ b/tests/utils/test_datetime_helpers.py @@ -3,8 +3,8 @@ from datetime import datetime, timedelta, timezone import pytest import time_machine -from freqtrade.util import dt_floor_day, dt_from_ts, dt_now, dt_ts, dt_utc, shorten_date -from freqtrade.util.datetime_helpers import dt_humanize +from freqtrade.util import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, dt_utc, + format_ms_time, shorten_date) def test_dt_now(): @@ -57,3 +57,16 @@ def test_dt_humanize() -> None: assert dt_humanize(dt_now()) == 'just now' assert dt_humanize(dt_now(), only_distance=True) == 'instantly' assert dt_humanize(dt_now() - timedelta(hours=16), only_distance=True) == '16 hours' + + +def test_format_ms_time() -> None: + # Date 2018-04-10 18:02:01 + date_in_epoch_ms = 1523383321000 + date = format_ms_time(date_in_epoch_ms) + assert type(date) is str + res = datetime(2018, 4, 10, 18, 2, 1, tzinfo=timezone.utc) + assert date == res.astimezone(None).strftime('%Y-%m-%dT%H:%M:%S') + res = datetime(2017, 12, 13, 8, 2, 1, tzinfo=timezone.utc) + # Date 2017-12-13 08:02:01 + date_in_epoch_ms = 1513152121000 + assert format_ms_time(date_in_epoch_ms) == res.astimezone(None).strftime('%Y-%m-%dT%H:%M:%S')