Skip to content

Commit

Permalink
Merge pull request #37 from alhoqbani/dev
Browse files Browse the repository at this point in the history
Update date correction tests and docs.
  • Loading branch information
alhoqbani committed Aug 2, 2017
2 parents 2454ecd 710b1a3 commit c13d6c7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/ArDateTime.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 94,12 @@ $arD->setOutputMode(ArDateTime::ARABIC_TRANSLITERATION);
echo $arD->arFormat('l dS F Y h:i:s A');
// 'الجمعة 14 يوليو 2017 02:15:30 صباحاً'
```


### Date Conversion Correction:
Because of the hijri calendar is calculated, there is an error margin of -/ 1 when converting from Hijri calendar to Gregorian calendar.

All The create methods accept a correction parameters to deal with this error.
```php
$arD = ArUtil::date()->arCreateFromDate(1430, 10, 15, $tz = null, $correction = -1);
```
25 changes: 24 additions & 1 deletion tests/I18N/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@

namespace ArUtil\Tests\Arabic;

use Carbon\Carbon;
use ArUtil\I18N\Date;
use ArUtil\I18N\Arabic;
use ArUtil\Tests\AbstractTestCase;
Expand Down Expand Up @@ -117,7 118,29 @@ protected function VerifyFormatWithMode($mode, $expectedString)
/** @test */
public function it_calculate_correction_factor_for_Um_Alqura_calendar()
{
$this->markTestSkipped('dateCorrection need to be fixed');
// Timestamp 1501311600
// GMT: Saturday, July 29, 2017 7:00:00 AM
// DST: Saturday, July 29, 2017 12:00:00 AM
// UmAlqura: 1438-11-06
// Is this accurate?
$this->assertEquals(0, $this->date->dateCorrection(1009843200));
// dd(Carbon::now()->startOfDay()->timestamp);
// dd(ArUtil::date()->artoDateTimeString());
// dd(strtotime('Jul 29, 2017 00:00:00'));
$i = 0;
$x = 0;
foreach(range(2000, 2037) as $year) {
foreach (range(1,12) as $month) {
$dt = Carbon::createFromDate($year, $month);
$correctionFactor = $this->date->dateCorrection($dt->timestamp);
$i ;
if ($correctionFactor != 0) {
$x ;
echo "\n\e[32mYear: $year, Month: $month, Correction Factor: $correctionFactor";
}
}
}
echo "\n\033[31m Number of Tested Months: {$i}\033[0m";
echo "\n\033[31m Number of correction factor != 0: {$x}\033[0m";
}
}
17 changes: 16 additions & 1 deletion tests/I18N/MktimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,22 @@ public function it_returns_timestamp_from_hijri_date()
/** @test */
public function it_makes_time_correction()
{
$actualDiff = $this->mktime->mktimeCorrection(10, 1430);
$arr = [
"1450-11-1" => "2029-3-16", // 0 Correction Day.
"1439-8-1" => "2018-4-17", // 0 Correction Day.
"1438-1-1" => "2016-10-2", // -1 Correction Day.
"1438-6-1" => "2017-2-28", // 0 Correction Day.
"1437-9-15" => "2016-6-20", // -1 Correction Day.
"1435-4-15" => "2014-2-15", // -1 Correction Day.
"1434-9-15" => "2013-7-23", // 0 Correction Day.
"1433-6-12" => "2012-5-3", // -1 Correction Day.
"1430-2-25" => "2009-2-20", // -1 Correction Day.
"1420-8-1" => "1999-11-9", // -1 Correction Day.
];
$correction = $this->mktime->mktimeCorrection( 9, 1437 );
$time = $this->mktime->mktime( 0, 0, 0, 9, 15, 1437, $correction );
$this->assertEquals('2016-6-20', date( 'Y-n-d', $time));
$actualDiff = $this->mktime->mktimeCorrection( 10, 1430 );
$this->assertEquals(-1, $actualDiff);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Time/ArDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 38,11 @@ public function it_return_carbon_instance_from_hijri_string_date()
$this->assertArDateTime($arD, 1405, 8, 10);
$this->assertEquals('1985-05-01', $arD->toDateString());
}

/** @test */
public function it_adjust_hijri_date_based_on_correction_factor()
{
$arD = ArUtil::date()->arCreateFromDate(1433, 6, 12 ,$tz = null, -1);
$this->assertEquals('2012-05-03', $arD->toDateString());
}
}
9 changes: 9 additions & 0 deletions tests/Time/CreateFromDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 52,13 @@ public function it_create_from_date_with_day()

$this->assertEquals($expected, $actual);
}

/** @test */
public function it_adjust_the_date_based_on_provided_correction_factor()
{
$arD = ArUtil::date()->arCreateFromDate(1430, 10, 15, null, -1);

$this->assertArDateTime($arD, 1430, 10, 15);
$this->assertCarbon($arD, 2009, 10, 4);
}
}
14 changes: 14 additions & 0 deletions tests/Time/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 44,18 @@ public function it_create_from_format()
$this->assertArDateTime($arD, 1430, 10, 1);
$this->assertCarbon($arD, 2009, 9, 21);
}

/** @test */
public function it_adjust_the_date_based_on_provided_correction_factor()
{
$arD = ArUtil::date()->arCreateFromFormat('Y/m/d', '1430/10/15', null, -1);
$this->assertCarbon($arD, 2009, 10, 4);

$arD = ArUtil::date()->arCreateFromDateTimeString('1430-10-15 4:30:45', null, -1);
$this->assertCarbon($arD, 2009, 10, 4);

$arD = ArUtil::date()->arCreateFromDateString('1430-10-15', null, -1);
$this->assertCarbon($arD, 2009, 10, 4);
}

}
8 changes: 8 additions & 0 deletions tests/Time/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 81,12 @@ public function testCreateWithTimeZoneString()
$this->assertCarbon($d, 1985, 5, 1, 0, 0, 0);
$this->assertSame('Europe/London', $d->tzName);
}

/** @test */
public function testCreateWithDateCorrectionFactor()
{
$arD = ArDateTime::arCreate(1430, 10, 15, null, null, null, null, -1);
$this->assertCarbon($arD, 2009, 10, 4);
}

}
19 changes: 19 additions & 0 deletions tests/Time/DateCorrectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
<?php

namespace ArUtil\Tests\Time;

use ArUtil\ArUtil;
use Carbon\Carbon;
use ArUtil\Time\ArDateTime;

class DateCorrectionTest extends AbstractTimeTest
{
/** @test */
public function it_set_the_default_correction_date()
{
$arD = ArUtil::date()->arCreateFromDate(1430, 10, 15, null, -1);

$this->assertArDateTime($arD, 1430, 10, 15);
$this->assertCarbon($arD, 2009, 10, 4);
}
}

0 comments on commit c13d6c7

Please sign in to comment.