Improve dt_now_ts helper
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from freqtrade.util.datetime_helpers import dt_from_ts, dt_now, dt_now_ts
|
from freqtrade.util.datetime_helpers import dt_from_ts, dt_now, dt_ts
|
||||||
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
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@ from freqtrade.util.periodic_cache import PeriodicCache
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
'dt_from_ts',
|
'dt_from_ts',
|
||||||
'dt_now',
|
'dt_now',
|
||||||
'dt_now_ts',
|
'dt_ts',
|
||||||
'FtPrecise',
|
'FtPrecise',
|
||||||
'PeriodicCache',
|
'PeriodicCache',
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
def dt_now() -> datetime:
|
def dt_now() -> datetime:
|
||||||
@@ -6,8 +7,10 @@ def dt_now() -> datetime:
|
|||||||
return datetime.now(timezone.utc)
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
def dt_now_ts() -> int:
|
def dt_ts(dt: Optional[datetime] = None) -> int:
|
||||||
"""Return the current timestamp in ms as a timestamp in UTC."""
|
"""Return the current timestamp in ms as a timestamp in UTC."""
|
||||||
|
if dt:
|
||||||
|
return int(dt.timestamp() * 1000)
|
||||||
return int(dt_now().timestamp() * 1000)
|
return int(dt_now().timestamp() * 1000)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,19 +3,24 @@ from datetime import datetime, timedelta, timezone
|
|||||||
import pytest
|
import pytest
|
||||||
import time_machine
|
import time_machine
|
||||||
|
|
||||||
from freqtrade.util import dt_from_ts, dt_now, dt_now_ts
|
from freqtrade.util import dt_from_ts, dt_now, dt_ts
|
||||||
|
|
||||||
|
|
||||||
def test_dt_now():
|
def test_dt_now():
|
||||||
with time_machine.travel("2021-09-01 05:01:00 +00:00", tick=False) as t:
|
with time_machine.travel("2021-09-01 05:01:00 +00:00", tick=False) as t:
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
assert dt_now() == now
|
assert dt_now() == now
|
||||||
assert dt_now_ts() == int(now.timestamp() * 1000)
|
assert dt_ts() == int(now.timestamp() * 1000)
|
||||||
|
assert dt_ts(now) == int(now.timestamp() * 1000)
|
||||||
|
|
||||||
t.shift(timedelta(hours=5))
|
t.shift(timedelta(hours=5))
|
||||||
assert dt_now() >= now
|
assert dt_now() >= now
|
||||||
assert dt_now() == datetime.now(timezone.utc)
|
assert dt_now() == datetime.now(timezone.utc)
|
||||||
assert dt_now_ts() == int(dt_now().timestamp() * 1000)
|
assert dt_ts() == int(dt_now().timestamp() * 1000)
|
||||||
|
# Test with different time than now
|
||||||
|
assert dt_ts(now) == int(now.timestamp() * 1000)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('as_ms', [True, False])
|
@pytest.mark.parametrize('as_ms', [True, False])
|
||||||
|
|||||||
Reference in New Issue
Block a user