Example #1
0
 /**
  * Constructs Timezone
  *
  * @param mixed $value The timezone value
  *
  * @throws DomainException When the value is not a valid timezone
  */
 public function __construct($value)
 {
     if (!Validate::isTimezone($value)) {
         $message = sprintf('Invalid timezone: %s', VarPrinter::toString($value));
         throw new DomainException($message);
     }
     if ($value instanceof DateTimeZone) {
         $value = $value->getName();
     }
     $this->value = (string) $value;
 }
Example #2
0
 /**
  * Creates instance from a timestamp and timezone
  *
  * @param int         $timestamp The timestamp
  * @param string|null $timezone  The timezone string or null for default
  *
  * @return Time
  */
 public static function fromTimestamp(int $timestamp, string $timezone = null) : Time
 {
     $timezone = $timezone ?: date_default_timezone_get();
     assert(Validate::isTimezone($timezone), sprintf('Invalid timezone: %s', $timezone));
     $time = sprintf('%d', $timestamp);
     $dateTime = DateTimeImmutable::createFromFormat('U', $time, new DateTimeZone('UTC'));
     $dateTime = $dateTime->setTimezone(new DateTimeZone($timezone));
     $hour = (int) $dateTime->format('G');
     $minute = (int) $dateTime->format('i');
     $second = (int) $dateTime->format('s');
     return new static($hour, $minute, $second);
 }
Example #3
0
 /**
  * @dataProvider invalidTimezoneProvider
  */
 public function test_that_is_timezone_returns_false_for_invalid_value($value)
 {
     $this->assertFalse(Validate::isTimezone($value));
 }
Example #4
0
 /**
  * Creates instance from a timestamp and timezone
  *
  * @param int         $timestamp The timestamp
  * @param string|null $timezone  The timezone string or null for default
  *
  * @return Date
  */
 public static function fromTimestamp(int $timestamp, string $timezone = null) : Date
 {
     $timezone = $timezone ?: date_default_timezone_get();
     assert(Validate::isTimezone($timezone), sprintf('Invalid timezone: %s', $timezone));
     $time = sprintf('%d', $timestamp);
     $dateTime = DateTimeImmutable::createFromFormat('U', $time, new DateTimeZone('UTC'));
     $dateTime = $dateTime->setTimezone(new DateTimeZone($timezone));
     $year = (int) $dateTime->format('Y');
     $month = (int) $dateTime->format('n');
     $day = (int) $dateTime->format('j');
     return new static($year, $month, $day);
 }