Skip to content

Commit

Permalink
Fix scheduling future actions (#237)
Browse files Browse the repository at this point in the history
* Fix scheduling future actions

* add unit tests for sensor.utils datetime manipulations
  • Loading branch information
aromanielloNTIA committed Apr 25, 2023
1 parent e02a8b1 commit 81d0653
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/sensor/tests/test_sensor_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from datetime import datetime

from django.conf import settings

from sensor import utils


def test_timestamp_manipulation():
now = datetime.now()
now_int = int(now.timestamp())
now_fromint = datetime.fromtimestamp(now_int)
now_str = now.strftime(settings.DATETIME_FORMAT)

test_dt_int = utils.get_timestamp_from_datetime(now)
test_dt_fromint = utils.get_datetime_from_timestamp(now_int)
test_dt_fromstr = utils.parse_datetime_str(now_str)

assert now_int == test_dt_int
assert now_fromint == test_dt_fromint
assert now == test_dt_fromstr
8 changes: 4 additions & 4 deletions src/sensor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
logger = logging.getLogger(__name__)


def get_datetime_from_timestamp(ts):
def get_datetime_from_timestamp(ts: int) -> datetime:
return datetime.fromtimestamp(ts)


def get_timestamp_from_datetime(dt: datetime):
def get_timestamp_from_datetime(dt: datetime) -> int:
"""Assumes UTC datetime."""
return int(dt.strftime("%S"))
return int(dt.timestamp())


def parse_datetime_str(d):
def parse_datetime_str(d: str) -> datetime:
return datetime.strptime(d, settings.DATETIME_FORMAT)


Expand Down

0 comments on commit 81d0653

Please sign in to comment.