

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon QLDB 中的 TO\$1TIMESTAMP 函数
<a name="ql-functions.to_timestamp"></a>

**重要**  
终止支持通知：现有客户将能够使用 Amazon QLDB，直到 2025 年 7 月 31 日终止支持。有关更多详细信息，请参阅[将亚马逊 QLDB 账本迁移到亚马逊 Aurora PostgreSQL](https://aws.amazon.com/blogs/database/migrate-an-amazon-qldb-ledger-to-amazon-aurora-postgresql/)。

在 Amazon QLDB，如果给定一个表示时间戳的字符串，则使用函数`TO_TIMESTAMP`将该字符串转换为`timestamp`数据类型。这是 `TO_STRING` 的逆运算。

## 语法
<a name="ql-functions.to_timestamp.syntax"></a>

```
TO_TIMESTAMP ( string [, 'format' ] )
```

## Arguments
<a name="ql-functions.to_timestamp.arguments"></a>

*string*  
函数转换为时间戳的数据类型 `string` 的字段名称或表达式。

*format*  
（可选）用日期部分定义输入*string*格式模式的字符串文字。有关有效格式，请参阅[时间戳格式字符串](ql-functions.timestamp-format.md)。  
如果省略此参数，则该函数假定采用[标准 Ion 时间戳](http://amzn.github.io/ion-docs/docs/spec.html#timestamp)的格式。*string*建议使用此函数解析 Ion 时间戳。  
使用单字符格式符号（例如`y`、`M`、`d`、`H`、`h`、`m`、`s`）时，可选择零填充，但是零填充变体为必填项（如`yyyy`、`MM`、`dd`、`HH`、 `hh`、`mm`、`ss`）。  
对两位数年份（格式符号`yy`）给予特殊处理。大于或等于 70 的值加上 1900，小于 70 的值加上 2000。  
月份名称和上午或下午指标不区分大小写。

## 返回类型
<a name="ql-functions.to_timestamp.return-type"></a>

`timestamp`

## 示例
<a name="ql-functions.to_timestamp.examples"></a>

```
TO_TIMESTAMP('2007T')                         -- `2007T`
TO_TIMESTAMP('2007-02-23T12:14:33.079-08:00') -- `2007-02-23T12:14:33.079-08:00`
TO_TIMESTAMP('2016', 'y')                     -- `2016T`
TO_TIMESTAMP('2016', 'yyyy')                  -- `2016T`
TO_TIMESTAMP('02-2016', 'MM-yyyy')            -- `2016-02T`
TO_TIMESTAMP('Feb 2016', 'MMM yyyy')          -- `2016-02T`
TO_TIMESTAMP('February 2016', 'MMMM yyyy')     -- `2016-02T`

-- Runnable statements
SELECT TO_TIMESTAMP('2007T') FROM << 0 >>              -- 2007T
SELECT TO_TIMESTAMP('02-2016', 'MM-yyyy') FROM << 0 >> -- 2016-02T
```

## 相关函数
<a name="ql-functions.to_timestamp.related"></a>
+ [CAST](ql-functions.cast.md)
+ [DATE\$1ADD](ql-functions.date_add.md)
+ [DATE\$1DIFF](ql-functions.date_diff.md)
+ [EXTRACT](ql-functions.extract.md)
+ [TO\$1STRING](ql-functions.to_string.md)
+ [UTCNOW](ql-functions.utcnow.md)