add dt_ts_def helper
This commit is contained in:
@@ -1,5 +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_utc, format_date, format_ms_time, shorten_date)
|
dt_ts_def, dt_utc, format_date, format_ms_time,
|
||||||
|
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
|
||||||
from freqtrade.util.template_renderer import render_template, render_template_with_fallback # noqa
|
from freqtrade.util.template_renderer import render_template, render_template_with_fallback # noqa
|
||||||
@@ -11,6 +12,7 @@ __all__ = [
|
|||||||
'dt_humanize',
|
'dt_humanize',
|
||||||
'dt_now',
|
'dt_now',
|
||||||
'dt_ts',
|
'dt_ts',
|
||||||
|
'dt_ts_def',
|
||||||
'dt_utc',
|
'dt_utc',
|
||||||
'format_date',
|
'format_date',
|
||||||
'format_ms_time',
|
'format_ms_time',
|
||||||
|
|||||||
@@ -28,6 +28,16 @@ def dt_ts(dt: Optional[datetime] = None) -> int:
|
|||||||
return int(dt_now().timestamp() * 1000)
|
return int(dt_now().timestamp() * 1000)
|
||||||
|
|
||||||
|
|
||||||
|
def dt_ts_def(dt: Optional[datetime], default: int = 0) -> int:
|
||||||
|
"""
|
||||||
|
Return dt in ms as a timestamp in UTC.
|
||||||
|
If dt is None, return the current datetime in UTC.
|
||||||
|
"""
|
||||||
|
if dt:
|
||||||
|
return int(dt.timestamp() * 1000)
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
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."""
|
||||||
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
|
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ 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_utc,
|
from freqtrade.util import (dt_floor_day, dt_from_ts, dt_humanize, dt_now, dt_ts, dt_ts_def, dt_utc,
|
||||||
format_date, format_ms_time, shorten_date)
|
format_date, format_ms_time, shorten_date)
|
||||||
|
|
||||||
|
|
||||||
@@ -22,6 +22,13 @@ def test_dt_now():
|
|||||||
assert dt_ts(now) == int(now.timestamp() * 1000)
|
assert dt_ts(now) == int(now.timestamp() * 1000)
|
||||||
|
|
||||||
|
|
||||||
|
def test_dt_ts_def():
|
||||||
|
assert dt_ts_def(None) == 0
|
||||||
|
assert dt_ts_def(None, 123) == 123
|
||||||
|
assert dt_ts_def(datetime(2023, 5, 5, tzinfo=timezone.utc)) == 1683244800000
|
||||||
|
assert dt_ts_def(datetime(2023, 5, 5, tzinfo=timezone.utc), 123) == 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,
|
||||||
|
|||||||
Reference in New Issue
Block a user