Skip to content

Commit

Permalink
Merge pull request #144 from amirfaramarzi/master
Browse files Browse the repository at this point in the history
add new features(getFirstDayOfWeek , getFirstDayOfMonth , getFirstDayOfYear)
  • Loading branch information
Morteza Parvini authored Apr 14, 2022
2 parents aa5fd58 + 61e105d commit 825f462
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Jalalian.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,45 @@ public static function fromDateTime($dateTime, \DateTimeZone $timeZone = null):

return static::fromCarbon(new Carbon($dateTime, $timeZone));
}

public function getFirstDayOfWeek(): Jalalian
{
return (new static(
$this->getYear(),
$this->getMonth(),
$this->getDay(),
$this->getHour(),
$this->getMinute(),
$this->getSecond(),
$this->getTimezone()
))->subDays($this->getDayOfWeek());
}

public function getFirstDayOfMonth(): Jalalian
{
return new static(
$this->getYear(),
$this->getMonth(),
1,
$this->getHour(),
$this->getMinute(),
$this->getSecond(),
$this->getTimezone()
);
}

public function getFirstDayOfYear(): Jalalian
{
return new static(
$this->getYear(),
1,
1,
$this->getHour(),
$this->getMinute(),
$this->getSecond(),
$this->getTimezone()
);
}

public function getMonthDays()
{
Expand Down
45 changes: 45 additions & 0 deletions tests/JalalianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,49 @@ public function testGetWeekOfMonth()
$jDate = new Jalalian(1395, 7, 20);
$this->assertEquals($jDate->getWeekOfMonth(), 4);
}

public function testGetFirstDayOfWeek()
{
$jDate = new Jalalian(1401, 1, 23);
$this->assertEquals($jDate->getFirstDayOfWeek()->format('Y-m-d'), '1401-01-20');

$jDate = new Jalalian(1395, 4, 24);
$this->assertEquals($jDate->getFirstDayOfWeek()->format('Y-m-d'), '1395-04-19');

$jDate = new Jalalian(1398, 11, 7);
$this->assertEquals($jDate->getFirstDayOfWeek()->format('Y-m-d'), '1398-11-05');

$jDate = new Jalalian(1400, 8, 19);
$this->assertEquals($jDate->getFirstDayOfWeek()->format('Y-m-d'), '1400-08-15');
}

public function testGetFirstDayOfMonth()
{
$jDate = new Jalalian(1401, 1, 23);
$this->assertEquals($jDate->getFirstDayOfMonth()->format('Y-m-d'), '1401-01-01');

$jDate = new Jalalian(1390, 5, 14);
$this->assertEquals($jDate->getFirstDayOfMonth()->format('Y-m-d'), '1390-05-01');

$jDate = new Jalalian(1399, 2, 29);
$this->assertEquals($jDate->getFirstDayOfMonth()->format('Y-m-d'), '1399-02-01');

$jDate = new Jalalian(1398, 10, 10);
$this->assertEquals($jDate->getFirstDayOfMonth()->format('Y-m-d'), '1398-10-01');
}

public function testGetFirstDayOfYear()
{
$jDate = new Jalalian(1401, 6, 11);
$this->assertEquals($jDate->getFirstDayOfYear()->format('Y-m-d'), '1401-01-01');

$jDate = new Jalalian(1399, 11, 28);
$this->assertEquals($jDate->getFirstDayOfYear()->format('Y-m-d'), '1399-01-01');

$jDate = new Jalalian(1394, 1, 12);
$this->assertEquals($jDate->getFirstDayOfYear()->format('Y-m-d'), '1394-01-01');

$jDate = new Jalalian(1393, 9, 5);
$this->assertEquals($jDate->getFirstDayOfYear()->format('Y-m-d'), '1393-01-01');
}
}

0 comments on commit 825f462

Please sign in to comment.