Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a5b1a7

Browse files
committed
Always represent message timestamps as DateTime objects
1 parent 2e0f951 commit 1a5b1a7

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

examples/04-connect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
if ($type === Protocol::REQUEST_RPCCALL && $message[1] === '2displayMsg(Message)') {
6464
$in = $message[2];
6565
assert($in instanceof Message);
66-
echo date(DATE_ISO8601, $in->timestamp) . ' in ' . $in->bufferInfo->name . ' by ' . explode('!', $in->sender)[0] . ': ' . $in->contents . PHP_EOL;
66+
echo $in->timestamp->format(DATE_ISO8601) . ' in ' . $in->bufferInfo->name . ' by ' . explode('!', $in->sender)[0] . ': ' . $in->contents . PHP_EOL;
6767

6868
return;
6969
}

examples/05-backlog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
echo json_encode(
6868
array(
6969
'id' => $in->id,
70-
'date' => date(\DATE_ATOM, $in->timestamp),
70+
'date' => $in->timestamp->format(\DATE_ATOM),
7171
'channel' => $in->bufferInfo->name,
7272
'sender' => explode('!', $in->sender)[0],
7373
'contents' => $in->contents

src/Io/Protocol.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ public function __construct()
7474
return $reader->readUInt();
7575
},
7676
'Message' => function (Reader $reader) {
77+
$readTimestamp = function () use ($reader) {
78+
// create DateTime object with local time zone from unix timestamp
79+
$d = new \DateTime('@' . $reader->readUint());
80+
$d->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
81+
return $d;
82+
};
83+
7784
return new Message(
7885
$reader->readUInt(),
79-
$reader->readUInt(),
86+
$readTimestamp(),
8087
$reader->readUInt(),
8188
$reader->readUChar(),
8289
$reader->readQUserTypeByName('BufferInfo'),

src/Models/Message.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Message
3939
public $id;
4040

4141
/**
42-
* @var int UNIX timestamp
42+
* @var \DateTime
4343
*/
4444
public $timestamp;
4545

@@ -72,15 +72,15 @@ class Message
7272
* [Internal] Instantiation is handled internally and should not be called manually.
7373
*
7474
* @param int $id
75-
* @param int $timestamp UNIX timestamp
75+
* @param \DateTime $timestamp
7676
* @param int $type single type constant, see self::TYPE_* constants
7777
* @param int $flags bitmask of flag constants, see self::FLAG_* constants
7878
* @param BufferInfo $bufferInfo
7979
* @param string $sender sender in the form `nick!user@host` or only `host` or empty string
8080
* @param string $contents
8181
* @internal
8282
*/
83-
public function __construct($id, $timestamp, $type, $flags, BufferInfo $bufferInfo, $sender, $contents)
83+
public function __construct($id, \DateTime $timestamp, $type, $flags, BufferInfo $bufferInfo, $sender, $contents)
8484
{
8585
$this->id = $id;
8686
$this->timestamp = $timestamp;

tests/FunctionalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public function testRequestBacklogReceivesBacklog()
263263
}
264264

265265
$this->assertTrue($newest instanceof Message);
266+
$this->assertInstanceOf('DateTime', $newest->timestamp);
266267

267268
$client->close();
268269
}

tests/Models/MessageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function testChatMessage()
99
$buffer = $this->getMockBuilder('Clue\React\Quassel\Models\BufferInfo')->disableOriginalConstructor()->getMock();
1010
$message = new Message(
1111
1000,
12-
1528039705,
12+
new DateTime('@1528039705'),
1313
Message::TYPE_PLAIN,
1414
Message::FLAG_NONE,
1515
$buffer,
@@ -18,7 +18,7 @@ public function testChatMessage()
1818
);
1919

2020
$this->assertSame(1000, $message->id);
21-
$this->assertSame(1528039705, $message->timestamp);
21+
$this->assertSame(1528039705, $message->timestamp->getTimestamp());
2222
$this->assertSame(Message::TYPE_PLAIN, $message->type);
2323
$this->assertSame(Message::FLAG_NONE, $message->flags);
2424
$this->assertSame($buffer, $message->bufferInfo);
@@ -31,7 +31,7 @@ public function testJoinMessage()
3131
$buffer = $this->getMockBuilder('Clue\React\Quassel\Models\BufferInfo')->disableOriginalConstructor()->getMock();
3232
$message = new Message(
3333
999,
34-
1528039704,
34+
new DateTime('@1528039704'),
3535
Message::TYPE_JOIN,
3636
Message::FLAG_NONE,
3737
$buffer,
@@ -40,7 +40,7 @@ public function testJoinMessage()
4040
);
4141

4242
$this->assertSame(999, $message->id);
43-
$this->assertSame(1528039704, $message->timestamp);
43+
$this->assertSame(1528039704, $message->timestamp->getTimestamp());
4444
$this->assertSame(Message::TYPE_JOIN, $message->type);
4545
$this->assertSame(Message::FLAG_NONE, $message->flags);
4646
$this->assertSame($buffer, $message->bufferInfo);

0 commit comments

Comments
 (0)