From 5d0cff2f763e18d2c32903d5db8c24c2c20ef26a Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 18 May 2023 07:07:22 +0200 Subject: [PATCH] Add dt_humanize helper --- freqtrade/exchange/exchange.py | 4 ++-- freqtrade/util/__init__.py | 5 +++-- freqtrade/util/datetime_helpers.py | 11 +++++++++++ tests/utils/test_datetime_helpers.py | 7 +++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 1c287438d..e41034f44 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -43,7 +43,7 @@ from freqtrade.misc import (chunks, deep_merge_dicts, file_dump_json, file_load_ safe_value_fallback2) from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist from freqtrade.util import dt_from_ts, dt_now -from freqtrade.util.datetime_helpers import dt_ts +from freqtrade.util.datetime_helpers import dt_humanize, dt_ts logger = logging.getLogger(__name__) @@ -1932,7 +1932,7 @@ class Exchange: logger.debug( "one_call: %s msecs (%s)", one_call, - arrow.utcnow().shift(seconds=one_call // 1000).humanize(only_distance=True) + dt_humanize(dt_now() - timedelta(milliseconds=one_call), only_distance=True) ) input_coroutines = [self._async_get_candle_history( pair, timeframe, candle_type, since) for since in diff --git a/freqtrade/util/__init__.py b/freqtrade/util/__init__.py index 68cb724e9..bed65a54b 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_now, dt_ts, dt_utc, - shorten_date) +from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, + dt_utc, shorten_date) from freqtrade.util.ft_precise import FtPrecise from freqtrade.util.periodic_cache import PeriodicCache @@ -10,6 +10,7 @@ __all__ = [ 'dt_now', 'dt_ts', 'dt_utc', + 'dt_humanize', 'shorten_date', 'FtPrecise', 'PeriodicCache', diff --git a/freqtrade/util/datetime_helpers.py b/freqtrade/util/datetime_helpers.py index ffe8effaa..39d134e11 100644 --- a/freqtrade/util/datetime_helpers.py +++ b/freqtrade/util/datetime_helpers.py @@ -2,6 +2,8 @@ import re from datetime import datetime, timezone from typing import Optional +import arrow + def dt_now() -> datetime: """Return the current datetime in UTC.""" @@ -50,3 +52,12 @@ def shorten_date(_date: str) -> str: new_date = re.sub('days?', 'd', new_date) new_date = re.sub('^an?', '1', new_date) return new_date + + +def dt_humanize(dt: datetime, **kwargs) -> str: + """ + Return a humanized string for the given datetime. + :param dt: datetime to humanize + :param kwargs: kwargs to pass to arrow's humanize() + """ + return arrow.get(dt).humanize(**kwargs) diff --git a/tests/utils/test_datetime_helpers.py b/tests/utils/test_datetime_helpers.py index 7a596fcbe..0db5f3798 100644 --- a/tests/utils/test_datetime_helpers.py +++ b/tests/utils/test_datetime_helpers.py @@ -4,6 +4,7 @@ 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 def test_dt_now(): @@ -51,3 +52,9 @@ def test_shorten_date() -> None: str_data = '1 day, 2 hours, 3 minutes, 4 seconds ago' str_shorten_data = '1 d, 2 h, 3 min, 4 sec ago' assert shorten_date(str_data) == str_shorten_data + + +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'