Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
timestamp: Add function to convert a datetime to precise Unix timestamp.
Hemanth V. Allurid5f42e2722ac
diff --git a/zerver/lib/timestamp.py b/zerver/lib/timestamp.pyindex dcbc3dee499..80993c33e01 100644--- a/zerver/lib/timestamp.py+++ b/zerver/lib/timestamp.py@@ -1,5 +1,6 @@import calendarimport datetime+import timeclass TimezoneNotUTCException(Exception):@@ -40,3 +41,7 @@ def timestamp_to_datetime(timestamp: float) -> datetime.datetime:def datetime_to_timestamp(dt: datetime.datetime) -> int:verify_UTC(dt)return calendar.timegm(dt.timetuple())++def datetime_to_precise_timestamp(dt: datetime.datetime) -> float:+ verify_UTC(dt)+ return time.mktime(dt.timetuple()) + dt.microsecond/1000000diff --git a/zerver/tests/test_timestamp.py b/zerver/tests/test_timestamp.pyindex 5460d510b5d..46853bd610e 100644--- a/zerver/tests/test_timestamp.py+++ b/zerver/tests/test_timestamp.py@@ -1,3 +1,4 @@+import timefrom datetime import timedelta, timezonefrom dateutil import parser@@ -7,6 +8,7 @@ from zerver.lib.timestamp import (TimezoneNotUTCException,ceiling_to_hour,convert_to_UTC,+ datetime_to_precise_timestamp,datetime_to_timestamp,floor_to_day,floor_to_hour,@@ -42,3 +44,9 @@ class TestTimestamp(ZulipTestCase):for function in [floor_to_hour, floor_to_day, ceiling_to_hour, ceiling_to_hour]:with self.assertRaises(TimezoneNotUTCException):function(non_utc_datetime)++ def test_datetime_to_precise_timestamp(self) -> None:+ timestamp = round(time.time(), 6)+ dt = timestamp_to_datetime(timestamp)+ post_conversion_timestamp = datetime_to_precise_timestamp(dt)+ self.assertEqual(timestamp, post_conversion_timestamp)