Skip to content

Commit

Permalink
fix: nanosecond datetime string convertor (influxdata#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored and peynman committed Mar 7, 2023
1 parent 1653a38 commit b0dd30d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Bug Fixes
1. [#95](https://github.com/influxdata/influxdb-client-php/pull/95): Fix file descriptor leaks in WriteApi
1. [#96](https://github.com/influxdata/influxdb-client-php/pull/96): Fix nanosecond datetime string convertor

## 2.2.0 [2021-09-17]

Expand Down
6 changes: 3 additions & 3 deletions src/InfluxDB2/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public static function deserialize($data, $class, $httpHeaders = null)
return $instance;
}
}

/**
* Nano precision is not supported while decoding RFC 3339 formatted date/times.
* https://bugs.php.net/bug.php?id=6481
Expand All @@ -337,9 +338,9 @@ public static function deserialize($data, $class, $httpHeaders = null)
*/
static function fixDatetimeNanos(string $date) : string {
$dateParts = explode(".", $date);
$nanosZ = $dateParts[1];
$posZ = strpos($nanosZ, 'Z');
$nanosZ = $dateParts[1] ?? "";
if (strlen($nanosZ) > 9) {
$posZ = strpos($nanosZ, 'Z');
$converted = $dateParts[0] . "." . substr($nanosZ, 0, 8);
if ($posZ > 0) {
$converted .= "Z";
Expand All @@ -349,5 +350,4 @@ static function fixDatetimeNanos(string $date) : string {
return $date;
}
}

}
5 changes: 5 additions & 0 deletions tests/ITBucketServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function testFixNanosTimeSerialization()
"2020-09-18T08:03:48.12345678Z",
ObjectSerializer::fixDatetimeNanos("2020-09-18T08:03:48.1234567899Z")
);

self::assertEquals(
"2021-09-29T06:32:56Z",
ObjectSerializer::fixDatetimeNanos("2021-09-29T06:32:56Z")
);
}

public function testBucketService()
Expand Down
34 changes: 34 additions & 0 deletions tests/ITTaskServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use InfluxDB2\Model\TaskCreateRequest;
use InfluxDB2\Service\TasksService;

require_once('IntegrationBaseTestCase.php');

/**
* @group integration
*/
class ITTaskServiceTest extends IntegrationBaseTestCase
{
public function testCreateTask()
{
/** @var TasksService $taskService */
$taskService = $this->client->createService(TasksService::class);

$flux = "option task = {
name: \"task-name\",
every: 6h
}
from(bucket: \"telegraf\") |> range(start: -1h)
";

$taskCreateRequest = new TaskCreateRequest();
$taskCreateRequest
->setFlux($flux)
->setOrgId($this->findMyOrg()->getId());

$task = $taskService->postTasks($taskCreateRequest);

self::assertEquals("task-name", $task->getName());
}
}

0 comments on commit b0dd30d

Please sign in to comment.