PostGreSQL: timestamp time zone issue

time | date type

        The built-in time types of the PostGreSQL database are as follows. Note that the built-in time types are divided into two types: with time zone and without time zone.

   time, timestampand interval都可以accept an optional precision value  p(value: 0-6), which declares the number of digits to be retained after the decimal point in the seconds field. By default, there is no clear boundary on precision.

Acceptable input formats for time|date type

        In actual use, for a time type, we are usually very concerned about how to insert and update fields of this type. Then we must consider: What input format can PostGreSQL accept? For PGSQL, date and time input can accept almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES and other forms.

        As shown in the table below, PG officially recommends using ISO 8601 format for time and date string input.

Timestamp: input format | current time

Timestamp input format

        Compared to the above part, this article is more concerned with content related to timestamps.

        Valid inputs for the timestamp type: consist of a concatenation of date and time, followed by an optional time zone, therefore, both of the following inputs are valid (both follow the ISO 8601 standard),

1999-01-08 04:05:06
1999-01-08 04:05:06 -8:00

timestamp without time zoneThe SQL standard distinguishes and         literals by the presence of a "+" or "-" symbol and a time zone offset after the time timestamp with time zone. Therefore, according to the standard,

        ①TIMESTAMP '2004-10-19 10:23:54': is atimestamp without time zone;

   ②TIMESTAMP '2004-10-19 10:23:54+02': is atimestamp with time zone。

Get current timestamp     

         Let’s first get a current timestamp. PGSQL provides the following two ways to get it,

CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_TIME(precision)
CURRENT_TIMESTAMP(precision)
LOCALTIME
LOCALTIMESTAMP
LOCALTIME(precision)
LOCALTIMESTAMP(precision)

Note the difference between CURRENT prefix and LOCAL prefix functions:

  CURRENT_TIMEand CURRENT_TIMESTAMPpassing a value with time zone;

  LOCALTIMEand LOCALTIMESTAMPthe value passed without time zone.

        Then take a look at the official example:

SELECT CURRENT_TIME;
结果:14:39:53.662522-05

SELECT CURRENT_DATE;
结果:2001-12-23

SELECT CURRENT_TIMESTAMP;
结果:2001-12-23 14:39:53.662522-05

SELECT CURRENT_TIMESTAMP(2);
结果:2001-12-23 14:39:53.66-05

SELECT LOCALTIMESTAMP;
结果:2001-12-23 14:39:53.662522

        Taking 2023-09-09 11:16:56.745139+08 as an example, it means: September 9, 2023, 11:16 am 56.745139 seconds, +08 East Eighth District.

Time|Date output format

        Time|date formatting is also an unavoidable issue when we do project development and database operations. The output format of PostGreSQL's time/date type can be set to one of four styles: ISO 8601, SQL (Ingres), traditional POSTGRES ( Unix date format) or German. The default is ISO format.

        The following are examples of 4 formats,

        According to PG’s official description, there are two ways to implement data formatting operations:

① The date/time style can SET datestylebe selected by the user using a command, set by the parameter DateStylepostgresql.conf in the configuration file or set in the environment variable of the server or client .PGDATESTYLE

②The format function to_char(see Section 9.8 ) can also be used as a more flexible way to format date/time output.

        Let’s take a look at type 2.

Data type formatting functions

        PostgreSQL formatting functions provide a powerful set of tools for converting various data types (date/time, integer, floating point, number) into formatted strings and vice versa. type.

        It can be seen that through the formatting function, the interconversion operation between timestamps and strings can be realized. For the format control parameter template string of the formatting string, see: 9.8. Data type formatting function (postgres.cn ) , Table 9.25.

        For example: We want to convert the current timestamp into a string format similar to: yyyy-MM-dd hh:mm:ss (24-hour format), then the SQL statement is:


SELECT to_char(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS')

--输出
2023-09-09 11:29:02

        Next, we convert the above output string into a timestamp,

SELECT to_timestamp('2023-09-09 11:29:02', 'YYYY-MM-DD HH24:MI:SS')

--
2023-09-09 11:29:02+08

About time zones

        PG’s official explanation of the time zone issue is as follows. It is not recommended to use the type with time zone.

PostgreSQL strives to be compatible with the definitions of the SQL standard in typical usage. But the SQL standard has some weird confusion over date and time types and functions. The two obvious questions are:

  • Although datetypes are not associated with time zones, timetypes can be. However, real-world time zones only make sense when associated with both time and date, since offsets (time differences) may vary throughout the year due to systems like daylight saving time.

  • The default time zone specifies a numeric constant offset (time difference) from UTC

We recommend using date/time types that contain both date and time when working with time zones. We do not recommend using types  time with time zone (although PostgreSQL supports them for legacy applications and for compatibility with the SQL standard) . PostgreSQL assumes that the local time zone you use for any type only contains dates or times.

        For information on the concepts and functions of UTC and ISO 8601, please refer to: Time Standard Basics UTC and ISO8601 .

        View the time zone currently used by the database,


show time zone
--Asia/Shanghai

        View the available time zones for your database:

select * from pg_timezone_names;

        set time zone,

①Temporary setting: After exiting the cmd terminal, the time zone setting will be lost.

set time zone "Asia/Shanghai"

②Permanent settings: modify the configuration file,

        If we want to make permanent changes, we need to change the configuration file postgresql.conf and modify the timezone.

log_timezone = 'PRC' timezone = 'PRC'

        Set the values ​​of these two variables in the configuration file to the time zone you want (PRC refers to: People's Republic of China), and then reload: pg_ctl reload.

Guess you like

Origin blog.csdn.net/weixin_43524214/article/details/132774857