pg_when
PostgreSQL 自然语言时间解析扩展
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_when | 0.1.9 | TIME | MIT | Rust |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 1120 | pg_when | 否 | 是 | 否 | 是 | 否 | 否 | - |
manually upgraded PGRX from 0.15.0 to 0.17.0 by Vonng
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.1.9 | 1817161514 | pg_when | - |
| RPM | PIGSTY | 0.1.9 | 1817161514 | pg_when_$v | - |
| DEB | PIGSTY | 0.1.9 | 1817161514 | postgresql-$v-pg-when | - |
构建
您可以使用 pig build 命令构建 pg_when 扩展的 RPM / DEB 包:
pig build pkg pg_when # 构建 RPM / DEB 包
安装
您可以直接安装 pg_when 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_when; # 当前活跃 PG 版本安装
pig ext install -y pg_when -v 18 # PG 18
pig ext install -y pg_when -v 17 # PG 17
pig ext install -y pg_when -v 16 # PG 16
pig ext install -y pg_when -v 15 # PG 15
pig ext install -y pg_when -v 14 # PG 14
dnf install -y pg_when_18 # PG 18
dnf install -y pg_when_17 # PG 17
dnf install -y pg_when_16 # PG 16
dnf install -y pg_when_15 # PG 15
dnf install -y pg_when_14 # PG 14
apt install -y postgresql-18-pg-when # PG 18
apt install -y postgresql-17-pg-when # PG 17
apt install -y postgresql-16-pg-when # PG 16
apt install -y postgresql-15-pg-when # PG 15
apt install -y postgresql-14-pg-when # PG 14
创建扩展:
CREATE EXTENSION pg_when;
用法
pg-when 是一个 PostgreSQL 扩展,用于根据自然语言短语生成时间值。
它通过同一份解析结果提供多种返回格式:when_is、seconds_at、millis_at、micros_at 和 nanos_at。
查询语法最多由三部分组成:
SELECT when_is('<date> at <time> in <timezone>');
SELECT when_is('<date>');
SELECT when_is('<time> in <timezone>');
SELECT when_is('<date> at <time>');
如果未指定时区,扩展默认使用 UTC。
支持的组件
<date> 可以是相对日期,也可以是精确日期。
README 中列出的相对日期示例包括:
todayyesterdaytomorrownext weeklast monththis friday5 days agoin 2 years
精确日期可以写成以下格式:
YYYY-MM-DD或YYYY/MM/DDDD-MM-YYYY或DD/MM/YYYYMonth D, YYYYD Month YYYY
<time> 也可以是相对时间或精确时间。
相对时间示例包括:
noonmidnightmorningeveningnext hourprevious minutethis hour
精确时间可以用 12 小时制或 24 小时制表示,例如 8:30 pm 或 15:45。
时区既支持 IANA 名称,也支持 UTC 偏移量,例如 America/New_York 或 UTC-08:00。
示例
SELECT when_is('5 days ago at this hour in Asia/Tokyo');
SELECT when_is('next friday at 8:00 pm in America/New_York');
SELECT when_is('in 2 months at midnight in UTC-8');
SELECT when_is('last monday at 22:30');
SELECT when_is('December 31, 2026 at evening');
部署
上游 README 展示了 PostgreSQL 13 到 18 的 Docker 镜像。 这与本仓库中的包元数据一致。