Add dt_humanize helper
This commit is contained in:
@@ -43,7 +43,7 @@ from freqtrade.misc import (chunks, deep_merge_dicts, file_dump_json, file_load_
|
|||||||
safe_value_fallback2)
|
safe_value_fallback2)
|
||||||
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
|
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
|
||||||
from freqtrade.util import dt_from_ts, dt_now
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -1932,7 +1932,7 @@ class Exchange:
|
|||||||
logger.debug(
|
logger.debug(
|
||||||
"one_call: %s msecs (%s)",
|
"one_call: %s msecs (%s)",
|
||||||
one_call,
|
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(
|
input_coroutines = [self._async_get_candle_history(
|
||||||
pair, timeframe, candle_type, since) for since in
|
pair, timeframe, candle_type, since) for since in
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_now, dt_ts, dt_utc,
|
from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts,
|
||||||
shorten_date)
|
dt_utc, shorten_date)
|
||||||
from freqtrade.util.ft_precise import FtPrecise
|
from freqtrade.util.ft_precise import FtPrecise
|
||||||
from freqtrade.util.periodic_cache import PeriodicCache
|
from freqtrade.util.periodic_cache import PeriodicCache
|
||||||
|
|
||||||
@@ -10,6 +10,7 @@ __all__ = [
|
|||||||
'dt_now',
|
'dt_now',
|
||||||
'dt_ts',
|
'dt_ts',
|
||||||
'dt_utc',
|
'dt_utc',
|
||||||
|
'dt_humanize',
|
||||||
'shorten_date',
|
'shorten_date',
|
||||||
'FtPrecise',
|
'FtPrecise',
|
||||||
'PeriodicCache',
|
'PeriodicCache',
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import re
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import arrow
|
||||||
|
|
||||||
|
|
||||||
def dt_now() -> datetime:
|
def dt_now() -> datetime:
|
||||||
"""Return the current datetime in UTC."""
|
"""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('days?', 'd', new_date)
|
||||||
new_date = re.sub('^an?', '1', new_date)
|
new_date = re.sub('^an?', '1', new_date)
|
||||||
return 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)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import pytest
|
|||||||
import time_machine
|
import time_machine
|
||||||
|
|
||||||
from freqtrade.util import dt_floor_day, dt_from_ts, dt_now, dt_ts, dt_utc, shorten_date
|
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():
|
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_data = '1 day, 2 hours, 3 minutes, 4 seconds ago'
|
||||||
str_shorten_data = '1 d, 2 h, 3 min, 4 sec ago'
|
str_shorten_data = '1 d, 2 h, 3 min, 4 sec ago'
|
||||||
assert shorten_date(str_data) == str_shorten_data
|
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user