--- title: "epoch" slug: "dev-database-reference-sql-functions-date-time-epoch" updated: 2026-10-30T18:19:01Z published: 2026-10-30T18:19:01Z canonical: "docs.vastdata.com/dev-database-reference-sql-functions-date-time-epoch" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.vastdata.com/llms.txt > Use this file to discover all available pages before exploring further. # epoch *Applies to VAST Cluster 5.5.* *[Functions](/develop/docs/dev-database-reference-sql-functions-index) › [Date and time functions](/develop/docs/dev-database-reference-sql-functions-date-time) › Epoch values.* Returns the number of seconds since 1970-01-01 00:00:00 of a date or timestamp, with the fraction, as a `DOUBLE`. On a time of day it returns the seconds since midnight, and on an interval its length in seconds. ## Syntax ``` epoch(value) ``` | Argument | Type | Description | | --- | --- | --- | | `value` | `DATE`, `TIMESTAMP` (also `TIMESTAMP_NS`, `TIMESTAMP_MS`, `TIMESTAMP_S`), `TIME`, `TIME WITH TIME ZONE` or `INTERVAL` | The value to convert to seconds. | **Returns:** `DOUBLE`. **Same as:** `date_part('epoch', value)` and `extract(epoch FROM value)`; see [date_part](/develop/docs/dev-database-reference-sql-functions-date-time-date-part). ## Set up the examples The examples connect with the ADBC driver, as in [VAST Query Engine (ADBC)](/develop/docs/dev-database-integrations-adbc), and work in a schema called `docs_sql_fn_date_time_epoch`. Set `QE_ENDPOINT`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `VASTDB_BUCKET` ([SQL reference](/develop/docs/dev-database-reference-sql-index#example-connect-and-use-a-transaction)), and run the blocks in order in one Python session. Below each block is the output you should see. This block connects, defines the `run()` and `plan()` helpers, and creates a table of five events. Each has a date (`d`), a timestamp (`ts`), a time of day (`t`) and a timestamp with nanoseconds (`tns`). Row 4 has no values, and row 5 is the last moment of 1969: ``` import os import re import adbc_driver_manager import adbc_driver_manager.dbapi as dbapi import adbc_driver_vastdb BUCKET = os.environ["VASTDB_BUCKET"] SCHEMA = f'"{BUCKET}/docs_sql_fn_date_time_epoch"' conn = dbapi.connect( driver=adbc_driver_vastdb.get_driver_path(), db_kwargs={ "vast.db.endpoint": os.environ["QE_ENDPOINT"], "vast.db.access_key": os.environ["AWS_ACCESS_KEY_ID"], "vast.db.secret_key": os.environ["AWS_SECRET_ACCESS_KEY"], }, autocommit=True, ) def server_message(e): """The server's message in a driver error, without the request ID or the echoed statement.""" text = re.sub(r"\[req=0x[0-9a-f]+\]: ", "", str(e)) found = (re.search(r'message: "(.*?)", request_id', text, re.S) or re.search(r"description: (.*)\)", text, re.S)) message = found.group(1) if found else text.strip().splitlines()[-1] message = re.sub(r"^Plan query: '.*?' failed with: ", "", message, flags=re.S) message = re.split(r"\\n|\n", message)[0].replace('\\"', '"') return message.replace(BUCKET, "") def run(sql): """Run one statement. Print the rows it returns, or the error.""" try: with conn.cursor() as cur: cur.execute(sql) rows = cur.fetchall() if cur.description else [] except (adbc_driver_manager.Error, OSError) as e: print(f"{type(e).__name__}: {server_message(e)}") else: print(rows) def plan(sql): """Print the plan without its ~N Rows lines: the table's row count, which can still be 0 just after an insert.""" with conn.cursor() as cur: cur.execute("EXPLAIN " + sql) steps = cur.fetchall()[0][1] print("\n".join(line for line in steps.splitlines() if "Rows" not in line)) TABLE = f"{SCHEMA}.events" run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}") run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, d DATE, ts TIMESTAMP, t TIME, tns TIMESTAMP_NS)") run(f"INSERT INTO {TABLE} VALUES " "(1, DATE '2026-02-28', TIMESTAMP '2026-02-28 23:15:00', TIME '23:15:00', " "CAST('2026-02-28 23:15:00.123456789' AS TIMESTAMP_NS)), " "(2, DATE '2026-03-01', TIMESTAMP '2026-03-01 08:30:45.123456', TIME '08:30:45.123456', " "CAST('2026-03-01 08:30:45.123456' AS TIMESTAMP_NS)), " "(3, DATE '2024-12-30', TIMESTAMP '2024-12-30 12:00:00', TIME '12:00:00', " "CAST('2024-12-30 12:00:00' AS TIMESTAMP_NS)), " "(4, NULL, NULL, NULL, NULL), " "(5, DATE '1969-12-31', TIMESTAMP '1969-12-31 23:59:59.999999', TIME '23:59:59.999999', " "CAST('1969-12-31 23:59:59.999999999' AS TIMESTAMP_NS))") ``` ``` [] [] [(5,)] ``` ## What it does `epoch` converts a value to seconds, as a `DOUBLE` with the fraction of a second: | Argument type | What it returns | Example | Result | | --- | --- | --- | --- | | `TIMESTAMP` | Seconds since 1970-01-01 00:00:00 | `epoch(TIMESTAMP '2024-02-29 13:45:30.123456')` | `1709214330.123456` | | `DATE` | Seconds since 1970-01-01 at the date's midnight | `epoch(DATE '2024-02-29')` | `1709164800.0` | | `TIME` | Seconds since midnight | `epoch(TIME '13:45:30.123456')` | `49530.123456` | | `INTERVAL` | The interval's length in seconds | `epoch(INTERVAL '1 day')` | `86400.0` | Values before 1970 are negative: one microsecond before 1970 (row 5) is `-1e-06`. On an `INTERVAL`, `epoch` counts a month as 30 days and a year as 365.25 days: `epoch(INTERVAL '1 month')` is `2592000.0`, and `epoch(INTERVAL '1 year')` is `31557600.0`. [epoch_ms](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ms), [epoch_us](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-us) and [epoch_ns](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ns) count a year as 12 months of 30 days instead. On a `TIME WITH TIME ZONE`, the result is the seconds since midnight of the local time; the offset isn't applied: `epoch(TIMETZ '13:45:30.123456+02:30')` is `49530.123456`. ## NULLs and edge cases | Input | Result | | --- | --- | | A NULL value (row 4) | `NULL` | | `DATE 'infinity'`, `TIMESTAMP '-infinity'` | `NULL` | | `DATE '0001-01-01'` | `-62135596800.0` | | `TIMESTAMP '1969-12-31 23:59:59.5'` | `-0.5` | | `TIME '24:00:00'` | `86400.0` | | `INTERVAL '1 year 2 months 3 days 04:05:06.789'` | `37015506.789` | | `INTERVAL '-1 year -2 months -3 days -04:05:06.789'` | `-37015506.789` | | `TIMESTAMP_NS '2024-02-29 13:45:30.123456789'` | `1709214330.123456`: the nanoseconds are dropped | | An untyped `NULL`: `epoch(NULL)` | Error: `Could not choose a best candidate function for the function call "epoch("NULL")"` | | A string | Error: `Could not choose a best candidate function`; cast it to `TIMESTAMP` first | | A number, boolean, `BLOB` or list | Error: `No function matches the given name and argument types`, for example `'epoch(INTEGER_LITERAL)'` | The same cases, as the cluster returns them. They use the table and helpers from [Set up the examples](/develop/docs/dev-database-reference-sql-functions-date-time-epoch#set-up-the-examples): ``` run(f"SELECT id, epoch(ts) FROM {TABLE} WHERE id = 4") run("SELECT epoch(DATE 'infinity'), epoch(TIMESTAMP '-infinity'), epoch(DATE '0001-01-01'), " "epoch(TIMESTAMP '1969-12-31 23:59:59.5'), epoch(TIME '24:00:00')") run("SELECT epoch(INTERVAL '1 year 2 months 3 days 04:05:06.789'), " "epoch(INTERVAL '-1 year -2 months -3 days -04:05:06.789'), epoch(TIMESTAMP_NS '2024-02-29 13:45:30.123456789')") run("SELECT epoch(NULL)") run("SELECT epoch('2024-02-29 13:45:30')") run("SELECT epoch(123)") ``` ``` [(4, None)] [(None, None, -62135596800.0, -0.5, 86400.0)] [(37015506.789, -37015506.789, 1709214330.123456)] InternalError: Binder Error: Could not choose a best candidate function for the function call "epoch("NULL")". In order to select one, please add explicit type casts. InternalError: Binder Error: Could not choose a best candidate function for the function call "epoch(STRING_LITERAL)". In order to select one, please add explicit type casts. InternalError: Binder Error: No function matches the given name and argument types 'epoch(INTEGER_LITERAL)'. You might need to add explicit type casts. ``` ## Examples The seconds since 1970 of each event's date and timestamp, and the seconds since midnight of its time: ``` run(f"SELECT id, epoch(d), epoch(ts), epoch(t) FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, 1772236800.0, 1772320500.0, 83700.0), (2, 1772323200.0, 1772353845.123456, 30645.123456), (3, 1735516800.0, 1735560000.0, 43200.0), (4, None, None, None), (5, -86400.0, -1e-06, 86399.999999)] ``` The length of an interval in seconds, and the seconds between two timestamps: ``` run("SELECT epoch(INTERVAL '1 month'), epoch(INTERVAL '1 year'), " "epoch(TIMESTAMP '2026-03-01 09:01:00') - epoch(TIMESTAMP '2026-03-01 08:59:00')") ``` ``` [(2592000.0, 31557600.0, 120.0)] ``` Keep only the events after a Unix time in seconds. This works, but [Make it faster](/develop/docs/dev-database-reference-sql-functions-date-time-epoch#make-it-faster) has a faster way to write it: ``` run(f"SELECT id FROM {TABLE} WHERE epoch(ts) > 1772300000 ORDER BY id LIMIT 10") ``` ``` [(1,), (2,)] ``` ## How it runs in VAST The Query Engine runs on the cluster's CNodes, and a query on a table starts with a scan in VAST DataBase (`VAST_SCAN`). VAST DataBase can evaluate a comparison of a column with a constant, but not a function of a column, so `epoch()` is always computed by the Query Engine: - **In the `SELECT` list,** the scan reads the column and the Query Engine computes `epoch()` for each row it returns. - **In `WHERE`,** the scan sends the column for every row, and a `FILTER` step in the Query Engine computes `epoch()` and keeps the rows that match. No rows are skipped in storage. The plan of the `WHERE` example shows the `FILTER` step above the scan: ``` plan(f"SELECT id FROM {TABLE} WHERE epoch(ts) > 1772300000") ``` ``` ┌───────────────────────────┐ │ PROJECTION │ │ ──────────────────── │ │ #1 │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ──────────────────── │ │ (epoch(ts) > 1772300000.0)│ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ │ │ Projections: │ │ ts │ │ id │ │ │ └───────────────────────────┘ ``` ## Make it faster - **Convert the number to a timestamp, not the column to a number.** `ts > make_timestamp(1772300000000000)` (microseconds) and `ts > CAST(to_timestamp(1772300000) AS TIMESTAMP)` (seconds) return the same rows as `epoch(ts) > 1772300000`. The function is applied to a constant, so the comparison of the raw column is pushed down to VAST DataBase, and the plans have no `FILTER` step: ``` plan(f"SELECT id FROM {TABLE} WHERE ts > make_timestamp(1772300000000000)") plan(f"SELECT id FROM {TABLE} WHERE ts > CAST(to_timestamp(1772300000) AS TIMESTAMP)") run(f"SELECT id FROM {TABLE} WHERE ts > make_timestamp(1772300000000000) ORDER BY id LIMIT 10") ``` ``` ┌───────────────────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ Projections: id │ │ │ └───────────────────────────┘ ┌───────────────────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ Projections: id │ │ │ └───────────────────────────┘ [(1,), (2,)] ``` - **Use `epoch()` in the `SELECT` list,** where it doesn't stop a filter from being pushed down. ## Limitations and errors - **No time-zone support.** On a `TIMESTAMP WITH TIME ZONE` value, such as `now()`, `epoch` fails with an `Extension Autoloading Error`. Cast the value to `TIMESTAMP` first, or use [epoch_ms](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ms), which takes a `TIMESTAMP WITH TIME ZONE`. See [time zones](/develop/docs/dev-database-reference-sql-functions-date-time-time-zones). - **Microseconds at most.** On a `TIMESTAMP_NS`, the result has microsecond precision: row 5's `1969-12-31 23:59:59.999999999` gives `0.0`, while `epoch_ns` gives `-1`. Use [epoch_ns](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ns) for nanoseconds. - **No reverse form.** `epoch(1772353845)` fails with `No function matches the given name and argument types 'epoch(INTEGER_LITERAL)'`. To turn seconds into a timestamp, use [to_timestamp](/develop/docs/dev-database-reference-sql-functions-date-time-to-timestamp). `epoch_s` and `epoch_seconds` don't exist. - **One argument.** `epoch(ts, ts)` fails with `No function matches the given name and argument types 'epoch(TIMESTAMP, TIMESTAMP)'`. ``` run(f"SELECT id, epoch(tns), epoch_ns(tns) FROM {TABLE} ORDER BY id LIMIT 10") run("SELECT epoch(1772353845)") run("SELECT CAST(to_timestamp(1772353845) AS TIMESTAMP)") run(f"SELECT epoch(ts, ts) FROM {TABLE} LIMIT 1") ``` ``` [(1, 1772320500.123456, 1772320500123456789), (2, 1772353845.123456, 1772353845123456000), (3, 1735560000.0, 1735560000000000000), (4, None, None), (5, 0.0, -1)] InternalError: Binder Error: No function matches the given name and argument types 'epoch(INTEGER_LITERAL)'. You might need to add explicit type casts. [(datetime.datetime(2026, 3, 1, 8, 30, 45),)] InternalError: Binder Error: No function matches the given name and argument types 'epoch(TIMESTAMP, TIMESTAMP)'. You might need to add explicit type casts. ``` ## Similar functions | Function | Returns | Result for `TIMESTAMP '2024-02-29 13:45:30.123456'` (`epoch` gives `1709214330.123456`) | Result for `INTERVAL '1 year 1 month 1 day'` (`epoch` gives `34236000.0`) | | --- | --- | --- | --- | | [date_part](/develop/docs/dev-database-reference-sql-functions-date-time-date-part) with `'epoch'` | The same value | `1709214330.123456` | `34236000.0` | | [epoch_ms](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ms) | Whole milliseconds, as a `BIGINT` | `1709214330123` | `33782400000` | | [epoch_us](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-us) | Whole microseconds, as a `BIGINT` | `1709214330123456` | `33782400000000` | | [epoch_ns](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ns) | Whole nanoseconds, as a `BIGINT` | `1709214330123456000` | (not compared) | | [julian](/develop/docs/dev-database-reference-sql-functions-date-time-julian) | Days since 4714 BC, as a `DOUBLE` | `2460370.573265318` | (dates and timestamps only) | ``` run("SELECT epoch(TIMESTAMP '2024-02-29 13:45:30.123456'), date_part('epoch', TIMESTAMP '2024-02-29 13:45:30.123456'), " "epoch_ms(TIMESTAMP '2024-02-29 13:45:30.123456'), epoch_us(TIMESTAMP '2024-02-29 13:45:30.123456'), " "epoch_ns(TIMESTAMP '2024-02-29 13:45:30.123456'), julian(TIMESTAMP '2024-02-29 13:45:30.123456')") run("SELECT epoch(INTERVAL '1 year 1 month 1 day'), date_part('epoch', INTERVAL '1 year 1 month 1 day'), " "epoch_ms(INTERVAL '1 year 1 month 1 day'), epoch_us(INTERVAL '1 year 1 month 1 day')") ``` ``` [(1709214330.123456, 1709214330.123456, 1709214330123, 1709214330123456, 1709214330123456000, 2460370.573265318)] [(34236000.0, 34236000.0, 33782400000, 33782400000000)] ``` Clean up: ``` run(f"DROP TABLE IF EXISTS {TABLE}") run(f"DROP SCHEMA IF EXISTS {SCHEMA}") conn.close() ``` ``` [] [] ``` ## See also - [epoch_ms](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ms), [epoch_us](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-us) and [epoch_ns](/develop/docs/dev-database-reference-sql-functions-date-time-epoch-ns): whole milliseconds, microseconds and nanoseconds. - [to_timestamp](/develop/docs/dev-database-reference-sql-functions-date-time-to-timestamp) and [make_timestamp](/develop/docs/dev-database-reference-sql-functions-date-time-make-timestamp): from a number back to a timestamp. - [Date and time functions](/develop/docs/dev-database-reference-sql-functions-date-time): all the date and time functions. - [Where filters run](/develop/docs/dev-database-reference-sql-select#where-filters-run) and [pushdowns](/develop/docs/dev-database-reference-pushdowns). - [EXPLAIN](/develop/docs/dev-database-reference-sql-explain): how to read a plan.