Introduction to date and time data types

Time Series Analysis in PostgreSQL

Jasmin Ludolf

Content Developer, DataCamp

Date and time data types

  • Date, Time, DateTime data types

In PostgreSQL:

  • DATE : date data only
  • TIME : time information without time zone
  • TIMESTAMP : combines date and time without time zone
  • TIMESTAMPTZ : TIMESTAMP with time zones
  • INTERVAL : time between two points

Illustration of a calendar and a clock face.

Time Series Analysis in PostgreSQL

Date and time values

  • DATE : YYYY-MM-DD (ISO 8601 international standard)
  • TIME : HH:MM:SS (seconds can be decimals, 12 or 24 hour clock)
  • TIMESTAMP : YYYY-MM-DD HH:MM:SS
  • TIMESTAMPTZ : YYYY-MM-DD HH:MM:SS+/-HH (+01:00 or CET)
  • INTERVAL : one input example is 1 02:30:04
    • Interval of 1 day, 2 hours, 30 minutes, 4 seconds
Time Series Analysis in PostgreSQL

Date and time in a table

CREATE TABLE timetable (
    date_info DATE,

time_info TIMESTAMP,
time_with_zone TIMESTAMPTZ,
interval_length INTERVAL);
INSERT INTO timetable(
    date_info,

time_info,
time_with_zone,
interval_length)
VALUES ( 'January 23 2013',
'2023-01-20 18:00:00',
'2023-01-20 18:00:00 EST',
'1 02:03:04');
Time Series Analysis in PostgreSQL

Date and time in a table

 

SELECT *
FROM timetable;

 

| date_info  | time_info           | time_with_zone            | interval_length |
| ---------- | ------------------- | ------------------------- | --------------- |
| 2023-01-20 | 2023-01-20 18:00:00 | 2023-01-20 00:00:00+01:00 | 1 day, 2:03:04  |
Time Series Analysis in PostgreSQL

Partial or incorrect data

INSERT INTO timetable(time_info)
VALUES ('2020-02-20 12:00:00');
SELECT *
FROM timetable;
| date_info  | time_info           | time_with_zone            | interval_length |
| ---------- | ------------------- | ------------------------- | --------------- |
| 2023-01-20 | 2023-01-20 18:00:00 | 2023-01-20 00:00:00+01:00 | 1 day, 2:03:04  |
| null.      | 2020-02-20 12:00:00 | null                      | null            |
Time Series Analysis in PostgreSQL

Partial or incorrect data

INSERT INTO timetable(time_info)
VALUES ('2020-02-20 02:00:00 EST');
SELECT *
FROM timetable;
| date_info  | time_info           | time_with_zone            | interval_length |
| ---------- | ------------------- | ------------------------- | --------------- |
| 2023-01-20 | 2023-01-20 18:00:00 | 2023-01-20 00:00:00+01:00 | 1 day, 2:03:04  |
| null.      | 2020-02-20 02:00:00 | null                      | null            |
Time Series Analysis in PostgreSQL

Unix time

  • Unix time : seconds since the Unix epoch
  • Unix epoch : January 1 1970 00:00:00, UTC
|unix_time |
|----------|
|1483444800|

That's January 3 2017!

Time Series Analysis in PostgreSQL

Let's practice!

Time Series Analysis in PostgreSQL

Preparing Video For Download...