| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | * with JSON documents, and with JMESPath. |
| 12 | 12 | */ |
| 13 | 13 | class DateTimeResult extends \DateTime implements \JsonSerializable |
| 14 | 14 | { |
| 15 | + private const ISO8601_NANOSECOND_REGEX = '/^(.*\\.\\d{6})(\\d{1,3})(Z|[+-]\\d{2}:\\d{2})?$/'; | |
| 15 | 16 | /** |
| 16 | 17 | * Create a new DateTimeResult from a unix timestamp. |
| 17 | 18 | * The Unix epoch (or Unix time or POSIX time or Unix |
| 18 | 19 | * timestamp) is the number of seconds that have elapsed since |
| @@ -44,8 +45,13 @@ | ||
| 44 | 45 | public static function fromISO8601($iso8601Timestamp) |
| 45 | 46 | { |
| 46 | 47 | if (\is_numeric($iso8601Timestamp) || !\is_string($iso8601Timestamp)) { |
| 47 | 48 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601'); |
| 49 | + } | |
| 50 | + // Prior to 8.0.10, nanosecond precision is not supported | |
| 51 | + // Reduces to microsecond precision if nanosecond precision is detected | |
| 52 | + if (\PHP_VERSION_ID < 80010 && \preg_match(self::ISO8601_NANOSECOND_REGEX, $iso8601Timestamp, $matches)) { | |
| 53 | + $iso8601Timestamp = $matches[1] . ($matches[3] ?? ''); | |
| 48 | 54 | } |
| 49 | 55 | return new DateTimeResult($iso8601Timestamp); |
| 50 | 56 | } |
| 51 | 57 | /** |