influxdata/influxdb-php

Change `exec('date + %s%N')` to `time()` in README.md

Paulomart opened this issue · 4 comments

I was just using this api and tried exec('date + %s%N') as described in the README. This did not work for me, then I looked further, I saw that its also possible to do:

list($usec, $sec) = explode(' ', microtime());
$timestamp = sprintf('%d%06d', $sec, $usec*1000000);

But AFAIK this is the same was time(). (Please correct me if I am wrong)

Would you mind if I open a PR to change this?

@Paulomart

It seems to be a typo:

$ date +%s%N
1495012275N

there is a space between + and %

But my best guess is that you don't run it on linux. Maybe you can create a PR for a cross-platform example. That would be nice. In the docs it's stated that exec only works on Linux. The date tool on OSX is kind of outdated compared to the one shipped with linux distros (GNU coreutils).

Im running PHP Version 5.5.9-1ubuntu4.21 and get this output:

time(): 1495014729
exec('date +%s%N'): 1495014729339787398
mircotime(): 1495014729340115 

using

<?php
echo "time(): ". time();
echo "<br>";
echo "exec('date +%s%N'): " . exec('date +%s%N');
echo "<br>";

list($usec, $sec) = explode(' ', microtime());
$timestamp = sprintf('%d%06d', $sec, $usec*1000000);
echo "mircotime(): " . $timestamp;
?>

I guess it would be best to change the docs to time() then?

@Paulomart the docs were made with nanosecond precisions in mind. Are you using nanosecond precision or a less accurate one? time() returns seconds since epoch. The date function described earlier just returns nanoseconds since epoch. If you don't set the appropriate precision it will not work.

AFAIK the default precision is still nanoseconds. influxdata/influxdb#6041

I guess the docs are right then and php dosent provide a better method.