1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
<?php
declare(strict_types=1);
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Jenssegers;
use Carbon\Carbon;
class DateTest extends TestCaseBase
{
public function testConstructFromString()
{
Carbon::setTestNow();
$date = new Carbon('2013-01-31');
$this->assertSame(1359590400, $date->getTimestamp());
$before = time();
$date = new Carbon('1 day ago');
$after = time();
$this->assertGreaterThanOrEqual($before - 86400, $date->getTimestamp());
$this->assertLessThanOrEqual($after - 86400, $date->getTimestamp());
}
public function testConstructTimestamp()
{
$date = new Carbon(1367186296);
$this->assertSame(1367186296, $date->getTimestamp());
}
public function testMake()
{
$date1 = Carbon::make('Sunday 28 April 2013 21:58:16');
$date2 = new Carbon('Sunday 28 April 2013 21:58:16');
$this->assertEquals($date1, $date2);
}
public function testCreateFromCarbon()
{
$date = Carbon::make(Carbon::createFromFormat('U', '1367186296'));
$this->assertInstanceOf(Carbon::class, $date);
$this->assertSame(1367186296, $date->getTimestamp());
}
public function testManipulation()
{
$now = Carbon::now();
$this->assertSame(86400, $now->copy()->add('1 day')->getTimestamp() - $now->getTimestamp());
$this->assertSame(4 * 86400, $now->copy()->add('4 day')->getTimestamp() - $now->getTimestamp());
$this->assertSame(-86400, $now->copy()->sub('1 day')->getTimestamp() - $now->getTimestamp());
$this->assertSame(-4 * 86400, $now->copy()->sub('4 day')->getTimestamp() - $now->getTimestamp());
$this->assertSame(10 * 86400, $now->copy()->add('P10D')->getTimestamp() - $now->getTimestamp());
$this->assertSame(-10 * 86400, $now->copy()->sub('P10D')->getTimestamp() - $now->getTimestamp());
}
public function testFormat()
{
$date = new Carbon(1367186296);
$this->assertSame('Sunday 28 April 2013 21:58:16', $date->format('l j F Y H:i:s'));
}
public function testAge()
{
// Age test can't work on February 29th
if (Carbon::now()->format('m-d') === '02-29') {
Carbon::setTestNow(Carbon::now()->subDay());
}
$date = Carbon::parse('-5 years');
$this->assertSame(5, $date->age);
}
public function testAgo()
{
// Ago test can't work on February 29th
if (Carbon::now()->format('m-d') === '02-29') {
Carbon::setTestNow(Carbon::now()->subDay());
}
$date = Carbon::parse('-5 years');
$this->assertSame('5 years ago', $date->ago());
$date = JenssegersDate::now()->subMonthsNoOverflow(5);
$this->assertSame('5 months ago', $date->ago());
$date = Carbon::parse('-32 days');
$this->assertSame('1 month ago', $date->ago());
$date = Carbon::parse('-4 days');
$this->assertSame('4 days ago', $date->ago());
$date = Carbon::parse('-1 day');
$this->assertSame('1 day ago', $date->ago());
$date = Carbon::parse('-3 hours');
$this->assertSame('3 hours ago', $date->ago());
$date = Carbon::parse('-1 hour');
$this->assertSame('1 hour ago', $date->ago());
$date = Carbon::parse('-2 minutes');
$this->assertSame('2 minutes ago', $date->ago());
$date = Carbon::parse('-1 minute');
$this->assertSame('1 minute ago', $date->ago());
$date = Carbon::parse('-50 second');
$this->assertSame('50 seconds ago', $date->ago());
$date = Carbon::parse('-1 second');
$this->assertSame('1 second ago', $date->ago());
$date = Carbon::parse('+5 days');
$this->assertSame('5 days from now', $date->ago());
$date = Carbon::parse('+5 days');
$this->assertSame('5 days after', $date->ago(Carbon::now()));
$date = Carbon::parse('-5 days');
$this->assertSame('5 days before', $date->ago(Carbon::now()));
}
public function testAbsoluteAgo()
{
$date = Carbon::parse('-5 days');
$this->assertSame('5 days', $date->ago(Carbon::now(), true));
$date = Carbon::parse('+5 days');
$this->assertSame('5 days', $date->ago(Carbon::now(), true));
}
public function testDiffForHumans()
{
// Diff for humans test can't work on February 29th
if (Carbon::now()->format('m-d') === '02-29') {
Carbon::setTestNow(Carbon::now()->subDay());
}
$date = Carbon::parse('-5 years');
$this->assertSame('5 years ago', $date->diffForHumans());
$date = Carbon::parse('-15 days');
$this->assertSame('2 weeks ago', $date->diffForHumans());
$date = Carbon::parse('-13 days');
$this->assertSame('1 week ago', $date->diffForHumans());
$date = Carbon::parse('-13 days');
$this->assertSame('1 week', $date->diffForHumans(null, true));
$date = JenssegersDate::now()->subMonthsNoOverflow(3);
$this->assertSame('3 months', $date->diffForHumans(null, true));
$date = Carbon::parse('-1 week');
$future = Carbon::parse('+1 week');
$this->assertSame('2 weeks after', $future->diffForHumans($date));
$this->assertSame('2 weeks before', $date->diffForHumans($future));
}
public function testTimespan()
{
$date = new Carbon(1403619368);
$date = $date->sub('-100 days -3 hours -20 minutes');
$this->assertSame('3 months, 1 week, 1 day, 3 hours, 20 minutes', $date->timespan(1403619368));
}
public function testTranslateTimeString()
{
Carbon::setLocale('ru');
$date = Carbon::translateTimeString('понедельник 21 март 2015');
$this->assertSame('monday 21 march 2015', mb_strtolower($date));
Carbon::setLocale('de');
$date = Carbon::translateTimeString('Montag 21 März 2015');
$this->assertSame('monday 21 march 2015', mb_strtolower($date));
$this->assertSame('Foobar', Carbon::translateTimeString('Foobar', 'xx'));
}
public function testTranslateTimeStringWithOrdinalWords()
{
$date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'en');
$this->assertSame('first monday of may', mb_strtolower($date));
$date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'es');
$this->assertSame('primer lunes de mayo', mb_strtolower($date));
}
}
|