Epoch conversion functions
Use these functions to convert between a date or time value and a count of seconds, milliseconds, microseconds, or nanoseconds since 1970-01-01 00:00:00 UTC, the Unix epoch. You can also get the length of an interval in these units, or the Julian day number of a date.
epoch function
Returns the number of seconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to temporal, as a DOUBLE whose fractional part holds the microseconds. A value before the epoch returns a negative number. A TIMESTAMP value without a time zone is read as UTC. For a TIME or TIME WITH TIME ZONE value, returns the number of seconds since midnight, and the offset of a TIME WITH TIME ZONE value is not applied. For an INTERVAL value, returns the length of the interval in seconds.
If temporal is NULL, the result is NULL. To get whole milliseconds, microseconds, or nanoseconds as a BIGINT, use epoch_ms, epoch_us, or epoch_ns. To convert a number of seconds back to a timestamp, use the to_timestamp function.
Syntax
epoch(temporal)
Arguments
The epoch function takes the following argument:
temporal-
The value to convert to seconds, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue. Nanosecond values such asTIMESTAMP_NSare accepted too, and the result keeps microsecond precision.
Return type
DOUBLE
Examples
The following SQL query returns the number of seconds since the epoch for a timestamp.
SELECT epoch( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes 1 AM on January 1, 1970, at UTC offset +01. In UTC, that time is midnight, which is the Unix epoch itself, so the result is 0.0.
SELECT epoch( TIMESTAMPTZ '1970-01-01 01:00:00+01' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the number of seconds since midnight for a time of day.
SELECT epoch(TIME '01:30:00') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the length of a one-month interval and a one-year interval in seconds. The month counts as 30 days, and the year counts as 365.25 days.
SELECT epoch(INTERVAL '1 month') AS one_month, epoch(INTERVAL '1 year') AS one_year
The query returns the following result.
| one_month | one_year |
|---|---|
|
|
Considerations
In an INTERVAL, each full 12 months count as 365.25 days, whether the interval is written in years or in months, and each remaining month counts as 30 days. For example, epoch(INTERVAL '12 months') returns 31557600.0, but epoch(INTERVAL '11 months') returns 28512000.0, which is 330 days.
For an infinite date or timestamp, such as TIMESTAMP 'infinity', the result is NULL.
Differences from Athena engine version 3
Athena engine version 3 uses to_unixtime to get the number of seconds since the epoch. DuckDB does not have to_unixtime, so use epoch.
epoch_ms function
Returns the number of milliseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to the date, time, timestamp, or interval value temporal, as a BIGINT. For a TIME or TIME WITH TIME ZONE value, the count starts at midnight, and the offset of a TIME WITH TIME ZONE value is not applied. For an INTERVAL value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from epoch, which counts a year as 365.25 days.
With a BIGINT argument, epoch_ms converts the other way: it reads the number as milliseconds since the epoch and returns the matching TIMESTAMP. If the argument is NULL, the result is NULL.
Syntax
epoch_ms(temporal)
epoch_ms(milliseconds)
Arguments
The epoch_ms function takes the following arguments:
temporal-
The value to convert to milliseconds, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue. milliseconds-
The number of milliseconds since the epoch to convert to a
TIMESTAMP, as aBIGINTvalue.
Return type
BIGINT for a date, time, timestamp, or interval argument, and TIMESTAMP for a BIGINT argument.
Examples
The following SQL query returns the number of milliseconds since the epoch for a timestamp.
SELECT epoch_ms( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts a number of milliseconds since the epoch to a TIMESTAMP.
SELECT epoch_ms(1627991984123) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a timestamp whose fraction of a second has four digits. The result is rounded to the nearest millisecond.
SELECT epoch_ms( TIMESTAMP '2021-08-03 11:59:44.1236' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
For a TIMESTAMP or TIMESTAMP WITH TIME ZONE value, the result is rounded to the nearest millisecond. For example, epoch_ms(TIMESTAMP '2021-08-03 11:59:44.1236') returns a number that ends in 124, not 123. For TIME and INTERVAL values, the extra digits are dropped instead.
epoch_ms does not work on TIMESTAMP_NS values, and the query fails. epoch_us and epoch_ns accept TIMESTAMP_NS values.
epoch_ns function
Returns the number of nanoseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to temporal, as a BIGINT. For a TIME or TIME WITH TIME ZONE value, the count starts at midnight, and the offset of a TIME WITH TIME ZONE value is not applied. For an INTERVAL value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from epoch, which counts a year as 365.25 days. If temporal is NULL, the result is NULL.
A TIMESTAMP value has microsecond precision, so the last three digits of its result are always 000. To keep nanoseconds, pass a TIMESTAMP_NS value. To convert a number of nanoseconds back to a timestamp, use the make_timestamp_ns function.
Syntax
epoch_ns(temporal)
Arguments
The epoch_ns function takes the following argument:
temporal-
The value to convert to nanoseconds, as a
DATE,TIME,TIME_NS,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP_NS,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the number of nanoseconds since the epoch for a timestamp with microsecond precision.
SELECT epoch_ns( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a TIMESTAMP_NS value, so the result keeps all nine digits of the fraction of a second.
SELECT epoch_ns( TIMESTAMP_NS '2021-08-03 11:59:44.123456789' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts a timestamp to nanoseconds and back with make_timestamp_ns, and checks that the value is unchanged.
SELECT make_timestamp_ns( epoch_ns( TIMESTAMP_NS '2021-08-03 11:59:44.123456789' ) ) = TIMESTAMP_NS '2021-08-03 11:59:44.123456789' AS is_round_trip
The query returns the following result.
| is_round_trip |
|---|
|
Considerations
The result must fit in a BIGINT, so epoch_ns works only for times from 1677-09-21 00:12:43.145225 through 2262-04-11 23:47:16.854775 UTC. For a date or timestamp outside that range, the query fails.
epoch_ns does not accept a number. To convert nanoseconds since the epoch to a timestamp, use the make_timestamp_ns function.
epoch_us function
Returns the number of microseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to temporal, as a BIGINT. For a TIME or TIME WITH TIME ZONE value, the count starts at midnight, and the offset of a TIME WITH TIME ZONE value is not applied. For an INTERVAL value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from epoch, which counts a year as 365.25 days. If temporal is NULL, the result is NULL.
For a TIMESTAMP_NS value, the nanosecond digits are dropped, not rounded. To convert a number of microseconds back to a TIMESTAMP, use the make_timestamp function with one argument.
Syntax
epoch_us(temporal)
Arguments
The epoch_us function takes the following argument:
temporal-
The value to convert to microseconds, as a
DATE,TIME,TIME_NS,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP_NS,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the number of microseconds since the epoch for a timestamp.
SELECT epoch_us( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a TIMESTAMP_NS value. The last three digits of the fraction of a second are dropped.
SELECT epoch_us( TIMESTAMP_NS '2021-08-03 11:59:44.123456789' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts a timestamp to microseconds and back with make_timestamp, and checks that the value is unchanged.
SELECT make_timestamp( epoch_us(TIMESTAMP '2021-08-03 11:59:44.123456') ) = TIMESTAMP '2021-08-03 11:59:44.123456' AS is_round_trip
The query returns the following result.
| is_round_trip |
|---|
|
Considerations
epoch_us does not accept a number. To convert microseconds since the epoch to a timestamp, use make_timestamp(.microseconds)
julian function
Returns the Julian day number of ts as a DOUBLE. The Julian day number counts days from November 24, 4714 BC in the proleptic Gregorian calendar, so DATE '1970-01-01' is day 2440588. For a DATE, the result is a whole number. For a timestamp, the fractional part is the part of the day that has passed since midnight, so 6 PM adds 0.75.
A TIMESTAMP WITH TIME ZONE value is read in the session time zone, which is UTC. If ts is NULL, the result is NULL.
Syntax
julian(ts)
Arguments
The julian function takes the following argument:
ts-
The value to convert to a Julian day number, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
DOUBLE
Examples
The following SQL query returns the Julian day number of noon on January 1, 2006.
SELECT julian(TIMESTAMP '2006-01-01 12:00') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares a date with a timestamp at 6 PM on the same day. The date gives a whole number, and the timestamp adds the fraction of the day.
SELECT julian(DATE '2006-01-01') AS from_date, julian( TIMESTAMP '2006-01-01 18:00:00' ) AS from_timestamp
The query returns the following result.
| from_date | from_timestamp |
|---|---|
|
|
Considerations
The fractional part counts from midnight, not from noon, so the result is 0.5 greater than the astronomical Julian date, which starts each day at noon UTC. For example, julian(TIMESTAMP '2000-01-01 12:00:00') returns 2451545.5, and the astronomical Julian date of that moment is 2451545.0. To get the astronomical Julian date, subtract 0.5.
make_timestamp_ms function
Returns the TIMESTAMP that is milliseconds milliseconds after 1970-01-01 00:00:00. A negative value gives a timestamp before 1970. If milliseconds is NULL, the result is NULL.
make_timestamp_ms( returns the same value as milliseconds)epoch_ms(. For a number of microseconds, use milliseconds)make_timestamp(, and for nanoseconds, use the make_timestamp_ns function.microseconds)
Syntax
make_timestamp_ms(milliseconds)
Arguments
The make_timestamp_ms function takes the following argument:
milliseconds-
The number of milliseconds since 1970-01-01 00:00:00, as a
BIGINTvalue.
Return type
TIMESTAMP
Examples
The following SQL query converts a number of milliseconds to a timestamp.
SELECT make_timestamp_ms(1732117793123) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a number of microseconds instead of milliseconds. The query succeeds, but the year of the result is far in the future.
SELECT year(make_timestamp_ms(1732117793000000)) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument is in milliseconds. If you pass a number of microseconds by mistake, the function does not fail: it returns a timestamp about 1,000 times farther from 1970, in a year such as 56858.
make_timestamp_ns function
Returns the TIMESTAMP_NS value that is nanoseconds nanoseconds after 1970-01-01 00:00:00. TIMESTAMP_NS keeps nanoseconds, while TIMESTAMP keeps only microseconds. If nanoseconds is NULL, the result is NULL.
For a number of microseconds, use make_timestamp(, and for milliseconds, use the make_timestamp_ms function.microseconds)
Syntax
make_timestamp_ns(nanoseconds)
Arguments
The make_timestamp_ns function takes the following argument:
nanoseconds-
The number of nanoseconds since 1970-01-01 00:00:00, as a
BIGINTvalue.
Return type
TIMESTAMP_NS
Examples
The following SQL query converts a number of nanoseconds to a timestamp.
SELECT make_timestamp_ns(1732117793000000000) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts a number of nanoseconds that is not a whole number of seconds, so the result has a fractional second.
SELECT make_timestamp_ns(1732117793123456789) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the result keeps all nine digits of the fractional second.
SELECT nanosecond( make_timestamp_ns(1732117793123456789) ) % 1000000000 AS result
The query returns the following result.
| result |
|---|
|
Considerations
Because the argument is a BIGINT, the result is between 1677-09-21 and 2262-04-11.
Casting the result to TIMESTAMP rounds it to microseconds.
to_timestamp function
Returns the point in time that is seconds seconds after the Unix epoch, 1970-01-01 00:00:00 UTC, as a TIMESTAMP WITH TIME ZONE value. A fractional part adds a fraction of a second, and a negative value gives a time before the epoch. If seconds is NULL, the result is NULL.
The result is shown in the session time zone, which is UTC, so it ends in +00.
Syntax
to_timestamp(seconds)
Arguments
The to_timestamp function takes the following argument:
seconds-
The number of seconds since the Unix epoch, as a
DOUBLEvalue.
Return type
TIMESTAMP WITH TIME ZONE
Examples
The following SQL query converts a number of seconds, with a fractional part, to a timestamp.
SELECT to_timestamp(1284352323.5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts the result to a TIMESTAMP without a time zone.
SELECT CAST( to_timestamp(1600000000) AS TIMESTAMP ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows what happens when you pass milliseconds instead of seconds. Dividing by 1000 gives the intended year.
SELECT year( to_timestamp(1700000000000) ) AS year_from_milliseconds, year( to_timestamp(1700000000000 / 1000) ) AS year_from_seconds
The query returns the following result.
| year_from_milliseconds | year_from_seconds |
|---|---|
|
|
Considerations
to_timestamp expects seconds. If you pass milliseconds, the result is a date tens of thousands of years in the future, and the query does not fail. Divide a millisecond value by 1000 first, or use the epoch_ms function, which takes milliseconds and returns a TIMESTAMP.
To get a TIMESTAMP without a time zone, cast the result, as in CAST(to_timestamp(. The cast uses the session time zone, so the result is the date and time in UTC.seconds) AS TIMESTAMP)
to_timestamp does not take a format argument. To convert text in a known layout to a timestamp, use the strptime function or the try_strptime function.
Differences from Athena engine version 3
In Athena engine version 3, to_timestamp parses a string with a format, as in to_timestamp('2024-01-02', 'yyyy-mm-dd'). In DuckDB, to_timestamp takes only a number of seconds, and a call with a format fails. To parse a string, use the strptime function with its own format specifiers, for example strptime('2024-01-02', '%Y-%m-%d').
DuckDB does not have from_unixtime. To replace from_unixtime( from Athena engine version 3, use seconds)CAST(to_timestamp(.seconds) AS TIMESTAMP)