Commonly used time and date scripts in PostgreSQL tutorial, postgresql tutorial
Obtain System Time Functions
select now(); --2013-11-28 16:20:25.259715+08select current_timestamp; --2013-11-28 16:20:38.815466+08select current_date; --2013-11-28select current_time; --16:21:08.981171+08
Time Calculation
-- Use interval
Select now () + interval '2 Day'; -- 16:21:47. 610118 + 08 2 days later select now ()-interval '2 Day'; -- 16:22:03. 390593 + 08 2 days ago select now () + interval '2 hour '; -- 18:22:14. 578733 + 08 2 hours later
-- Interval can be left empty, and its value can be
-- Abbreviation Meaning -- Y Years -- M Months (in the date part) -- W Weeks -- D Days -- H Hours -- M Minutes (in the time part)
Time Truncation
-- Use extract (interval, timestamp );
Select extract (year from now (); -- 2013 select extract (mon from now (); -- January
Time Conversion
select timestamp '2012-05-12 18:54:54'; --2012-05-12 18:54:54select date '2012-05-12 18:54:54'; --2012-05-12select time '2012-05-12 18:54:54'; --18:54:54select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54' --2012-05-12 18:54:54+08
Conversion from unix Timestamp
SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second'; --2012-07-01 20:32:47
Instance
1. Current Time/date/Timestamp
There are many ways to get the current time. Before that, we need to know the differences between the following two types:
Always Returns the current value (clock_timestamp ())
Always Returns the current value, but in the transaction it returns the start time of the transaction (now ())
Let's take a look at the example below
postgres=# BEGIN; postgres=# SELECT now(); now ------------------------------- 2013-08-26 12:17:43.182331+02 postgres=# SELECT now(); now ------------------------------- 2013-08-26 12:17:43.182331+02 postgres=# SELECT clock_timestamp(); clock_timestamp ------------------------------- 2013-08-26 12:17:50.698413+02 postgres=# SELECT clock_timestamp(); clock_timestamp ------------------------------- 2013-08-26 12:17:51.123905+02
You will find that the clock_timestamp () returned value changes every time when the statement is executed, but now () always returns the same value. When you need to consider the time zone, you should pay special attention to the differences between the two functions.
2. Time Range: for example, 3 days ago
Using the interval operator, you can easily build a time interval, for example
interval '1 day'interval '5 days'interval '5 days' + interval '3 hours'interval '5 days 3 hours'
As you can see, we can use the interval operator for simple mathematical operations. This is especially suitable for building time ranges such as three days ago, for example:
postgres=# SELECT now() - interval '3 days'; ?column? ------------------------------- 2013-08-23 12:23:40.069717+02
3. Obtain the day of the week
Sometimes, for a given time, you only want to know the day of the week, the day of the century, or the day of the year. The extract () function in PostgreSQL provides this function.
The following example is a test conducted on Monday, August 26.
postgres=# SELECT extract(DAY FROM now()); date_part ----------- 26 postgres=# SELECT extract(DOW FROM now()); date_part ----------- 1
4. Time Zone Conversion
Sometimes, time zone conversion is particularly useful for displaying specific time zones in different time zones. At time zone provides this function. How does it do it? We will demonstrate it in a transaction, because the now () function always returns the same value in the same transaction, so that we can easily see the differences displayed in different time zones at the same time.
postgres=# BEGIN; BEGIN postgres=# SELECT now(); now ------------------------------- 2013-08-26 12:39:39.122218+02 postgres=# SELECT now() AT TIME ZONE 'GMT'; timezone ---------------------------- 2013-08-26 10:39:39.122218 postgres=# SELECT now() AT TIME ZONE 'GMT+1'; timezone ---------------------------- 2013-08-26 09:39:39.122218 postgres=# SELECT now() AT TIME ZONE 'PST'; timezone ---------------------------- 2013-08-26 02:39:39.122218