Skip to content

Commit 8ff73b0

Browse files
committed
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.
1 parent 408aaf6 commit 8ff73b0

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

python/nav/activeipcollector/manager.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
1717
#
1818
"""Manage collection and storing of active ip-addresses statistics"""
19-
19+
import datetime
2020
import logging
2121
import time
22+
from typing import Optional
23+
2224
from IPy import IP
2325

2426
import nav.activeipcollector.collector as collector
@@ -82,11 +84,7 @@ def find_range(prefix):
8284
return 0
8385

8486

85-
def get_timestamp(timestamp=None):
87+
def get_timestamp(timestamp: Optional[datetime.datetime] = None) -> int:
8688
"""Find timestamp closest to 30 minutes intervals"""
8789

88-
def get_epoch():
89-
"""Find epoch from a datetime object"""
90-
return int(time.mktime(timestamp.timetuple()))
91-
92-
return get_epoch() if timestamp else int(time.time())
90+
return timestamp.timestamp() if timestamp else int(time.time())

0 commit comments

Comments
 (0)