pg_when

PostgreSQL 自然语言时间解析扩展

概览

扩展包名版本分类许可证语言
pg_when0.1.9TIMEMITRust
ID扩展名BinLibLoadCreateTrustReloc模式
1120pg_when-

manually upgraded PGRX from 0.15.0 to 0.17.0 by Vonng

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.1.91817161514pg_when-
RPMPIGSTY0.1.91817161514pg_when_$v-
DEBPIGSTY0.1.91817161514postgresql-$v-pg-when-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u24.x86_64
u24.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9

构建

您可以使用 pig build 命令构建 pg_when 扩展的 RPM / DEB 包:

pig build pkg pg_when         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_when 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

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;

用法

来源: README项目仓库

pg-when 是一个 PostgreSQL 扩展,用于根据自然语言短语生成时间值。 它通过同一份解析结果提供多种返回格式:when_isseconds_atmillis_atmicros_atnanos_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 中列出的相对日期示例包括:

  • today
  • yesterday
  • tomorrow
  • next week
  • last month
  • this friday
  • 5 days ago
  • in 2 years

精确日期可以写成以下格式:

  • YYYY-MM-DDYYYY/MM/DD
  • DD-MM-YYYYDD/MM/YYYY
  • Month D, YYYY
  • D Month YYYY

<time> 也可以是相对时间或精确时间。

相对时间示例包括:

  • noon
  • midnight
  • morning
  • evening
  • next hour
  • previous minute
  • this hour

精确时间可以用 12 小时制或 24 小时制表示,例如 8:30 pm15:45

时区既支持 IANA 名称,也支持 UTC 偏移量,例如 America/New_YorkUTC-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 镜像。 这与本仓库中的包元数据一致。


最后修改 2026-04-14: update extension catalog (fa7cf58)