3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); final class TimeFormatter { private const TIME_FORMAT = 'Y-m-d\TH:i:s\Z'; private const TIME_ZONE = 'UTC'; public static function encode(?DateTimeImmutable $time): ?string { if ($time === null) { return null; } return $time->setTimezone(new DateTimeZone(self::TIME_ZONE))->format(self::TIME_FORMAT); } public static function decode(?string $time): ?DateTimeImmutable { if ($time === null) { return null; } $decoded = DateTimeImmutable::createFromFormat( str_contains($time, '.') ? DateTimeInterface::RFC3339_EXTENDED : DateTimeInterface::RFC3339, strtoupper($time), new DateTimeZone(self::TIME_ZONE) ); if ($decoded === false) { throw new ValueError( \sprintf('%s(): Argument #1 ($time) is not a valid RFC3339 timestamp', __METHOD__) ); } return $decoded; } } var_dump(TimeFormatter::encode(TimeFormatter::decode('1985-04-12T23:20:50.52Z'))); var_dump(TimeFormatter::encode(TimeFormatter::decode('1985-04-13T01:20:50+02:00')));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mkQKd
function name:  (null)
number of ops:  19
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_STATIC_METHOD_CALL                                      'TimeFormatter', 'encode'
          2        INIT_STATIC_METHOD_CALL                                      'TimeFormatter', 'decode'
          3        SEND_VAL                                                     '1985-04-12T23%3A20%3A50.52Z'
          4        DO_FCALL                                          0  $0      
          5        SEND_VAR                                                     $0
          6        DO_FCALL                                          0  $1      
          7        SEND_VAR                                                     $1
          8        DO_ICALL                                                     
   42     9        INIT_FCALL                                                   'var_dump'
         10        INIT_STATIC_METHOD_CALL                                      'TimeFormatter', 'encode'
         11        INIT_STATIC_METHOD_CALL                                      'TimeFormatter', 'decode'
         12        SEND_VAL                                                     '1985-04-13T01%3A20%3A50%2B02%3A00'
         13        DO_FCALL                                          0  $3      
         14        SEND_VAR                                                     $3
         15        DO_FCALL                                          0  $4      
         16        SEND_VAR                                                     $4
         17        DO_ICALL                                                     
         18      > RETURN                                                       1

Class TimeFormatter:
Function encode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mkQKd
function name:  encode
number of ops:  17
compiled vars:  !0 = $time
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   12     1        TYPE_CHECK                                        2          !0
          2      > JMPZ                                                         ~1, ->4
   13     3    > > RETURN                                                       null
   16     4    >   INIT_METHOD_CALL                                             !0, 'setTimezone'
          5        NEW                                                  $2      'DateTimeZone'
          6        SEND_VAL_EX                                                  'UTC'
          7        DO_FCALL                                          0          
          8        SEND_VAR_NO_REF_EX                                           $2
          9        DO_FCALL                                          0  $4      
         10        INIT_METHOD_CALL                                             $4, 'format'
         11        SEND_VAL_EX                                                  'Y-m-d%5CTH%3Ai%3As%5CZ'
         12        DO_FCALL                                          0  $5      
         13        VERIFY_RETURN_TYPE                                           $5
         14      > RETURN                                                       $5
   17    15*       VERIFY_RETURN_TYPE                                           
         16*     > RETURN                                                       null

End of function encode

Function decode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 29
Branch analysis from position: 23
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 29
Branch analysis from position: 23
Branch analysis from position: 29
filename:       /in/mkQKd
function name:  decode
number of ops:  33
compiled vars:  !0 = $time, !1 = $decoded
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   RECV                                                 !0      
   21     1        TYPE_CHECK                                        2          !0
          2      > JMPZ                                                         ~2, ->4
   22     3    > > RETURN                                                       null
   25     4    >   INIT_STATIC_METHOD_CALL                                      'DateTimeImmutable', 'createFromFormat'
   26     5        FRAMELESS_ICALL_2                str_contains        ~3      !0, '.'
          6      > JMPZ                                                         ~3, ->9
          7    >   QM_ASSIGN                                            ~4      'Y-m-d%5CTH%3Ai%3As.vP'
          8      > JMP                                                          ->10
          9    >   QM_ASSIGN                                            ~4      'Y-m-d%5CTH%3Ai%3AsP'
         10    >   SEND_VAL                                                     ~4
   27    11        INIT_FCALL                                                   'strtoupper'
         12        SEND_VAR                                                     !0
         13        DO_ICALL                                             $5      
         14        SEND_VAR                                                     $5
   28    15        NEW                                                  $6      'DateTimeZone'
         16        SEND_VAL_EX                                                  'UTC'
         17        DO_FCALL                                          0          
         18        SEND_VAR                                                     $6
   25    19        DO_FCALL                                          0  $8      
         20        ASSIGN                                                       !1, $8
   31    21        TYPE_CHECK                                        4          !1
         22      > JMPZ                                                         ~10, ->29
   32    23    >   NEW                                                  $11     'ValueError'
   33    24        NOP                                                          
         25        FAST_CONCAT                                          ~12     'TimeFormatter%3A%3Adecode', '%28%29%3A+Argument+%231+%28%24time%29+is+not+a+valid+RFC3339+timestamp'
         26        SEND_VAL_EX                                                  ~12
   32    27        DO_FCALL                                          0          
   33    28      > THROW                                             0          $11
   37    29    >   VERIFY_RETURN_TYPE                                           !1
         30      > RETURN                                                       !1
   38    31*       VERIFY_RETURN_TYPE                                           
         32*     > RETURN                                                       null

End of function decode

End of class TimeFormatter.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
116.19 ms | 506 KiB | 11 Q