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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
<?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\CarbonImmutable;
use Carbon\CarbonImmutable as Carbon;
use DateTimeZone;
use Tests\AbstractTestCase;
class CreateFromTimestampTest extends AbstractTestCase
{
public function testCreateReturnsDatingInstance()
{
$d = Carbon::createFromTimestamp(Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5);
}
public function testCreateFromTimestampMs()
{
$baseTimestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000;
$timestamp = $baseTimestamp + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
$timestamp = $baseTimestamp + 321.8;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321800);
$timestamp = $baseTimestamp + 321.84;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321840);
$timestamp = $baseTimestamp + 321.847;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321847);
$timestamp = $baseTimestamp + 321.8474;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321847);
$timestamp = $baseTimestamp + 321.8479;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321848);
}
public function testCreateFromTimestampMsUTC()
{
// Toronto is GMT-04:00 in May
$baseTimestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000;
$timestamp = $baseTimestamp + 321;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321000);
$timestamp = $baseTimestamp + 321.8;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321800);
$timestamp = $baseTimestamp + 321.84;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321840);
$timestamp = $baseTimestamp + 321.847;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321847);
$timestamp = $baseTimestamp + 321.8474;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321847);
$timestamp = $baseTimestamp + 321.8479;
$d = Carbon::createFromTimestampMsUTC($timestamp);
$this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321848);
$d = Carbon::createFromTimestampMsUTC(1);
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 0, 1000);
$d = Carbon::createFromTimestampMsUTC(60);
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 0, 60000);
$d = Carbon::createFromTimestampMsUTC(1000);
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 1, 0);
$d = Carbon::createFromTimestampMsUTC(-0.04);
$this->assertCarbon($d, 1969, 12, 31, 23, 59, 59, 999960);
}
public function testComaDecimalSeparatorLocale()
{
$date = new Carbon('2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
$locale = setlocale(LC_ALL, '0');
if (setlocale(LC_ALL, 'fr_FR.UTF-8', 'fr_FR.utf8', 'French_France.UTF8') === false) {
$this->markTestSkipped('testComaDecimalSeparatorLocale test need fr_FR.UTF-8.');
}
$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
$date = new Carbon('2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
$this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
$timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
$d = Carbon::createFromTimestampMs($timestamp);
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
setlocale(LC_ALL, $locale);
}
public function testCreateFromTimestampUsesDefaultTimezone()
{
$d = Carbon::createFromTimestamp(0);
// We know Toronto is -5 since no DST in Jan
$this->assertSame(1969, $d->year);
$this->assertSame(-5 * 3600, $d->offset);
}
public function testCreateFromTimestampWithDateTimeZone()
{
$d = Carbon::createFromTimestamp(0, new DateTimeZone('UTC'));
$this->assertSame('UTC', $d->tzName);
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
}
public function testCreateFromTimestampWithString()
{
$d = Carbon::createFromTimestamp(0, 'UTC');
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
$this->assertSame(0, $d->offset);
$this->assertSame('UTC', $d->tzName);
}
public function testCreateFromTimestampGMTDoesNotUseDefaultTimezone()
{
$d = Carbon::createFromTimestampUTC(0);
$this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
$this->assertSame(0, $d->offset);
}
/**
* Ensures DST php bug does not affect createFromTimestamp in DST change.
*
* @see https://github.com/briannesbitt/Carbon/issues/1951
*/
public function testCreateFromTimestampInDstChange()
{
$this->assertSame(
'2019-11-03T01:00:00-04:00',
Carbon::createFromTimestamp(1572757200, 'America/New_York')->toIso8601String()
);
$this->assertSame(
'2019-11-03T01:00:00-05:00',
Carbon::createFromTimestamp(1572757200 + 3600, 'America/New_York')->toIso8601String()
);
$this->assertSame(
'2019-11-03T01:00:00-04:00',
Carbon::createFromTimestampMs(1572757200000, 'America/New_York')->toIso8601String()
);
$this->assertSame(
'2019-11-03T01:00:00-05:00',
Carbon::createFromTimestampMs(1572757200000 + 3600000, 'America/New_York')->toIso8601String()
);
}
public function testCreateFromMicrotimeFloat()
{
$microtime = 1600887164.88952298;
$d = Carbon::createFromTimestamp($microtime);
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:44.889523', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.889523', $d->format('U.u'));
$microtime = 1600887164.0603;
$d = Carbon::createFromTimestamp($microtime);
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:44.060300', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.060300', $d->format('U.u'));
$this->assertSame('010000', Carbon::createFromTimestamp(0.01)->format('u'));
}
public function testCreateFromMicrotimeStrings()
{
$microtime = '0.88951247 1600887164';
$d = Carbon::createFromTimestamp($microtime);
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:44.889512', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.889512', $d->format('U.u'));
$microtime = '0.88951247/1600887164/12.56';
$d = Carbon::createFromTimestamp($microtime);
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:57.449512', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887177.449512', $d->format('U.u'));
$d = Carbon::createFromTimestamp('-10.6');
$this->assertSame('1969-12-31 18:59:49.400000 -05:00', $d->format('Y-m-d H:i:s.u P'));
$d = new Carbon('-10.6');
$this->assertSame('1969-12-31 23:59:49.400000 +00:00', $d->format('Y-m-d H:i:s.u P'));
}
public function testCreateFromMicrotimeUTCFloat()
{
$microtime = 1600887164.88952298;
$d = Carbon::createFromTimestampUTC($microtime);
$this->assertSame('+00:00', $d->tzName);
$this->assertSame('2020-09-23 18:52:44.889523', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.889523', $d->format('U.u'));
}
public function testCreateFromMicrotimeUTCStrings()
{
$microtime = '0.88951247 1600887164';
$d = Carbon::createFromTimestampUTC($microtime);
$this->assertSame('+00:00', $d->tzName);
$this->assertSame('2020-09-23 18:52:44.889512', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.889512', $d->format('U.u'));
$microtime = '0.88951247/1600887164/12.56';
$d = Carbon::createFromTimestampUTC($microtime);
$this->assertSame('+00:00', $d->tzName);
$this->assertSame('2020-09-23 18:52:57.449512', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887177.449512', $d->format('U.u'));
}
public function testNegativeIntegerTimestamp()
{
$this->assertSame(
'1969-12-31 18:59:59.000000 -05:00',
Carbon::createFromTimestamp(-1)->format('Y-m-d H:i:s.u P')
);
}
}
|