Skip to content

Commit

Permalink
Refactor get_timestamp()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lunkwill42 committed Feb 27, 2024
1 parent 408aaf6 commit 8ff73b0
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions python/nav/activeipcollector/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""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
Expand Down Expand Up @@ -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())

0 comments on commit 8ff73b0

Please sign in to comment.