Example #1
0
 public function it_should_convert_between_internal_and_unix()
 {
     $this::toInternalFromUnix(0)->shouldHaveKey('seconds');
     $this::toInternalFromUnix(BC::sub(-1, BC::pow(2, 62)))->shouldHaveKeyWithValue('seconds', '0');
     $this::toInternalFromUnix(BC::sub(BC::pow(2, 63), BC::pow(2, 62)))->shouldHaveKeyWithValue('seconds', BC::sub(BC::pow(2, 63), 1, 0));
     $this::fromInternalToUnix(['seconds' => 0, 'nano' => 0, 'atto' => 0])->shouldBeString();
     $this::fromInternalToUnix(['seconds' => 0, 'nano' => 0, 'atto' => 0])->shouldBeLike('-4611686018427387904');
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public static function toInternal($date, $format = null)
 {
     if (!in_array(strtolower($format), ['tai64', 'tai64n', 'tai64na', 'numeric'])) {
         $format = 'tai64na';
     }
     if (substr(strtolower($format), 0, 5) == 'tai64') {
         $date = str_pad(str_pad($date, 16, '0', STR_PAD_LEFT), 32, '0', STR_PAD_RIGHT);
         $time = ['seconds' => gmp_strval(gmp_init('0x' . substr($date, 0, 16), 16), 10), 'nano' => gmp_strval(gmp_init('0x' . substr($date, 16, 8), 16), 10), 'atto' => gmp_strval(gmp_init('0x' . substr($date, 24, 8), 16), 10)];
     } elseif (strtolower($format) == 'numeric') {
         list($time['seconds'], $frac) = explode('.', "{$date}.");
         $frac = str_pad($frac, 18, '0', STR_PAD_RIGHT);
         $time['nano'] = substr($frac, 0, 9);
         $time['atto'] = substr($frac, 9, 9);
     }
     if (BC::comp($time['seconds'], BC::pow(2, 63, 18), 18) >= 0) {
         $time = ['seconds' => BC::sub(BC::pow(2, 63, 18), 1, 0), 'nano' => '999999999', 'atto' => '999999999'];
     } elseif (BC::comp($time['seconds'], 0, 18) <= 0) {
         $time = ['seconds' => '0', 'nano' => '0', 'atto' => '0'];
     }
     return $time;
 }
Example #3
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'));
 }
Example #4
0
 /**
  * Convert an internal TAI array to a Unix timestamp
  *
  * @internal
  *
  * @param string[] $stamp The internal TAI array representation to convert
  *        into a Unix timestamp
  * @return string|float|integer
  */
 public static function fromInternalToUnix($time)
 {
     return BC::add(BC::sub($time['seconds'], BC::pow(2, 62, 18), 0), BC::div(BC::add(BC::div($time['atto'], BC::pow(10, 9, 18), 9), $time['nano'], 9), BC::pow(10, 9, 18), 18), 18);
 }
 /**
  * @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));
 }
Example #6
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'));
 }
Example #7
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'));
 }