Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix activeipcollector get_timestamp function implementation and its broken timezone-naive test #2831

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
hmpf marked this conversation as resolved.
Show resolved Hide resolved
Loading