Exemplo n.º 1
0
 /**
  * Constructs Timezone
  *
  * @internal
  *
  * @param mixed $value The timezone value
  *
  * @throws DomainException When the value is not a valid timezone
  */
 private function __construct($value)
 {
     if (!Test::isTimezone($value)) {
         $message = sprintf('Invalid timezone: %s', VarPrinter::toString($value));
         throw DomainException::create($message);
     }
     if ($value instanceof DateTimeZone) {
         $value = $value->getName();
     }
     $this->value = (string) $value;
 }
Exemplo n.º 2
0
 /**
  * Creates instance from a timestamp and timezone
  *
  * @param int         $timestamp The timestamp
  * @param int         $micro     The microsecond
  * @param string|null $timezone  The timezone string or null for default
  *
  * @return DateTime
  */
 public static function fromTimestamp($timestamp, $micro = 0, $timezone = null)
 {
     $timezone = $timezone === null ? date_default_timezone_get() : (string) $timezone;
     assert(Test::isTimezone($timezone), sprintf('Invalid timezone: %s', $timezone));
     $time = sprintf('%d.%06d', (int) $timestamp, (int) $micro);
     $dateTime = DateTimeImmutable::createFromFormat('U.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');
     $hour = (int) $dateTime->format('G');
     $minute = (int) $dateTime->format('i');
     $second = (int) $dateTime->format('s');
     $micro = (int) $dateTime->format('u');
     return new self(Date::create($year, $month, $day), Time::create($hour, $minute, $second, $micro), Timezone::create($timezone));
 }
Exemplo n.º 3
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($timestamp, $timezone = null)
 {
     $timezone = $timezone === null ? date_default_timezone_get() : (string) $timezone;
     assert(Test::isTimezone($timezone), sprintf('Invalid timezone: %s', $timezone));
     $time = sprintf('%d', (int) $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 self($year, $month, $day);
 }