Add dt_humanize helper

This commit is contained in:
Matthias 2023-05-18 07:07:22 +02:00
parent f657d06e91
commit 5d0cff2f76
4 changed files with 23 additions and 4 deletions

View File

@ -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

View File

@ -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',

View File

@ -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)

View File

@ -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'