From 8ff73b0aa2e58898ed17901ab4be03346ab884af Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Tue, 27 Feb 2024 15:11:41 +0100 Subject: [PATCH] Refactor `get_timestamp()` No need to get convoluted about how a unix timestamp is derived from a datetime object - it's right there in its methods. Also adds type annotations to make it easer to reason about what the arguments and results are. --- python/nav/activeipcollector/manager.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/python/nav/activeipcollector/manager.py b/python/nav/activeipcollector/manager.py index ae52ccd392..99ba4c0edc 100644 --- a/python/nav/activeipcollector/manager.py +++ b/python/nav/activeipcollector/manager.py @@ -16,9 +16,11 @@ # License along with NAV. If not, see . # """Manage collection and storing of active ip-addresses statistics""" - +import datetime import logging import time +from typing import Optional + from IPy import IP import nav.activeipcollector.collector as collector @@ -82,11 +84,7 @@ def find_range(prefix): return 0 -def get_timestamp(timestamp=None): +def get_timestamp(timestamp: Optional[datetime.datetime] = None) -> int: """Find timestamp closest to 30 minutes intervals""" - def get_epoch(): - """Find epoch from a datetime object""" - return int(time.mktime(timestamp.timetuple())) - - return get_epoch() if timestamp else int(time.time()) + return timestamp.timestamp() if timestamp else int(time.time())