時刻 +0900」) */ final class Person { /** * @param string $name 名前 * @param string $email メールアドレス * @param int $time UNIX時刻 * @param string $tz 時差(「+0900」) */ public function __construct(public string $name, public string $email, public int $time, public string $tz = '+0000') {} public static function parse(string $s): self { if (preg_match('/^(.*?) ?<([^>]*)> (-?\d+) ([+-]\d{4})$/s', $s, $m)) return new self($m[1], $m[2], (int)$m[3], $m[4]); if (preg_match('/^(.*?) ?<([^>]*)>/s', $s, $m)) return new self($m[1], $m[2], 0, '+0000'); return new self($s, '', 0, '+0000'); } /** 今の時刻で(時差はPHPの時間帯) */ public static function now(string $name, string $email): self { $t = new \DateTimeImmutable(); return new self($name, $email, $t->getTimestamp(), $t->format('O')); } public function __toString(): string { return "{$this->name} <{$this->email}> {$this->time} {$this->tz}"; } /** 時差を付けた日時 */ public function date(): \DateTimeImmutable { return (new \DateTimeImmutable('@'.$this->time))->setTimezone(new \DateTimeZone($this->tz)); } }