1

I have a column with a timestamp. The converted column gives me a datetime value starting from the year 1975. That means I should concat() that value with '0' and convert it to the correct datetime.

I wrote something, and it shows me very close values, but not the same when I double-check it in browser.

Example:

select to_timestamp(
          cast(
             dt:double precision/1000 as bigint
          )
       ) as ts, *
from (select col1, col2, concat(timestamp, '0') as dt
      from table1
      order by col1 desc) as foo

For example, with the initial timestamp 168665720100, after concat and to_timestamp I get 2023-06-13 14:53:21, when it should be 2023-06-13 11:53:21.

1
  • 1
    to_timestamp gives you a timestamp with time zone, so probably the timezone parameter in your session is set to a time zone offset three hours from UTC. Which means that the result could correct, and all you have to do is append AT TIME ZONE 'UTC'. Commented Jun 14, 2023 at 6:15

1 Answer 1

2

It will depend in which timezone your database / session is set.

This example shows same value you posted using a different timezone: 'Australia/Brisbane'

SET timezone TO 'Australia/Brisbane';

SELECT to_timestamp(1686657201000/1000);

      to_timestamp      
------------------------
 2023-06-13 21:53:21+10
(1 row)

So, all you need to do is to set the timezone that best fit your location using AT TIME ZONE, e.g. UTC

SET timezone TO 'Australia/Brisbane';

SELECT to_timestamp(1686657201000/1000) AT TIME ZONE 'UTC';

      timezone       
---------------------
 2023-06-13 11:53:21
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.