| 1: | <?php |
| 2: | declare(strict_types=1); |
| 3: | |
| 4: | namespace om\RRule; |
| 5: | |
| 6: | use DateTime; |
| 7: | use DateTimeZone; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | final class LocalTime { |
| 15: | |
| 16: | |
| 17: | |
| 18: | public static function timestamp(DateTimeZone $timezone, int $local): int { |
| 19: | static $probe; |
| 20: | $probe ??= new DateTime('@0'); |
| 21: | $offsetAt = static fn(int $timestamp): int => $timezone->getOffset($probe->setTimestamp($timestamp)); |
| 22: | |
| 23: | |
| 24: | $before = $offsetAt($local - 2 * 86400); |
| 25: | $after = $offsetAt($local + 2 * 86400); |
| 26: | if ($before === $after && $offsetAt($local - $before) === $before) { |
| 27: | return $local - $before; |
| 28: | } |
| 29: | |
| 30: | $valid = []; |
| 31: | foreach ([$before, $after] as $offset) { |
| 32: | $timestamp = $local - $offset; |
| 33: | if ($offsetAt($timestamp) === $offset) { |
| 34: | $valid[] = $timestamp; |
| 35: | } |
| 36: | } |
| 37: | if ($valid !== []) { |
| 38: | return min($valid); |
| 39: | } |
| 40: | |
| 41: | return $local - $before; |
| 42: | } |
| 43: | } |
| 44: | |