0

I use postgres for the rails app and I have a unix timestamp in postgresql db. I have a requirement to select and group by the dd-mm-yyyy and by month name.

Consider I have the following unix timestamp

1425148200

and I would need to change this to datetime and I used to_timestamp which returned

2015-02-28 18:30:00 UTC

and I tried to convert the datetime to local timezone using

::timestamp without time zone AT TIME ZONE 'IST'

but that did not give time in required timezone and instead it returned

2015-02-28 16:30:00 UTC

and I tried to get the date part using ::date which returned

Sat, 28 Feb 2015

So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp.

Thanks in Advance!

4
  • did not return time in required timezone - what does it mean? what return? Commented Mar 20, 2015 at 10:54
  • I have updated the result of :timestamp without time zone AT TIME ZONE 'IST' Commented Mar 20, 2015 at 10:57
  • Would this: select DATE(to_timestamp(1425148200)) be what you want? This would return 2015-02-28, date in format requested by you. Commented Mar 20, 2015 at 11:45
  • Welcome to TimeZone hell of Ruby(Rails) Commented Mar 20, 2015 at 11:59

1 Answer 1

2

select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')

01-03-2015 & of course March

It is postgres mistake I guess according to http://www.postgresql.org/docs/7.2/static/timezones.html enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

The time corresponding to '1425148200' in IST is "3/1/2015, 12:00:00 AM GMT+5:30" but it gives as 2015-02-28 20:30:00 UTC
@Logesh there are four time zones with ISTabbreviation. postgres knows Israel one
Yes, I looked into that and I could somehow solve this with the solution that you gave and I also tried getting the offset and adding to timestamp and that too works. Thanks anyway.

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.