/** * {@inheritdoc} */ public static function toInternal($date, $format = null) { if (!in_array(strtolower($format), ['jd', 'gjd', 'geo', 'geo-centric', 'rjd', 'reduced', 'mjd', 'modified', 'tjd', 'truncated', 'djd', 'dublin', 'j1900', 'j2000', 'lilian', 'rata-die', 'mars-sol'])) { $format = 'gjd'; } if (in_array(strtolower($format), ['jd', 'gjd', 'geo', 'geo-centric'])) { $jdc = $date; } elseif (in_array(strtolower($format), ['rjd', 'reduced'])) { $jdc = BC::add($date, '2400000', 18); } elseif (in_array(strtolower($format), ['mjd', 'modified'])) { $jdc = BC::add($date, '2400000.5', 18); } elseif (in_array(strtolower($format), ['tjd', 'truncated'])) { $jdc = BC::add($date, '2440000.5', 18); } elseif (in_array(strtolower($format), ['djd', 'dublin', 'j1900'])) { $jdc = BC::add($date, '2415020', 18); } elseif (in_array(strtolower($format), ['j2000'])) { $jdc = BC::add($date, '2451545', 18); } elseif (in_array(strtolower($format), ['lilian'])) { $jdc = BC::add($date, '2299159.5', 18); } elseif (in_array(strtolower($format), ['rata-die'])) { $jdc = BC::add($date, '1721424.5', 18); } elseif (in_array(strtolower($format), ['mars-sol'])) { $jdc = BC::add(BC::mul($date, '1.02749', 18), '2405522', 18); } return Calends::toInternalFromUnix(BC::mul(BC::sub($jdc, 2440587.5, 18), 86400, 18)); }
/** * {@inheritdoc} */ public static function convert(Calends $cal) { $source = last(explode('\\', get_called_class())); $start = $source::createInstance(IntlTimeZone::getGMT(), static::$locale); $start->setTime(BC::mul($cal->getDate('unix'), 1000, 15)); $end = $source::createInstance(IntlTimeZone::getGMT(), static::$locale); $end->setTime(BC::mul($cal->getEndDate('unix'), 1000, 15)); return ['start' => $start, 'duration' => new DateInterval("PT{$cal->getDuration(0)}S"), 'end' => $end]; }
/** * @covers ::fromInternal */ public function testFromInternal() { $this->assertEquals('22 Tebeth 5730 00:00:00.000000 +00:00', Hebrew::fromInternal(['seconds' => BC::pow(2, 62), 'nano' => 0, 'atto' => 0])); $this->assertEquals('22 Tebeth 5731 00:00:00.000000 +00:00', Hebrew::fromInternal(['seconds' => BC::add(BC::pow(2, 62), BC::mul(86400, 384)), 'nano' => 0, 'atto' => 0])); }
/** * Convert a Unix timestamp to an internal TAI array * * @internal * * @param string|float|integer $stamp Unix timestamp to convert into the * internal TAI array representation * @return string[] */ public static function toInternalFromUnix($stamp) { $stamp = is_null($stamp) ? microtime(true) : (is_numeric($stamp) ? $stamp : 0); $time = []; if (BC::comp($stamp, BC::sub(0, BC::pow(2, 62, 18), 18), 18) === -1) { $stamp = BC::sub(0, BC::pow(2, 62, 18), 18); } elseif (BC::comp($stamp, BC::sub(BC::pow(2, 63, 18), BC::pow(2, 62, 18), 18), 18) >= 0) { $stamp = BC::sub(BC::sub(BC::pow(2, 63, 18), BC::pow(2, 62, 18), 18), BC::pow(10, -18, 18), 18); } $unix_seconds = BC::div($stamp, 1, 0); $time['seconds'] = BC::add($unix_seconds, BC::pow(2, 62, 18), 0); $time['nano'] = gmp_strval(gmp_abs(BC::mul(BC::sub($stamp, $unix_seconds, 18), BC::pow(10, 9, 18), 0)), 10); $time['atto'] = gmp_strval(gmp_abs(BC::mul(BC::sub(BC::mul(BC::sub($stamp, $unix_seconds, 18), BC::pow(10, 9, 18), 9), BC::mul(BC::comp($unix_seconds, 0, 18), $time['nano'], 18), 18), BC::pow(10, 9, 18), 0)), 10); return $time; }
/** * {@inheritdoc} */ public static function toInternal($date, $format = null) { $greg = new DateTime($date); return Calends::toInternalFromUnix(BC::add(BC::mul(BC::sub(\JulianToJD($greg->format('n'), $greg->format('j'), $greg->format('Y')), 2440587, 18), 86400, 18), BC::mod($greg->getTimestamp(), 86400, 18), 18)); }
/** * Retrieve the calendar's date at the Unix Epoch * * @return array */ protected function getEpochUnitArray($positive = true) { $unitArray = []; foreach ($this->units()->where('is_auxiliary', 0)->get() as $unit) { $unitArray[$unit->internal_name] = $positive ? $unit->unix_epoch : BC::mul(BC::sub($unit->unix_epoch, $unit->uses_zero ? 0 : 1, 18), -1, 18); } return $unitArray; }
/** * {@inheritdoc} */ public static function toInternal($date, $format = null) { $greg = new DateTime(str_replace(['6L', '7L'], ['06', '07'], str_ireplace(array_values(static::$months), array_keys(static::$months), $date))); return Calends::toInternalFromUnix(BC::add(BC::mul(BC::sub(\JewishToJD($greg->format('n'), $greg->format('j'), $greg->format('Y')), 2440587, 18), 86400, 18), BC::mod($greg->getTimestamp(), 86400, 18), 18)); }
protected function scaleReduce($value) { if (!is_null($this->scale_amount)) { $scaled = BC::parse("{$value} " . ($this->scale_inverse ? '/' : '*') . " {$this->scale_amount}", null, 18); } else { $lengths = $this->lengths()->get(); $lCount = $lengths->count(); if ($lCount == 0) { $scaled = 0; } else { $lSum = $lengths->sum('scale_amount'); $adjLoops = BC::div($value, $lCount, 0); $adjUnits = BC::modfrac($value, $lCount, 18); $adjRemains = BC::modfrac($adjUnits, 1, 18); $adjUnits = BC::add($adjUnits, $this->getEpochValue(), 0); $adjustment = BC::mul($adjLoops, $lSum, 18); for ($lNum = $this->getEpochValue(); BC::comp($lNum, $adjUnits) < 0; $lNum = BC::parse("({$lNum} + 1) % {$lCount}", null, 0)) { $adjustment = BC::add($adjustment, $lengths[$lNum]->scale_amount, 18); } $scaled = BC::parse("{$adjustment} + ({$adjRemains} * {lAmount})", ['lAmount' => $lengths[BC::parse("({$lNum} - 1 + {$lCount}) % {$lCount}", null, 0)]->scale_amount], 18); } } return $scaled; }