Convert Julian Date to Timestamp in PostgreSQL - postgresql

I have a table full of "Julian Dates", that is the number of days and seconds away from 1/1/2035. I need to convert these to normal postgres timestamps. Can anyone help?
--Converts '2000-06-20 12:30:15' into an Epoch time base which gives a result of -12612.478993055556
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp - '2035-01-01 00:00:00'))/86400.00) as run_ts
--Question, how to convert -12612.478993055556 back into '2000-06-20 12:30:15'
select -12612.478993055556 ??? as run_ts

You can use to_timestamp() to convert an epoch to a timestamp.
The epoch you posted does not correspond to 2000-06-20, as you have removed another date 2035-01-01 from it.
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp )));
date_part
-----------
961504215
(1 row)
select to_timestamp(961504215);
to_timestamp
------------------------
2000-06-20 08:30:15-04
(1 row)
select to_timestamp(-12612.478993055556);
to_timestamp
-------------------------------
1969-12-31 15:29:47.521007-05
(1 row)
EDIT
Since you are not considering a true epoch but really a difference between two dates, you can simply add this difference to the reference date. You can use the day interval to remove the need to multiply by 86400 (seconds/day)
select '2035-01-01 00:00:00'::timestamp + interval '1' day * -12612.478993055556;
?column?
---------------------
2000-06-20 12:30:15

Related

Postgresql timestamp difference greater than 1 hour

Hi I have a entrytime and exittime timestamp in my database, how can I query it to display only ones where the person exited more than an hour later;
Select * from store where EXTRACT(EPOCH FROM (exittime - entrytime))/3600 >60
That's what I have so far but it won't work, any help would be appreciated.
Just subtract the values and compare it with an interval
Select *
from store
where exittime - entrytime > interval '1 hour';
This assumes that both columns are defined as timestamptz or timestamp

postgresql - NOW() but less specific

When I use SELECT NOW(); I'll get an output like this:
now
-------------------------------
2019-09-09 18:55:38.794006-05
(1 row)
I want it like this:
now
-------------------
2019-09-09 18:55:38
(1 row)
How do I make NOW() round up/down accordingly? I tried SELECT NOW()::timestamptz(0); but it keeps adding -05 to the end of the time :(
You can to_char
select to_char(now(), 'YYYY-MM-DD HH:MI:SS')
or use date_truc to retain datatype timestamp with time zone
select date_trunc('minute', now());
Convert NOW() to a timestamp without timezone:
SELECT NOW()::timestamp(0)

When a DST day has 82800 seconds, why half a day is 43600?

I have next two queries to calculate a day and a half of day
tucha=> select extract( epoch from '2019-03-31'::timestamptz +interval '1day' - '2019-03-31'::timestamptz );
date_part
-----------
82800
(1 row)
tucha=> select extract( epoch from '2019-03-31'::timestamptz +interval '0.5day' - '2019-03-31'::timestamptz );
date_part
-----------
43200
(1 row)
if one day is 82800, then a half a day should be 41400, is not?
tucha=> select extract( epoch from '2019-04-01'::timestamptz -'2019-03-31'::timestamptz )/2;
?column?
----------
41400
(1 row)
NOTICE postgresql actually calculate 43200, but I expect 41400 as it calculated for last expression
NOTICE both: 1day and 0.5day periods contain time when DST occur
UPD
Is there a spec about how half a day is calculated for days when DST occur?

How to truncate a date to the beginning of week (Sunday)?

I need to truncate dates to the start of week, which is Sunday in my case. How can I do this in PostgreSQL? This truncates to Monday:
date_trunc('week', mydate)
If you subtract the dow value (0 for Sundays, 6 for Saturdays) from the current date than you get the previous Sunday which is the begin of your Sunday-based week
demo:db<>fiddle
SELECT
my_date - date_part('dow', my_date)::int
FROM
my_table
Further reading, documentation
You could truncate the date to the week's Monday, then subtract 1 day, e.g:
SELECT (date_trunc('week', now() + interval '1 day') - interval '1 day')::DATE;
date
------------
2019-06-16
As per documentation, date_trunc() accepts values of type date and timestamp and returns a timestamp (thus the cast at the end).

postgresql error : Multiple decimal points

So I'm having this query:
SELECT
TO_CHAR(date_part('hour', created_at), 'YYYY-MM-DD HH24'),
to_char(created_at, 'day') ",
COUNT(*) AS "
FROM table
GROUP BY 1,2
ORDER BY 1 DESC
When I execute the query I get this:
ERROR: multiple decimal points
Searching stackoverflow I found some recommendations here:
How to format bigint field into a date in Postgresql? but I don't get why do I have to divide by 1000 and how this would apply in the case of the date_part function.
I assume created_at is a timestamp?.. I'm choosing from date_part(text, timestamp) and date_part(text, interval), if so date_part will return a double precision, to which you try to apply the mask 'YYYY-MM-DD HH24', eg:
v=# select date_part('hour', now());
date_part
-----------
9
and I don't see how you could possibly get year, month, day and hour from nine...
Yet I assume you wanted to apply the mask against truncated date to the hour precision, which is done with date_trunc(text, timestamp):
v=# select date_trunc('hour', now());
date_trunc
------------------------
2017-06-20 09:00:00+01
(1 row)
so now you can apply the time format:
v=# select to_char(date_trunc('hour', now()),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
but if this is what you want, then you don't need to truncate time at all:
v=# select to_char(now(),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
https://www.postgresql.org/docs/current/static/functions-datetime.html