Skip to content

Commit

Permalink
Merge pull request #64 from lucasmichot/feature/start-end
Browse files Browse the repository at this point in the history
Add new start / end functions
  • Loading branch information
briannesbitt committed Dec 5, 2013
2 parents 51bc5af + 0d6692a commit 2ec31b3
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 0 deletions.
18 changes: 18 additions & 0 deletions readme.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,24 @@ These group of methods perform helpful modifications to the current instance. M
{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier4::exec(echo $dt->endOfMonth();/*pad(50)*/)}} // {{modifier4_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier15::exec(echo $dt->startOfYear();/*pad(50)*/)}} // {{modifier15_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier16::exec(echo $dt->endOfYear();/*pad(50)*/)}} // {{modifier16_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier17::exec(echo $dt->startOfDecade();/*pad(50)*/)}} // {{modifier17_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier18::exec(echo $dt->endOfDecade();/*pad(50)*/)}} // {{modifier18_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier19::exec(echo $dt->startOfCentury();/*pad(50)*/)}} // {{modifier19_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier20::exec(echo $dt->endOfCentury();/*pad(50)*/)}} // {{modifier20_eval}}

{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}}
{{modifier5::exec(echo $dt->startOfWeek();/*pad(50)*/)}} // {{modifier5_eval}}
{{modifier6::exec(var_dump($dt->dayOfWeek == Carbon::MONDAY);/*pad(50)*/)}} // {{modifier6_eval}} : ISO8601 week starts on Monday
Expand Down
61 changes: 61 additions & 0 deletions src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,67 @@ public function endOfMonth()
return $this->day($this->daysInMonth)->endOfDay();
}

/**
* Resets the date to the first day of the year and the time to 00:00:00
*
* @return Carbon
*/
public function startOfYear()
{
return $this->month(1)->startOfMonth();
}

/**
* Resets the date to end of the year and time to 23:59:59
*
* @return Carbon
*/
public function endOfYear()
{
return $this->month(12)->endOfMonth();
}

/**
* Resets the date to the first day of the decade and the time to 00:00:00
*
* @return Carbon
*/
public function startOfDecade()
{
return $this->startOfYear()->year($this->year - $this->year % 10);
}

/**
* Resets the date to end of the decade and time to 23:59:59
*
* @return Carbon
*/
public function endOfDecade()
{
return $this->endOfYear()->year($this->year - $this->year % 10 + 9);
}


/**
* Resets the date to the first day of the century and the time to 00:00:00
*
* @return Carbon
*/
public function startOfCentury()
{
return $this->startOfYear()->year($this->year - $this->year % 100);
}

/**
* Resets the date to end of the century and time to 23:59:59
*
* @return Carbon
*/
public function endOfCentury()
{
return $this->endOfYear()->year($this->year - $this->year % 100 + 99);
}

/**
* Resets the date to the first day of the ISO-8601 week (Monday) and the time to 00:00:00
*
Expand Down
126 changes: 126 additions & 0 deletions tests/StartEndOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ public function testStartOfMonthFromLastDay()
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}

public function testStartOfYearIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->startOfYear() instanceof Carbon);
}
public function testStartOfYearFromNow()
{
$dt = Carbon::now()->startOfYear();
$this->assertCarbon($dt, $dt->year, 1, 1, 0, 0, 0);
}
public function testStartOfYearFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfYear();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}
public function testStartOfYearFromLastDay()
{
$dt = Carbon::create(2000, 12, 31, 23, 59, 59)->startOfYear();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}

public function testEndOfMonthIsFluid()
{
$dt = Carbon::now();
Expand All @@ -57,4 +78,109 @@ public function testEndOfMonthFromLastDay()
$dt = Carbon::create(2000, 1, 31, 2, 3, 4)->endOfMonth();
$this->assertCarbon($dt, 2000, 1, 31, 23, 59, 59);
}

public function testEndOfYearIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->endOfYear() instanceof Carbon);
}
public function testEndOfYearFromNow()
{
$dt = Carbon::now()->endOfYear();
$this->assertCarbon($dt, $dt->year, 12, 31, 23, 59, 59);
}
public function testEndOfYearFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfYear();
$this->assertCarbon($dt, 2000, 12, 31, 23, 59, 59);
}
public function testEndOfYearFromLastDay()
{
$dt = Carbon::create(2000, 12, 31, 23, 59, 59)->endOfYear();
$this->assertCarbon($dt, 2000, 12, 31, 23, 59, 59);
}

public function testStartOfDecadeIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->startOfDecade() instanceof Carbon);
}
public function testStartOfDecadeFromNow()
{
$dt = Carbon::now()->startOfDecade();
$this->assertCarbon($dt, $dt->year - $dt->year % 10, 1, 1, 0, 0, 0);
}
public function testStartOfDecadeFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfDecade();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}
public function testStartOfDecadeFromLastDay()
{
$dt = Carbon::create(2009, 12, 31, 23, 59, 59)->startOfDecade();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}

public function testEndOfDecadeIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->endOfDecade() instanceof Carbon);
}
public function testEndOfDecadeFromNow()
{
$dt = Carbon::now()->endOfDecade();
$this->assertCarbon($dt, $dt->year - $dt->year % 10 + 9, 12, 31, 23, 59, 59);
}
public function testEndOfDecadeFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfDecade();
$this->assertCarbon($dt, 2009, 12, 31, 23, 59, 59);
}
public function testEndOfDecadeFromLastDay()
{
$dt = Carbon::create(2009, 12, 31, 23, 59, 59)->endOfDecade();
$this->assertCarbon($dt, 2009, 12, 31, 23, 59, 59);
}

public function testStartOfCenturyIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->startOfCentury() instanceof Carbon);
}
public function testStartOfCenturyFromNow()
{
$dt = Carbon::now()->startOfCentury();
$this->assertCarbon($dt, $dt->year - $dt->year % 100, 1, 1, 0, 0, 0);
}
public function testStartOfCenturyFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfCentury();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}
public function testStartOfCenturyFromLastDay()
{
$dt = Carbon::create(2009, 12, 31, 23, 59, 59)->startOfCentury();
$this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0);
}

public function testEndOfCenturyIsFluid()
{
$dt = Carbon::now();
$this->assertTrue($dt->endOfCentury() instanceof Carbon);
}
public function testEndOfCenturyFromNow()
{
$dt = Carbon::now()->endOfCentury();
$this->assertCarbon($dt, $dt->year - $dt->year % 100 + 99, 12, 31, 23, 59, 59);
}
public function testEndOfCenturyFromFirstDay()
{
$dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfCentury();
$this->assertCarbon($dt, 2099, 12, 31, 23, 59, 59);
}
public function testEndOfCenturyFromLastDay()
{
$dt = Carbon::create(2099, 12, 31, 23, 59, 59)->endOfCentury();
$this->assertCarbon($dt, 2099, 12, 31, 23, 59, 59);
}
}

0 comments on commit 2ec31b3

Please sign in to comment.