示例#1
0
 public function parseValue($value)
 {
     preg_match_all('/%([^$]+)\\$/', $this->format_string, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
     $i = count($matches[1]);
     $values = [];
     foreach (array_reverse($matches[1]) as $match) {
         $fragmentType = strtolower(ltrim(strrchr(get_class($this->fragment), '\\'), '\\'));
         // era or unit
         $fragmentName = $this->fragment->internal_name;
         $partBegin = stripos($match[0], '{');
         if ($partBegin >= 0) {
             $partEnd = stripos($match[0], '}', $partBegin);
             $fragmentPart = trim(substr($match[0], $partBegin, $partEnd), '{}');
         } else {
             $fragmentPart = 'unknown';
         }
         $type = "{$fragmentType}.{$fragmentName}.{$fragmentPart}";
         if (with($txtObj = $this->texts()->where('fragment_text', $value))->count() > 0) {
             $value = $txtObj->first()->fragment_value;
         }
         $invert = preg_replace('/##(\\d+|\\{[^}]+\\})/', '*${1}+{epoch}%%${1}', str_replace(['\\*', '-%', '\\', '+', '-', ':', '/', '*', ':', '%%', '%', '__', '$$'], ['__', '$$', '##', ':', '+', '-', ':', '/', '*', '+{epoch}\\*', '+{epoch}-%', '+{epoch}%%', '+{epoch}%'], $match[0]));
         $output = BC::parse($invert, ['epoch' => $this->fragment->getEpochValue(), $fragmentPart => $value], 0);
         $values[$i] = [$type, $output];
         $i--;
     }
     return $values;
 }
示例#2
0
 /**
  * {@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];
 }
示例#3
0
 public function unitValue(array $parsed)
 {
     $raw = Arr::get($parsed, 'value', 0);
     $code = Arr::get($parsed, 'code', $this->default_range);
     $range = $this->ranges()->where('range_code', $code)->where(function ($query) use($raw) {
         // @codeCoverageIgnoreStart
         $query->where(function ($query) use($raw) {
             $query->where('direction', 'asc')->where('start_value', '<=', "{$raw} - start_display")->where('end_value', '>=', "{$raw} - start_display");
         })->orWhere(function ($query) use($raw) {
             $query->where('direction', 'asc')->where('start_value', '<=', "{$raw} - start_display")->whereNull('end_value');
         })->orWhere(function ($query) use($raw) {
             $query->where('direction', 'desc')->where('start_value', '>=', "start_display - {$raw}")->where('end_value', '<=', "start_display - {$raw}");
         })->orWhere(function ($query) use($raw) {
             $query->where('direction', 'desc')->where('start_value', '>=', "start_display - {$raw}")->whereNull('end_value');
         })->orWhere(function ($query) use($raw) {
             $query->whereRaw('1=1');
         });
         // @codeCoverageIgnoreEnd
     })->first();
     $output = [$this->unit->internal_name, BC::parse("({$range->start_value}" . ($range->direction == 'asc' ? '-' : '+') . "{$range->start_display})" . ($range->direction == 'asc' ? '+' : '-') . $raw, null, 0)];
     return $output;
 }
示例#4
0
 /**
  * Compares the start time, end time, or duration of two Calends objects
  *
  * @api
  *
  * @param Calends $a One of the two Calends objects to compare times from
  * @param Calends $b One of the two Calends objects to compare times from
  * @param string $mode One of start, end, start-end, end-start, or duration
  * @return integer Values are -1 if a < b; 0 if a == b; +1 if a > b
  **/
 public static function compare(Calends $a, Calends $b, $mode = 'start')
 {
     $times = static::getTimesByMode($a, $b, $mode);
     return BC::comp($times[0], $times[1], 18);
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public static function convert(Calends $cal)
 {
     return ['start' => Source::createFromFormat('U.u', BC::add($cal->getDate('unix'), 0, 6)), 'duration' => new DateInterval("PT{$cal->getDuration(0)}S"), 'end' => Source::createFromFormat('U.u', BC::add($cal->getEndDate('unix'), 0, 6))];
 }
示例#6
0
 /**
  * @Given a scale of :arg1
  */
 public function aScaleOf($arg1)
 {
     BC::scale($arg1);
     bcscale($arg1);
 }
示例#7
0
 /**
  * @covers ::gap
  */
 public function testGap()
 {
     $test = Calends::create(['start' => -86400, 'end' => 0], 'unix')->gap(Calends::create(['start' => 86400, 'end' => 172800], 'unix'));
     $this->assertInstanceOf('Danhunsaker\\Calends\\Calends', $test);
     $this->assertAttributeEquals(['seconds' => BC::pow(2, 62), 'nano' => '0', 'atto' => '0'], 'internalTime', $test);
     $this->assertAttributeEquals(86400, 'duration', $test);
     $this->assertAttributeEquals(['seconds' => BC::add(BC::pow(2, 62), 86400), 'nano' => '0', 'atto' => '0'], 'endTime', $test);
     $this->setExpectedException('Danhunsaker\\Calends\\InvalidCompositeRangeException', "The ranges given overlap - they have no gap.");
     $test->gap(Calends::create(43200, 'unix'));
 }
示例#8
0
 /**
  * @covers ::offset
  */
 public function testOffset()
 {
     $this->assertEquals(['seconds' => BC::add(BC::pow(2, 62), 86400), 'nano' => 0, 'atto' => 0], Hebrew::offset(['seconds' => BC::pow(2, 62), 'nano' => 0, 'atto' => 0], '1 day'));
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public static function fromInternal($stamp, $format = null)
 {
     $date = Calends::fromInternalToUnix($stamp);
     return date_create_from_format('n/j/Y', \JDToJulian(BC::add(BC::div($date, 86400, 18), 2440587.5, 0)))->format('D, d M Y') . ' ' . date_create_from_format('U.u', BC::add(0, $date, 6))->format('H:i:s.u P');
 }
示例#10
0
 /**
  * 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;
 }
示例#11
0
 /**
  * @covers ::offset
  */
 public function testOffset()
 {
     $this->assertEquals(['seconds' => BC::add(BC::pow(2, 62), 86400), 'nano' => 0, 'atto' => 0], JulianDayCount::offset(['seconds' => BC::pow(2, 62), 'nano' => 0, 'atto' => 0], 1));
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public static function offset($stamp, $offset)
 {
     return Calends::toInternalFromUnix(date_create_from_format('Y-m-d H:i:s.u P', date('Y-m-d H:i:s.u P', BC::round(Calends::fromInternalToUnix($stamp), 6)))->modify($offset)->format('U.u'));
 }
示例#13
0
 /**
  * {@inheritdoc}
  */
 public static function offset($stamp, $offset)
 {
     $date = date_create_from_format('U.u', BC::add(Calends::fromInternalToUnix($stamp), 0, 6))->modify($offset);
     return Calends::toInternalFromUnix($date->getTimestamp());
 }
示例#14
0
 public function getFormatArgs(array $units)
 {
     $raw = BC::add(array_key_exists($this->internal_name, $units) ? $units[$this->internal_name] : 0, 0, 0);
     if (is_null($this->scale_amount)) {
         if (with($lenObj = $this->lengths()->where('unit_value', $raw))->count() > 0) {
             $length = $lenObj->first()->scale_amount;
         } else {
             $length = 0;
         }
     } else {
         $length = $this->scale_amount;
     }
     return ['length' => $length, 'value' => $raw];
 }
示例#15
0
 /**
  * {@inheritdoc}
  */
 public static function offset($stamp, $offset)
 {
     return Calends::toInternalFromUnix(BC::add(Calends::fromInternalToUnix($stamp), BC::add(Calends::fromInternalToUnix(static::toInternal($offset)), 0x4000000000000000, 18), 18));
 }
示例#16
0
 public function it_should_modify_dates()
 {
     BC::scale(18);
     $time = microtime(true);
     $start = BC::sub($time, BC::mod($time, 86400), 0);
     $end = BC::add($start, 86400);
     $this->beConstructedWith(['start' => $start, 'end' => $end]);
     $today = $this->getWrappedObject();
     $next24hrs = $today->setDate($time)->setEndDate(BC::add($time, 86400));
     $yesterday = $today->previous('1 day', 'gregorian');
     $subtracted = $this->subtractFromEnd('50', 'unix');
     $subtracted->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $subtracted->getEndDate()->shouldBeLike(BC::sub($this->getWrappedObject()->getEndDate(), 50));
     $duration = $this->setDuration('50', 'unix');
     $duration->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $duration->getEndDate()->shouldBeLike(BC::add($this->getWrappedObject()->getDate(), 50));
     $endDuration = $this->setDurationFromEnd('50', 'unix');
     $endDuration->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $endDuration->getDate()->shouldBeLike(BC::sub($this->getWrappedObject()->getEndDate(), 50));
     $merged = $this->merge($yesterday);
     $merged->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $merged->getDate()->shouldBeLike($yesterday->getDate());
     $merged->getEndDate()->shouldBeLike($this->getWrappedObject()->getEndDate());
     $this->shouldThrow('Danhunsaker\\Calends\\InvalidCompositeRangeException')->duringIntersect($yesterday);
     $intersection = $this->intersect($next24hrs);
     $intersection->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $intersection->getDate()->shouldBeLike($next24hrs->getDate());
     $intersection->getEndDate()->shouldBeLike($this->getWrappedObject()->getEndDate());
     $this->shouldThrow('Danhunsaker\\Calends\\InvalidCompositeRangeException')->duringGap($next24hrs);
     $gap = $this->gap($yesterday);
     $gap->shouldHaveType('Danhunsaker\\Calends\\Calends');
     $gap->getDate()->shouldBeLike($yesterday->getEndDate());
     $gap->getEndDate()->shouldBeLike($this->getWrappedObject()->getDate());
 }
示例#17
0
 /**
  * {@inheritdoc}
  */
 public static function offset($stamp, $offset)
 {
     return static::toInternal(BC::add(static::fromInternal($stamp), $offset, 18));
 }
示例#18
0
文件: BC.php 项目: danhunsaker/bcmath
 public static function getScale($scale)
 {
     return parent::getScale($scale);
 }
示例#19
0
 /**
  * {@inheritdoc}
  */
 public static function convert(Calends $cal)
 {
     $dtClass = class_exists('\\DateTimeImmutable') ? '\\DateTimeImmutable' : '\\DateTime';
     return new Source($dtClass::createFromFormat('U.u', BC::add($cal->getDate('unix'), 0, 6)), $dtClass::createFromFormat('U.u', BC::add($cal->getEndDate('unix'), 0, 6)));
 }
示例#20
0
 /**
  * @covers ::offset
  */
 public function testOffset()
 {
     $this->assertEquals(['seconds' => BC::add(BC::pow(2, 62), 86400), 'nano' => 0, 'atto' => 0], Calendar::find(1)->offset(['seconds' => BC::pow(2, 62), 'nano' => 0, 'atto' => 0], '1 day'));
 }