]> Cypherpunks.ru repositories - pyderasn.git/commitdiff
Add `totzdatetime` method to UTCTime
authorAnton Demkin <antondemkin@yandex.ru>
Wed, 25 Aug 2021 13:38:50 +0000 (16:38 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 30 Aug 2021 16:14:48 +0000 (19:14 +0300)
pyderasn.py
setup.py
tests/test_pyderasn.py

index 0a6613767d33a846cd18defc5746c72ffa4b890c..82021efab0391507dfda7e3d53402d71850cc037 100755 (executable)
@@ -1180,7 +1180,13 @@ except ImportError:  # pragma: no cover
     def colored(what, *args, **kwargs):
         return what
 
-__version__ = "9.0"
+try:
+    from dateutil.tz import tzutc
+except ImportError:  # pragma: no cover
+    tzutc = None
+
+
+__version__ = "9.1"
 
 __all__ = (
     "agg_octet_string",
@@ -5241,6 +5247,13 @@ class UTCTime(VisibleString):
     def todatetime(self):
         return self._value
 
+    def totzdatetime(self):
+        if tzutc is None:
+            raise NotImplementedError(
+                "Package python-dateutil is required to use this feature",
+            )
+        return self._value.replace(tzinfo=tzutc())
+
     def __repr__(self):
         return pp_console_row(next(self.pps()))
 
index 724a16123b4566e954f0a7bc9dd7f6bec0cf361b..c36a3735405f5b296e48c0651dfe3782fe6507a1 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -28,4 +28,7 @@ setup(
         "Topic :: Software Development :: Libraries :: Python Modules",
     ],
     py_modules=["pyderasn"],
+    extras_require={
+        "utc": ["python-dateutil~=2.8"],
+    },
 )
index 78448b86ca8eda107bf98829fab6f5db8031dc4d..f31967375c2f0e123051ebed97f071941ce22367 100644 (file)
@@ -32,7 +32,9 @@ from string import whitespace
 from time import mktime
 from time import time
 from unittest import TestCase
+from unittest.mock import patch
 
+from dateutil.tz import tzutc
 from dateutil.tz import UTC
 from hypothesis import assume
 from hypothesis import given
@@ -5062,6 +5064,17 @@ class TestUTCTime(TimeMixin, CommonMixin, TestCase):
         with self.assertRaisesRegex(ValueError, "only naive"):
             UTCTime(datetime(2000, 1, 1, 1, tzinfo=UTC))
 
+    def test_raises_if_no_dateutil(self):
+        with patch("pyderasn.tzutc", new=None):
+            with self.assertRaisesRegex(
+                NotImplementedError,
+                "Package python-dateutil is required to use this feature",
+            ):
+                UTCTime(datetime.now()).totzdatetime()
+
+    def test_tzinfo_gives_datetime_with_tzutc_tzinfo(self):
+        self.assertEqual(UTCTime(datetime.now()).totzdatetime().tzinfo, tzutc())
+
 
 @composite
 def tlv_value_strategy(draw):