UTCTime.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the PHPASN1 library.
  4. *
  5. * Copyright © Friedrich Große <friedrich.grosse@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FG\ASN1\Universal;
  11. use FG\ASN1\AbstractTime;
  12. use FG\ASN1\Parsable;
  13. use FG\ASN1\Identifier;
  14. use FG\ASN1\Exception\ParserException;
  15. /**
  16. * This ASN.1 universal type contains the calendar date and time.
  17. *
  18. * The precision is one minute or one second and optionally a
  19. * local time differential from coordinated universal time.
  20. *
  21. * Decoding of this type will accept the Basic Encoding Rules (BER)
  22. * The encoding will comply with the Distinguished Encoding Rules (DER).
  23. */
  24. class UTCTime extends AbstractTime implements Parsable
  25. {
  26. public function getType()
  27. {
  28. return Identifier::UTC_TIME;
  29. }
  30. protected function calculateContentLength()
  31. {
  32. return 13; // Content is a string o the following format: YYMMDDhhmmssZ (13 octets)
  33. }
  34. protected function getEncodedValue()
  35. {
  36. return $this->value->format('ymdHis').'Z';
  37. }
  38. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  39. {
  40. self::parseIdentifier($binaryData[$offsetIndex], Identifier::UTC_TIME, $offsetIndex++);
  41. $contentLength = self::parseContentLength($binaryData, $offsetIndex, 11);
  42. $format = 'ymdGi';
  43. $dateTimeString = substr($binaryData, $offsetIndex, 10);
  44. $offsetIndex += 10;
  45. // extract optional seconds part
  46. if ($binaryData[$offsetIndex] != 'Z'
  47. && $binaryData[$offsetIndex] != '+'
  48. && $binaryData[$offsetIndex] != '-') {
  49. $dateTimeString .= substr($binaryData, $offsetIndex, 2);
  50. $offsetIndex += 2;
  51. $format .= 's';
  52. }
  53. $dateTime = \DateTime::createFromFormat($format, $dateTimeString, new \DateTimeZone('UTC'));
  54. // extract time zone settings
  55. if ($binaryData[$offsetIndex] == '+'
  56. || $binaryData[$offsetIndex] == '-') {
  57. $dateTime = static::extractTimeZoneData($binaryData, $offsetIndex, $dateTime);
  58. } elseif ($binaryData[$offsetIndex++] != 'Z') {
  59. throw new ParserException('Invalid UTC String', $offsetIndex);
  60. }
  61. $parsedObject = new self($dateTime);
  62. $parsedObject->setContentLength($contentLength);
  63. return $parsedObject;
  64. }
  65. }