Skip to content

Commit d0ef4cf

Browse files
authored
docs: add convert_timezone reference (#2958)
1 parent ee070d9 commit d0ef4cf

File tree

4 files changed

+234
-46
lines changed

4 files changed

+234
-46
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: CONVERT_TIMEZONE
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="新增或更新:v1.2.680"/>
7+
8+
`CONVERT_TIMEZONE()` 用于将时间戳从当前会话时区(默认 `UTC`)转换为第一个参数指定的目标时区。目标时区必须是有效的 [IANA 时区名称](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html)
9+
10+
## 语法
11+
12+
```sql
13+
CONVERT_TIMEZONE(<target_timezone>, <timestamp_expr>)
14+
```
15+
16+
| 参数 | 说明 |
17+
|------|------|
18+
| `<target_timezone>` | 区分大小写的时区名称,例如 `'America/Los_Angeles'``'UTC'`|
19+
| `<timestamp_expr>` | 可解析为 TIMESTAMP 的表达式,按照当前会话时区进行解释。 |
20+
21+
## 返回类型
22+
23+
返回目标时区下等价时刻的 TIMESTAMP。
24+
25+
## 行为说明
26+
27+
- 源时区始终等于当前会话时区(默认 `UTC`)。如果存储的数据使用其他时区,请先在会话或连接层面做好设置。
28+
- 无效的时区名称会报错;任一参数为 `NULL` 时返回 `NULL`
29+
- 夏令时缺口可能导致部分时间戳无效,可在会话或租户级别开启 `enable_dst_hour_fix = 1` 让 Databend 自动调整。
30+
31+
## 示例
32+
33+
### 默认 UTC 会话下的单次转换
34+
35+
```sql
36+
SELECT CONVERT_TIMEZONE('America/Los_Angeles', '2024-11-01 11:36:10');
37+
```
38+
39+
```
40+
┌──────────────────────────────────────────────────────┐
41+
│ convert_timezone('America/Los_Angeles', '2024-11-01… │
42+
├──────────────────────────────────────────────────────┤
43+
│ 2024-11-01 04:36:10.000000 │
44+
└──────────────────────────────────────────────────────┘
45+
```
46+
47+
### 按用户偏好转换多行数据
48+
49+
```sql
50+
SELECT
51+
user_tz,
52+
event_time,
53+
CONVERT_TIMEZONE(user_tz, event_time) AS local_time
54+
FROM (
55+
VALUES
56+
('America/Los_Angeles', '2024-10-31 22:21:15'::TIMESTAMP),
57+
('Asia/Shanghai', '2024-10-31 22:21:15'::TIMESTAMP),
58+
(NULL, '2024-10-31 22:21:15'::TIMESTAMP)
59+
) AS v(user_tz, event_time)
60+
ORDER BY user_tz NULLS LAST;
61+
```
62+
63+
```
64+
┌──────────────────────┬──────────────────────────────┬──────────────────────────────┐
65+
│ user_tz │ event_time │ local_time │
66+
├──────────────────────┼──────────────────────────────┼──────────────────────────────┤
67+
│ America/Los_Angeles │ 2024-10-31 22:21:15.000000 │ 2024-10-31 15:21:15.000000 │
68+
│ Asia/Shanghai │ 2024-10-31 22:21:15.000000 │ 2024-11-01 06:21:15.000000 │
69+
│ NULL │ 2024-10-31 22:21:15.000000 │ NULL │
70+
└──────────────────────┴──────────────────────────────┴──────────────────────────────┘
71+
```
72+
73+
### 处理落在夏令时缺口内的时间戳
74+
75+
本示例的会话已配置为 Asia/Shanghai 并开启 `enable_dst_hour_fix = 1`。由于 1947 年 4 月 15 日 00:00:00 在该时区并不存在(时钟向前跳一小时),Databend 会先调整后再返回其对应的 UTC 时间。
76+
77+
```sql
78+
SELECT CONVERT_TIMEZONE('UTC', '1947-04-15 00:00:00');
79+
```
80+
81+
```
82+
┌──────────────────────────────────────────────┐
83+
│ convert_timezone('UTC', '1947-04-15 00:00:00')│
84+
├──────────────────────────────────────────────┤
85+
│ 1947-04-14 15:00:00.000000 │
86+
└──────────────────────────────────────────────┘
87+
```
88+
89+
## 另请参阅
90+
91+
- [TIMEZONE](timezone.md)
92+
- [TO_TIMESTAMP_TZ](to-timestamp-tz.md)
93+
- [TO_TIMESTAMP](to-timestamp.md)

0 commit comments

Comments
 (0)