Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useful.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import datetime
  8. from pyasn1 import error
  9. from pyasn1.compat import dateandtime
  10. from pyasn1.compat import string
  11. from pyasn1.type import char
  12. from pyasn1.type import tag
  13. from pyasn1.type import univ
  14. __all__ = ['ObjectDescriptor', 'GeneralizedTime', 'UTCTime']
  15. NoValue = univ.NoValue
  16. noValue = univ.noValue
  17. class ObjectDescriptor(char.GraphicString):
  18. __doc__ = char.GraphicString.__doc__
  19. #: Default :py:class:`~pyasn1.type.tag.TagSet` object for |ASN.1| objects
  20. tagSet = char.GraphicString.tagSet.tagImplicitly(
  21. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 7)
  22. )
  23. # Optimization for faster codec lookup
  24. typeId = char.GraphicString.getTypeId()
  25. class TimeMixIn(object):
  26. _yearsDigits = 4
  27. _hasSubsecond = False
  28. _optionalMinutes = False
  29. _shortTZ = False
  30. class FixedOffset(datetime.tzinfo):
  31. """Fixed offset in minutes east from UTC."""
  32. # defaulted arguments required
  33. # https: // docs.python.org / 2.3 / lib / datetime - tzinfo.html
  34. def __init__(self, offset=0, name='UTC'):
  35. self.__offset = datetime.timedelta(minutes=offset)
  36. self.__name = name
  37. def utcoffset(self, dt):
  38. return self.__offset
  39. def tzname(self, dt):
  40. return self.__name
  41. def dst(self, dt):
  42. return datetime.timedelta(0)
  43. UTC = FixedOffset()
  44. @property
  45. def asDateTime(self):
  46. """Create :py:class:`datetime.datetime` object from a |ASN.1| object.
  47. Returns
  48. -------
  49. :
  50. new instance of :py:class:`datetime.datetime` object
  51. """
  52. text = str(self)
  53. if text.endswith('Z'):
  54. tzinfo = TimeMixIn.UTC
  55. text = text[:-1]
  56. elif '-' in text or '+' in text:
  57. if '+' in text:
  58. text, plusminus, tz = string.partition(text, '+')
  59. else:
  60. text, plusminus, tz = string.partition(text, '-')
  61. if self._shortTZ and len(tz) == 2:
  62. tz += '00'
  63. if len(tz) != 4:
  64. raise error.PyAsn1Error('malformed time zone offset %s' % tz)
  65. try:
  66. minutes = int(tz[:2]) * 60 + int(tz[2:])
  67. if plusminus == '-':
  68. minutes *= -1
  69. except ValueError:
  70. raise error.PyAsn1Error('unknown time specification %s' % self)
  71. tzinfo = TimeMixIn.FixedOffset(minutes, '?')
  72. else:
  73. tzinfo = None
  74. if '.' in text or ',' in text:
  75. if '.' in text:
  76. text, _, ms = string.partition(text, '.')
  77. else:
  78. text, _, ms = string.partition(text, ',')
  79. try:
  80. ms = int(ms) * 1000
  81. except ValueError:
  82. raise error.PyAsn1Error('bad sub-second time specification %s' % self)
  83. else:
  84. ms = 0
  85. if self._optionalMinutes and len(text) - self._yearsDigits == 6:
  86. text += '0000'
  87. elif len(text) - self._yearsDigits == 8:
  88. text += '00'
  89. try:
  90. dt = dateandtime.strptime(text, self._yearsDigits == 4 and '%Y%m%d%H%M%S' or '%y%m%d%H%M%S')
  91. except ValueError:
  92. raise error.PyAsn1Error('malformed datetime format %s' % self)
  93. return dt.replace(microsecond=ms, tzinfo=tzinfo)
  94. @classmethod
  95. def fromDateTime(cls, dt):
  96. """Create |ASN.1| object from a :py:class:`datetime.datetime` object.
  97. Parameters
  98. ----------
  99. dt: :py:class:`datetime.datetime` object
  100. The `datetime.datetime` object to initialize the |ASN.1| object
  101. from
  102. Returns
  103. -------
  104. :
  105. new instance of |ASN.1| value
  106. """
  107. text = dt.strftime(cls._yearsDigits == 4 and '%Y%m%d%H%M%S' or '%y%m%d%H%M%S')
  108. if cls._hasSubsecond:
  109. text += '.%d' % (dt.microsecond // 1000)
  110. if dt.utcoffset():
  111. seconds = dt.utcoffset().seconds
  112. if seconds < 0:
  113. text += '-'
  114. else:
  115. text += '+'
  116. text += '%.2d%.2d' % (seconds // 3600, seconds % 3600)
  117. else:
  118. text += 'Z'
  119. return cls(text)
  120. class GeneralizedTime(char.VisibleString, TimeMixIn):
  121. __doc__ = char.VisibleString.__doc__
  122. #: Default :py:class:`~pyasn1.type.tag.TagSet` object for |ASN.1| objects
  123. tagSet = char.VisibleString.tagSet.tagImplicitly(
  124. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 24)
  125. )
  126. # Optimization for faster codec lookup
  127. typeId = char.VideotexString.getTypeId()
  128. _yearsDigits = 4
  129. _hasSubsecond = True
  130. _optionalMinutes = True
  131. _shortTZ = True
  132. class UTCTime(char.VisibleString, TimeMixIn):
  133. __doc__ = char.VisibleString.__doc__
  134. #: Default :py:class:`~pyasn1.type.tag.TagSet` object for |ASN.1| objects
  135. tagSet = char.VisibleString.tagSet.tagImplicitly(
  136. tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 23)
  137. )
  138. # Optimization for faster codec lookup
  139. typeId = char.VideotexString.getTypeId()
  140. _yearsDigits = 2
  141. _hasSubsecond = False
  142. _optionalMinutes = False
  143. _shortTZ = False