Skip to content

Commit 4a1ea50

Browse files
onlyshowYurunsoft
andauthored
支持 TDengine 3.0 时区配置 (#9)
* 新增时区配置 * 规范时区配置 * 增加timezone配置项的说明 * php-cs-fixer格式代码 * Update README.md Co-authored-by: Yurun <admin@yurunsoft.com>
1 parent d151fe2 commit 4a1ea50

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING, // 此配置 TDengine 3.0 下失效,统一返回格式为:2022-11-25T05:41:04.690Z
3939
// 'keepAlive' => true,
4040
// 'version' => '3', // TDengine 版本
41+
// 'timezone' => 'UTC', // TDengine >= 3.0 时支持,可选参数,指定返回时间的时区
4142
]));
4243
// 设置默认数据库为test
4344
\Yurun\TDEngine\TDEngineManager::setDefaultClientName('test');
@@ -59,6 +60,7 @@ $client = new \Yurun\TDEngine\Client(new \Yurun\TDEngine\ClientConfig([
5960
// 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING, // 此配置 TDengine 3.0 下失效,统一返回格式为:2022-11-25T05:41:04.690Z
6061
// 'keepAlive' => true,
6162
// 'version' => '3', // TDengine 版本
63+
// 'timezone' => 'UTC', // TDengine >= 3.0 时支持,可选参数,指定返回时间的时区
6264
]));
6365

6466
// 通过 sql 方法执行 sql 语句

src/Client.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ public function getConfig(): ClientConfig
4343
public function buildUrl(string $path): string
4444
{
4545
$config = $this->getConfig();
46+
if (version_compare($config->getVersion(), '3', '<'))
47+
{
48+
$query = '';
49+
}
50+
else
51+
{
52+
$query = http_build_query(['tz' => $config->getTimezone()]);
53+
}
4654

47-
return Uri::makeUri($config->getHost(), $path, '', $config->getPort(), $config->getSsl() ? 'https' : 'http', '', $config->getUser() . ':' . $config->getPassword())->__toString();
55+
return Uri::makeUri($config->getHost(), $path, $query, $config->getPort(), $config->getSsl() ? 'https' : 'http', '', $config->getUser() . ':' . $config->getPassword())->__toString();
4856
}
4957

5058
private function getHttpClient(): HttpRequest

src/ClientConfig.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class ClientConfig
6060
*/
6161
protected $version = '3';
6262

63+
/**
64+
* @var string
65+
*/
66+
protected $timezone = 'UTC';
67+
6368
public function __construct(array $config = [])
6469
{
6570
if ($config)
@@ -190,4 +195,16 @@ public function setVersion(string $version): self
190195

191196
return $this;
192197
}
198+
199+
public function getTimezone(): string
200+
{
201+
return $this->timezone;
202+
}
203+
204+
public function setTimezone(string $timezone): self
205+
{
206+
$this->timezone = $timezone;
207+
208+
return $this;
209+
}
193210
}

0 commit comments

Comments
 (0)