欢迎投稿

今日深度:

PostgreSQL设置时区、时间/日期函数汇总大全,

PostgreSQL设置时区、时间/日期函数汇总大全,


目录
  • 前言
  • 查看时区
  • 修改时区
  • 时间/日期操作符和函数
    • 时间/日期操作符
    • 日期/时间函数:
    • extract,date_part函数支持的field
  • 数据类型格式化函数
    • 用于日期/时间格式化的模式:
  • 扩展
    • 总结 

      前言

      本文基于 PostgreSQL 12.6 版本,不同版本的函数可能存在差异。

      查看版本 psql --version

      查看时区

      show timezone; --UTC
      select now(); -- 2023-07-24 09:22:48.589640 +00:00

      视图 pg_timezone_names 保存了所有可供选择的时区

      select * from pg_timezone_names;

      查询 PRC 时区

      select * from pg_timezone_names where name = 'PRC';

      PRC是指中华人民共和国 PRC(People’s Republic of China)。

      修改时区

      修改时区,设置成东八区 北京时间 UTC+8,默认为session级配置

      set time zone 'PRC';
      select now(); -- 2023-07-24 17:21:05.086183 +08:00

      修改时区,用户级配置

      alter role rolname set timezone='UTC'; -- 修改指定角色时区(rolname为角色名)
      alter role all set timezone='UTC';     -- 修改所有角色时区

      修改时区,数据库级配置

      alter database dbname set timezone='UTC'; -- dbname为数据库名称

      时间/日期操作符和函数

      时间/日期操作符

      操作符例子返回类型结果
      +select date ‘2023-07-24’ + integer ‘7’;date2023-07-31
      +select date ‘2023-07-24’ + interval ‘1 hour’;timestamp2023-07-24 01:00:00.000000
      +select date ‘2023-07-24’ + time ‘15:16’;timestamp2023-07-24 15:16:00.000000
      +select interval ‘1 day’ + interval ‘1 hour’;interval0 years 0 mons 1 days 1 hours 0 mins 0.0 secs
      +select timestamp ‘2023-07-24 15:16’ + interval ‘23 hours’;timestamp2023-07-25 14:16:00.000000
      +select time ‘01:00’ + interval ‘3 hours’;time04:00:00
      -select - interval ‘23 hours’;interval0 years 0 mons 0 days -23 hours 0 mins 0.0 secs
      -select date ‘2023-07-24’ - date ‘2023-07-22’;integer2
      -select date ‘2023-07-24’ - integer ‘7’;date2023-07-17
      -select date ‘2023-07-24’ - interval ‘1 hour’;timestamp2023-07-23 23:00:00.000000
      -select time ‘05:00’ - time ‘03:00’;interval0 years 0 mons 0 days 2 hours 0 mins 0.0 secs
      -select time ‘05:00’ - interval ‘2 hours’;time03:00:00
      -select timestamp ‘2023-07-24 23:00’ - interval ‘23 hours’;timestamp2023-07-24 00:00:00.000000
      -select interval ‘1 day’ - interval ‘1 hour’;interval0 years 0 mons 1 days -1 hours 0 mins 0.0 secs
      -select timestamp ‘2023-07-24 03:00’ - timestamp ‘2023-07-24 12:00’;interval0 years 0 mons 0 days -9 hours 0 mins 0.0 secs
      *select interval ‘1 hour’ * double precision ‘3.5’;interval0 years 0 mons 0 days 3 hours 30 mins 0.0 secs
      /select interval ‘1 hour’ / double precision ‘1.5’;interval0 years 0 mons 0 days 0 hours 40 mins 0.0 secs

      日期/时间函数:

      函数描述例子返回类型结果
      age(timestamp, timestamp)第1个timestamp 减去 第2个timestampselect age(‘2023-07-24’, ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secs
      age(timestamp)从current_date 减去 timestamp的值select age(timestamp ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secs
      current_date今天的日期select current_date;date2023-07-24
      current_time现在的时间select current_time;time07:53:43.911756 +00:00
      current_timestamp日期和时间select current_timestamp;timestamp2023-07-24 07:54:19.495372 +00:00
      date_part(text, timestamp)获取子域(等效于extract)select date_part(‘hour’, timestamp ‘2023-07-24 15:56:34’);double15
      date_part(text, interval)获取子域(等效于extract)select date_part(‘month’, interval ‘2 years 3 months’);double3
      date_trunc(text, timestamp)截断成指定的精度select date_trunc(‘hour’, timestamp ‘2023-07-24 15:56:34’);timestamp2023-07-24 15:00:00.000000
      extract(field from timestamp)获取子域select extract(hour from timestamp ‘2023-07-24 15:56:34’);double15
      extract(field from interval)获取子域select extract(month from interval ‘2 years 3 months’);double3
      localtime当前时间select localtime;time08:00:08
      localtimestamp当前日期和时间select localtimestamp;timestamp2023-07-24 08:05:03.650472
      now()当前的日期和时间(等效于current_timestamp)select now();timestamp2023-07-24 08:09:30.828408 +00:00
      timeofday()当前日期和时间select timeofday();textMon Jul 24 08:09:51.870484 2023 UTC

      extract,date_part函数支持的field

      extractdate_part 这两个函数可以从日期时间值中提取指定的部分,例如年份、月份、小时等。extract 是一个 PostgreSQL 特有的函数,而 date_part 在标准 SQL 中也有定义,但两者的功能类似。

      描述例子结果
      century世纪select extract(century from timestamp ‘2023-07-24 15:56:34’);21
      day(月份)里的日期域(1-31)select extract(day from timestamp ‘2023-07-24 15:56:34’);24
      decade年份域除以10select extract(decade from timestamp ‘2023-07-24 15:56:34’);202
      dow每周的星期号(0-6;星期天是0) (仅用于timestamp)select extract(dow from timestamp ‘2023-07-24 15:56:34’);1
      doy一年的第几天(1 -365/366) (仅用于 timestamp)select extract(doy from timestamp ‘2023-07-24 15:56:34’);205
      epochUnix时间戳select extract(epoch from timestamp ‘2023-07-24 15:56:34’);1690214194
      hour小时域(0-23)select extract(hour from timestamp ‘2023-07-24 15:56:34’);15
      isodowISO 周几(1-7,其中1代表星期一)select extract(isodow from timestamp ‘2023-07-24 15:56:34’);1
      isoyearISO 年份select extract(isoyear from timestamp ‘2023-07-24 15:56:34’);2023
      millennium千年((年份/1000)+1)select extract(millennium from timestamp ‘2023-07-24 15:56:34’);3
      microseconds微秒select extract(microseconds from TIME ‘15:56:34.5’);34500000
      millisecond毫秒select extract(millisecon from TIME ‘15:56:34.5’);34500
      minute分钟(0-59)select extract(minute from timestamp ‘2023-07-24 15:56:34’);56
      month月份,对于timestamp数值,它是一年里的月份数(1-12);对于interval数值,它是月的数目,然后对12取模(0-11)select extract(month from timestamp ‘2023-07-24 15:56:34’);7
      quarter季度,该天所在的该年的季度(1-4)(仅用于 timestamp)select extract(quarter from timestamp ‘2023-07-24 15:56:34’);3
      second秒域,包括小数部分(0-59[1])select extract(second from timestamp ‘2023-07-24 15:56:34’);34
      week该天在所在的年份里是第几周。select extract(week from timestamp ‘2023-07-24 15:56:34’);30
      year年份域select extract(year from timestamp ‘2023-07-24 15:56:34’);2023

      数据类型格式化函数

      PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间、integer、floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。

      to_char 函数第一个参数是待格式化的值,而第二个是定义输出或输出格式的模板。

      函数描述例子返回类型结果
      to_char(timestamp, text)把时间戳转换成字串select to_char(current_timestamp, ‘HH12:MI:SS’);text06:03:19
      to_char(interval, text)把时间间隔转为字串select to_char(interval ‘14h 6m 20s’, ‘HH24:MI:SS’);text14:06:20
      to_date(text, text)把字串转换成日期select to_date(‘25 Jul 2023’, ‘DD Mon YYYY’);date2023-07-24
      to_timestamp(text, text)把字串转换成时间戳select to_timestamp(‘25 Jul 2023’, ‘DD Mon YYYY’);timestamp2023-07-24 00:00:00.000000 +00:00
      to_timestamp(double)把UNIX纪元转换成时间戳select to_timestamp(1690179888);timestamp2023-07-24 06:24:48.000000 +00:00

      用于日期/时间格式化的模式:

      模式描述
      HH一天的小时数(01-12)
      HH12一天的小时数(01-12)
      HH24一天的小时数(00-23)
      MI分钟(00-59)
      SS秒(00-59)
      MS毫秒(000-999)
      US微秒(000000-999999)
      AM正午标识(大写)
      Y,YYY带逗号的年(4和更多位)
      YYYY年(4和更多位)
      YYY年的后三位
      YY年的后两位
      Y年的最后一位
      MONTH全长大写月份名(空白填充为9字符)
      Month全长混合大小写月份名(空白填充为9字符)
      month全长小写月份名(空白填充为9字符)
      MON大写缩写月份名(3字符)
      Mon缩写混合大小写月份名(3字符)
      mon小写缩写月份名(3字符)
      MM月份号(01-12)
      DAY全长大写日期名(空白填充为9字符)
      Day全长混合大小写日期名(空白填充为9字符)
      day全长小写日期名(空白填充为9字符)
      DY缩写大写日期名(3字符)
      Dy缩写混合大小写日期名(3字符)
      dy缩写小写日期名(3字符)
      DDD一年里的日子(001-366)
      DD一个月里的日子(01-31)
      D一周里的日子(1-7;周日是1)
      W一个月里的周数(1-5)(第一周从该月第一天开始)
      WW一年里的周数(1-53)(第一周从该年的第一天开始)

      示例:

      -- 查询今天是今年的第几天
      select to_char(now(), 'DDD'); -- 205

      扩展

      查询某个日期是否在某段日期范围,可以使用 > 和 < 判断;如果使用了 between ,则前一个日期必须小于后一个日期。

      示例:

      -- between 错误用法
      select date '2023-07-24' between date('2023-07-25') - 1 and date('2023-07-25') - 7; -- false
      -- between 正确用法
      select date '2023-07-24' between date('2023-07-25') - 7 and date('2023-07-25') - 1; -- true

      总结 

      到此这篇关于PostgreSQL设置时区、时间/日期函数汇总的文章就介绍到这了,更多相关PostgreSQL设置时区时间内容请搜索PHP之友以前的文章或继续浏览下面的相关文章希望大家以后多多支持PHP之友!

      您可能感兴趣的文章:
      • 在postgresql数据库中判断是否是数字和日期时间格式函数操作
      • PostgreSQL中的日期/时间函数详解
      • postgresql常用日期函数使用整理

      www.htsjk.Com true http://www.htsjk.com/shujukunews/47452.html NewsArticle PostgreSQL设置时区、时间/日期函数汇总大全, 目录 前言 查看时区 修改时区 时间/日期操作符和函数 时间/日期操作符 日期/时间函数: extract,date_part函数支持的field 数据类型格式化函...
      评论暂时关闭