How Do I Get the Current Time Without Time Zone in PostgreSQL

In PostgreSQL, a wide range of in-built functions are available that provide the current time, such as NOW(), CURRENT_TIME(), LOCALTIME, LOCALTIMESTAMP, etc. Some of them retrieve the current time along with the date while some don’t. Similarly, some functions provide only current time while some of them provide current time with zone information.

In this blog post, we will discuss how to get the current time without the zone offset in Postgres.

How Do I Get the Current Time Without Time Zone in Postgres?

In Postgres, the “LOCALTIME” function is used to get the current time without zone information. It is executed without parentheses; however, you can use the parentheses if you have to get the time with a particular precision. The below snippet displays the syntax of the stated function:

LOCALTIME(pre);

“pre” represents precision which is optional and can be omitted.

Example 1: Getting Current Time Using LOCALTIME

In the following snippet, we will utilize the LOCALTIME function without a precision parameter:

SELECT LOCALTIME;

The stated function will retrieve the current time without zone offset:

img

The first two digits “09” represent hours, and the next two digits “06” represent minutes. Similarly, “27” represents seconds while “835257” represents fractional seconds.

Example 2: Using LOCALTIME With Precision

Below is the example code of utilizing the LOCALTIME function with a precision parameter:

SELECT LOCALTIME(0);
img

The output depicts that passing “0” to the stated function trims the fractional seconds from the retrieved time.

Note: Both CURRENT_TIME and LOCALTIME functions retrieve current time however, the first one provides time zone information while the second one doesn’t.

Example 3: Using LOCALTIME With Table’s Data

Execute the SELECT query with the “*” wildcard to fetch all records of the “e_data” table:

SELECT * FROM e_data;
img

Let’s execute the “INSERT” query to add the current time to the “e_data” table:

INSERT INTO e_data(e_name, sign_in)
VALUES ('John', LOCALTIME(0));
img

A new record has been successfully inserted into the “e_data” table, which can be verified by executing the following query:

SELECT * FROM e_data;
img

The output snippet depicts that the current time has been successfully inserted into the selected table using the LOCALTIME function.

Conclusion

In Postgres, the “LOCALTIME” function is used to get the current time without zone information. It is executed without parentheses; however, you can use the parentheses if you have to get the time with particular precision. This post has presented a comprehensive guide on getting current time without time zone offset.