Calculates the offset between timezones at a given date/time

@param mixed  $ts              Date/time value
@param string $timezone        Timezone of the date/time value
$param string $target_timezone Target timezone
@return int
552540
public static function getOffset($ts = 'now', $timezone = self::UTC, $target_timezone = self::UTC) {

		if (!self::isValidTimezone($timezone) || !self::isValidTimezone($target_timezone)) {
			return 0;
		}

		$dta = new DateTime(null, new DateTimeZone($timezone));
		$dtb = new DateTime(null, new DateTimeZone($target_timezone));

		if (is_int($ts)) {
			$dta->setTimestamp($ts);
			$dtb->setTimestamp($ts);
		} else {
			$dta->modify($ts);
			$dtb->modify($ts);
		}

		return $dtb->getOffset() - $dta->getOffset();
	}
219276
public function UTCTimeZoneOffset() : int
    {
        $originDateTime = new \DateTime('now', $this);

        $utcTimeZone = new \DateTimeZone('UTC');
        $utcDateTime = new \DateTime('now', $utcTimeZone);

        return $utcTimeZone->getOffset($utcDateTime) - $this->getOffset($originDateTime);
    }
628760
public static function subtractSecond($ts = null, $num = 1)
	{
		return sfTime::subtract($ts, $num, sfTime::SECOND);
	}
261388
public static function breakdown($ts = null)
	{
		// default to now
		if ($ts === null) $ts = sfDateTimeToolkit::now();
		
		// gather individual variables
		$H = date('H', $ts); // hour
		$i = date('i', $ts); // minute
		$s = date('s', $ts); // second
		$m = date('m', $ts); // month
		$d = date('d', $ts); // day
		$Y = date('Y', $ts); // year
		
		return array($H, $i, $s, $m, $d, $Y);
	}
993520
protected function getOffsetFromUtc(string $zone)
    {
        $zone   = str_replace(['GMT','UTC'], '', $zone);
        $zone   = str_replace(':', '', $zone);

        $add    = $zone[0] == '+';
        $zone   = substr($zone, 1);

        switch (strlen($zone)) {
            case 1:
            case 2:
                return $zone * 3600 * ($add?1:-1);
            case 3:
                $hour   = substr($zone, 0, 1) * 3600;
                $minute = substr($zone, 1) * 60;
                return ($hour+$minute) * ($add?1:-1);
            case 4:
                $hour   = substr($zone, 0, 2) * 3600;
                $minute = substr($zone, 2) * 60;
                return ($hour+$minute) * ($add?1:-1);
        }

        return 0;
    }
1219729
public function set(string $timezone): bool {
    $auth = false;
    try {
      if (!empty($timezone)) {
        date_default_timezone_set($timezone);
        $auth = true;
      }
    } catch (Exception $ex) {
      throw $ex;
    }
    return $auth;
  }
1037284
public static function nextDay($ts = null, $day = sfTime::SUNDAY)
	{
		// default to now
		if ($ts === null) $ts = sfDateTimeToolkit::now();
		
		// get offsets from sunday
		$offset1 = date('w', $ts);
		$offset2 = $day;
		
		// adjust if date wraps into next week
		$offset2 += $offset2 > $offset1 ? 0 : 7;
		
		return sfTime::addDay($ts, $offset2 - $offset1);
	}
1229941
public function setDateByTimezone($timezone) {
    if (is_string($timezone)) {
      $timezone = new DateTimeZone($timezone);
    }
    $this->convertedTimezone = $timezone;
    $new_date_utc = $this->date->getTimeStamp() - $timezone->getOffset($this->date);
    $new_date = new DateTime();
    $new_date->setTimestamp($new_date_utc);
    $this->date = $new_date;
  }
390405
public static function finalDayOfWeek($ts = null)
	{
		return sfTime::subtractDay(sfTime::firstDayOfWeek(sfTime::addWeek($ts)));
	}
529564
function offset(date) {
        // Difference to Greenwich time (GMT) in hours
        var os = Math.abs(date.getTimezoneOffset());
        var h = String(Math.floor(os/60));
        var m = String(os%60);
        h.length === 1? h = '0'+h:1;
        m.length === 1? m = '0'+m:1;
        return date.getTimezoneOffset() < 0 ? '+'+h+m : '-'+h+m;
    }
1128581
protected function calculateTimezone($timezone)
    {
        if (!isset($timezone)) {
            return null;
        }
        $key = $timezone instanceof \DateTimeZone ? $timezone->getName() : $timezone;
        // lazy loading
        if (!isset(static::$timezonesInstances[$key])) {
            static::$timezonesInstances[$key] = is_string($timezone) ? new \DateTimezone($timezone) : $timezone;
        }
        return static::$timezonesInstances[$key];
    }
90767
private static function buildOffsetString(string $timeZone): string
    {
        $offset = self::createFromTimestamp(null, $timeZone)->getOffset();

        $hours = intdiv($offset, 3600);
        $minutes = intdiv($offset % 3600, 60);

        return sprintf('%s%02d:%02d', $offset >= 0 ? '+' : '-', abs($hours), abs($minutes));
    }
96670
public static long timestampCeil(TimeUnitRange range, long ts, TimeZone tz) {
		// assume that we are at UTC timezone, just for algorithm performance
		long offset = tz.getOffset(ts);
		long utcTs = ts + offset;

		switch (range) {
			case HOUR:
				return ceil(utcTs, MILLIS_PER_HOUR) - offset;
			case DAY:
				return ceil(utcTs, MILLIS_PER_DAY) - offset;
			case MONTH:
			case YEAR:
			case QUARTER:
				int days = (int) (utcTs / MILLIS_PER_DAY + EPOCH_JULIAN);
				return julianDateFloor(range, days, false) * MILLIS_PER_DAY - offset;
			default:
				// for MINUTE and SECONDS etc...,
				// it is more effective to use arithmetic Method
				throw new AssertionError(range);
		}
	}
725412
protected function timestampHasExpired($ts, $expire)
    {
        if (empty($expire)) {
            return false;
        }
        $diff = time() - $ts;
        if ($diff > $expire) {
            return true;
        }

        return false;
    }
784020
function tsToObj(ts, offset) {
  ts += offset * 60 * 1000;

  const d = new Date(ts);

  return {
    year: d.getUTCFullYear(),
    month: d.getUTCMonth() + 1,
    day: d.getUTCDate(),
    hour: d.getUTCHours(),
    minute: d.getUTCMinutes(),
    second: d.getUTCSeconds(),
    millisecond: d.getUTCMilliseconds()
  };
}
783162
public static UtcOffset parse(TimeZone timezone) {
		long offset = timezone.getOffset(System.currentTimeMillis());
		return new UtcOffset(offset);
	}
1161542
public static function compareTimezonesByOffset($a, $b) {

		$dta = new DateTime(null, new DateTimeZone($a));
		$dtb = new DateTime(null, new DateTimeZone($b));

		if ($dta->getOffset() == $dtb->getOffset()) {
			return (strcmp($a, $b) < 0) ? -1 : 1;
		}
		return ($dta->getOffset() < $dtb->getOffset()) ? -1 : 1;
	}