]> Cypherpunks.ru repositories - pyderasn.git/blobdiff - pyderasn.py
Add `totzdatetime` method to UTCTime
[pyderasn.git] / pyderasn.py
index bb62acb437c203fa31ee66e03d6789e302a59fdd..82021efab0391507dfda7e3d53402d71850cc037 100755 (executable)
@@ -1159,8 +1159,6 @@ Now you can print only the specified tree, for example signature algorithm::
 """
 
 from array import array
-from codecs import getdecoder
-from codecs import getencoder
 from collections import namedtuple
 from collections import OrderedDict
 from copy import copy
@@ -1182,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",
@@ -1441,20 +1445,16 @@ class BoundsError(ASN1Error):
 # Basic coders
 ########################################################################
 
-_hexdecoder = getdecoder("hex")
-_hexencoder = getencoder("hex")
-
-
 def hexdec(data):
     """Binary data to hexadecimal string convert
     """
-    return _hexdecoder(data)[0]
+    return bytes.fromhex(data)
 
 
 def hexenc(data):
     """Hexadecimal string to binary data convert
     """
-    return _hexencoder(data)[0].decode("ascii")
+    return data.hex()
 
 
 def int_bytes_len(num, byte_len=8):
@@ -5247,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()))