-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
58 lines (51 loc) · 2.23 KB
/
index.php
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
<?php
require __DIR__ . '/vendor/autoload.php';
$protocDir = __DIR__ .'/protoc';
echo "Assume protoc is in $protocDir", PHP_EOL;
$command = $protocDir .'/bin/protoc'
. ' --php_out=' . __DIR__
. ' --proto_path=' . __DIR__
. ' --proto_path=' . $protocDir . '/include'
. ' sample.proto';
exec($command);
require __DIR__ . '/Sample.php';
echo PHP_EOL;
echo PHP_EOL;
$timestamp = new \DateTime('2018-02-25T03:48:17.428086', new \DateTimeZone('UTC'));
$gpbTimestamp = new \Google\Protobuf\Timestamp();
$gpbTimestamp->setSeconds((int) $timestamp->format('U'));
$gpbTimestamp->setNanos((int) $timestamp->format('u'));
echo "PHP Version: ", PHP_VERSION, PHP_EOL;
echo PHP_EOL;
echo "Original as string : ", $timestamp->format('Y-m-d\TH:i:s.uP'), PHP_EOL;
echo "Original seconds : ", $timestamp->format('U.u'), PHP_EOL;
echo PHP_EOL;
echo "Convert nano string to/from JSON:", PHP_EOL;
$nanoArray = [
'timestamp' => $timestamp->format('Y-m-d\TH:i:s.u') . '000Z',
];
$message = new Sample();
$message->mergeFromJsonString(json_encode($nanoArray));
echo "Nano input string : ", $nanoArray['timestamp'], PHP_EOL;
echo "JS Decoded seconds : ", $message->getTimestamp()->getSeconds(), PHP_EOL;
echo "JS Decoded nanos : ", $message->getTimestamp()->getNanos(), PHP_EOL;
echo PHP_EOL;
echo "Convert to and from protobuf:", PHP_EOL;
$message = new Sample(); // ensure clear start
$message->setTimestamp($gpbTimestamp);
echo "Pre-enc seconds : ", $message->getTimestamp()->getSeconds(), PHP_EOL;
echo "Pre-enc nanos : ", $message->getTimestamp()->getNanos(), PHP_EOL;
$pbEnc = $message->serializeToString();
$message = new Sample(); // ensure clear start
$message->mergeFromString($pbEnc);
echo "PB Decoded sec : ", $message->getTimestamp()->getSeconds(), PHP_EOL;
echo "PB Decoded nsec : ", $message->getTimestamp()->getNanos(), PHP_EOL;
echo PHP_EOL;
echo "Convert to and from JSON:", PHP_EOL;
$message = new Sample(); // ensure clear start
$message->setTimestamp($gpbTimestamp);
$jsonEnc = $message->serializeToJsonString();
$message = new Sample(); // ensure clear start
$message->mergeFromJsonString($jsonEnc);
echo "JS Decoded sec : ", $message->getTimestamp()->getSeconds(), PHP_EOL;
echo "JS Decoded nsec : ", $message->getTimestamp()->getNanos(), PHP_EOL;