Skip to content

Commit

Permalink
Merge pull request Uninett#2831 from lunkwill42/bugfix/activeipcollec…
Browse files Browse the repository at this point in the history
…tor-naive-timestamp-test

Fix activeipcollector `get_timestamp` function implementation and its broken timezone-naive test
  • Loading branch information
lunkwill42 authored Mar 1, 2024
2 parents dcc18f9 + 8ff73b0 commit 127ce87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 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())
6 changes: 3 additions & 3 deletions tests/unittests/general/prefix_ip_collector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Tests for prefix_ip_collector"""

import unittest
from datetime import datetime
from datetime import datetime, timezone
from nav.activeipcollector.manager import find_range, get_timestamp


Expand All @@ -14,5 +14,5 @@ def test_find_range(self):
self.assertEqual(find_range('2001:700:0:251e::/64'), 0)

def test_find_timestamp(self):
ts = datetime(2012, 10, 4, 14, 30)
self.assertEqual(get_timestamp(ts), 1349353800)
ts = datetime(2012, 10, 4, 14, 30, tzinfo=timezone.utc)
self.assertEqual(get_timestamp(ts), 1349361000)

0 comments on commit 127ce87

Please sign in to comment.