Last time I checked, I couldn't store a datetime _with_ timezone. It was really strange that such a powerful database doesn't support storing full-ISO datetimes, like '2017-01-01T00:00:00Z'. Instead, it converted it to date-time-only instant, losing information of original timezone along the way. Sure, I could fetch it back using any timezone I want, but I really wanted to know the original timezone it was in.
Would it be that much work to add a smallint field, that had the original UTC offset used for your time?
Working with time in Postgres
11–20 of 128 posts
Re: Working with time in Postgres
#12Earlier quoted context omitted.
Would it be that much work to add a smallint field, that had the original UTC offset used for your time?
Timezones are more than just an offset.
I'm sure a use case exists where you want to store a timezone, but I don't know what it is. I try and use UTC everywhere, and only worry about time zones when displaying.
Re: Working with time in Postgres
#13 with weeks as (
select week as week
from generate_series('2017-01-01'::date, now()::date, '1 week'::interval) weeks
),
SELECT weeks.week,
count(*)
FROM weeks,
test_results
WHERE
test_results.date_created > weeks.week
AND
test_results.date_created
Throws an error for me... ERROR: syntax error at or near "SELECT"
LINE 5: SELECT weeks.week,
^Re: Working with time in Postgres
#14 SELECT DATE_TRUNC('week', CURRENT_TIMESTAMP);
gives: 2017-06-05 00:00:00+00
vs: SELECT TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-WW"wk"');
gives: 2017-23wkRe: Working with time in Postgres
#15Earlier quoted context omitted.
Timezones are more than just an offset.
True. I'm sure a use case exists where you want to store a timezone, but I don't know what it is. I try and use UTC everywhere, and only worry about time zones when displaying.
Re: Working with time in Postgres
#16Weird, the example in the post (after changing table/field names for my database) with weeks as ( select week as week from generate_series('2017-01-01'::date, now()::date, '1 week'::interval) weeks ), SELECT weeks.week, count(*) FROM weeks, test_results WHERE test_results.date_created > weeks.week AND test_results.date_created Throws an error for me... ERROR: syntax error at or near "SELECT" LINE 5: SELECT weeks.week…
Re: Working with time in Postgres
#17Weird, the example in the post (after changing table/field names for my database) with weeks as ( select week as week from generate_series('2017-01-01'::date, now()::date, '1 week'::interval) weeks ), SELECT weeks.week, count(*) FROM weeks, test_results WHERE test_results.date_created > weeks.week AND test_results.date_created Throws an error for me... ERROR: syntax error at or near "SELECT" LINE 5: SELECT weeks.week…
i.e.,
... weeks
)
SELECT weeks.weekRe: Working with time in Postgres
#18date_trunc() is one way but to_char is even better as you can get the resulting output to something nicer. Doing: SELECT DATE_TRUNC('week', CURRENT_TIMESTAMP); gives: 2017-06-05 00:00:00+00 vs: SELECT TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-WW"wk"'); gives: 2017-23wk
Re: Working with time in Postgres
#19Earlier quoted context omitted.
True. I'm sure a use case exists where you want to store a timezone, but I don't know what it is. I try and use UTC everywhere, and only worry about time zones when displaying.
Any future event (such as a meeting) can't be stored as UTC, because time zone rules may change between now and the event date, but the event still needs to happen 10am local time.
Re: Working with time in Postgres
#20Weird, the example in the post (after changing table/field names for my database) with weeks as ( select week as week from generate_series('2017-01-01'::date, now()::date, '1 week'::interval) weeks ), SELECT weeks.week, count(*) FROM weeks, test_results WHERE test_results.date_created > weeks.week AND test_results.date_created Throws an error for me... ERROR: syntax error at or near "SELECT" LINE 5: SELECT weeks.week…
yeah, the comma after the "with" block shouldn't be there. i.e., ... weeks ) SELECT weeks.week
with weeks as (
select week as week
from generate_series('2017-01-01'::date, now()::date, '1 week'::interval) weeks
)
SELECT weeks.week,
count(*)
FROM weeks,
test_results
WHERE
test_results.date_created > weeks.week
AND
test_results.date_created
it throws... ERROR: column "week" does not exist
LINE 2: select week as week
^
I would move this to the post's own "replies" section, but it doesn't have one.