Add dt_ts_none helper

This commit is contained in:
Matthias
2024-02-15 19:50:56 +01:00
committed by Joe Schr
parent c491c2a8ee
commit cb95298936
3 changed files with 22 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, from freqtrade.util.datetime_helpers import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts,
dt_ts_def, dt_utc, format_date, format_ms_time, dt_ts_def, dt_ts_none, dt_utc, format_date,
shorten_date) format_ms_time, shorten_date)
from freqtrade.util.formatters import decimals_per_coin, fmt_coin, round_value from freqtrade.util.formatters import decimals_per_coin, fmt_coin, round_value
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
@@ -14,6 +14,7 @@ __all__ = [
'dt_now', 'dt_now',
'dt_ts', 'dt_ts',
'dt_ts_def', 'dt_ts_def',
'dt_ts_none',
'dt_utc', 'dt_utc',
'format_date', 'format_date',
'format_ms_time', 'format_ms_time',

View File

@@ -31,12 +31,21 @@ def dt_ts(dt: Optional[datetime] = None) -> int:
def dt_ts_def(dt: Optional[datetime], default: int = 0) -> int: def dt_ts_def(dt: Optional[datetime], default: int = 0) -> int:
""" """
Return dt in ms as a timestamp in UTC. Return dt in ms as a timestamp in UTC.
If dt is None, return the current datetime in UTC. If dt is None, return the given default.
""" """
if dt: if dt:
return int(dt.timestamp() * 1000) return int(dt.timestamp() * 1000)
return default return default
def dt_ts_none(dt: Optional[datetime]) -> Optional[int]:
"""
Return dt in ms as a timestamp in UTC.
If dt is None, return the given default.
"""
if dt:
return int(dt.timestamp() * 1000)
return None
def dt_floor_day(dt: datetime) -> datetime: def dt_floor_day(dt: datetime) -> datetime:
"""Return the floor of the day for the given datetime.""" """Return the floor of the day for the given datetime."""

View File

@@ -3,8 +3,8 @@ from datetime import datetime, timedelta, timezone
import pytest import pytest
import time_machine import time_machine
from freqtrade.util import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, dt_ts_def, dt_utc, from freqtrade.util import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, dt_ts_def,
format_date, format_ms_time, shorten_date) dt_ts_none, dt_utc, format_date, format_ms_time, shorten_date)
def test_dt_now(): def test_dt_now():
@@ -29,6 +29,13 @@ def test_dt_ts_def():
assert dt_ts_def(datetime(2023, 5, 5, tzinfo=timezone.utc), 123) == 1683244800000 assert dt_ts_def(datetime(2023, 5, 5, tzinfo=timezone.utc), 123) == 1683244800000
def test_dt_ts_none():
assert dt_ts_none(None) is None
assert dt_ts_none(None) is None
assert dt_ts_none(datetime(2023, 5, 5, tzinfo=timezone.utc)) == 1683244800000
assert dt_ts_none(datetime(2023, 5, 5, tzinfo=timezone.utc)) == 1683244800000
def test_dt_utc(): def test_dt_utc():
assert dt_utc(2023, 5, 5) == datetime(2023, 5, 5, tzinfo=timezone.utc) assert dt_utc(2023, 5, 5) == datetime(2023, 5, 5, tzinfo=timezone.utc)
assert dt_utc(2023, 5, 5, 0, 0, 0, 555500) == datetime(2023, 5, 5, 0, 0, 0, 555500, assert dt_utc(2023, 5, 5, 0, 0, 0, 555500) == datetime(2023, 5, 5, 0, 0, 0, 555500,