-- jtimestamp and jtimestamptz: every cast, on edge values and infinities, under -- several time zones. Errors are shown as SQLSTATE only (\set VERBOSITY -- sqlstate), never as message text. -- -- The tables created here are dropped at the end: extension_schema runs -- DROP EXTENSION, which needs no column of our types left behind. SET DateStyle = 'ISO, YMD'; SET timezone = 'UTC'; -- --------------------------------------------------------------------- -- the catalog: which casts exist, in which context, by which method -- --------------------------------------------------------------------- -- every cast that has one of the new types at either end (jdate and the core -- types at the other). All ASSIGNMENT, apart from the two typmod (scale) -- casts, which are IMPLICIT; the two casts to the core types of the same kind -- are binary (b), the rest go through a function (f). SELECT c.castsource::regtype AS source, c.casttarget::regtype AS target, c.castcontext AS context, c.castmethod AS method, p.proname AS function, p.provolatile AS volatility, p.proisstrict AS strict, p.proparallel AS parallel FROM pg_cast c LEFT JOIN pg_proc p ON p.oid = c.castfunc WHERE c.castsource IN ('jtimestamp'::regtype, 'jtimestamptz'::regtype) OR c.casttarget IN ('jtimestamp'::regtype, 'jtimestamptz'::regtype) ORDER BY c.castsource::regtype::text COLLATE "C", c.casttarget::regtype::text COLLATE "C"; source | target | context | method | function | volatility | strict | parallel -----------------------------+-----------------------------+---------+--------+--------------+------------+--------+---------- date | jtimestamp | a | f | jtimestamp | i | t | s date | jtimestamptz | a | f | jtimestamptz | s | t | s jdate | jtimestamp | a | f | jtimestamp | i | t | s jdate | jtimestamptz | a | f | jtimestamptz | s | t | s jtimestamp | jdate | a | f | jdate | i | t | s jtimestamp | jtimestamp | i | f | jtimestamp | i | t | s jtimestamp | jtimestamptz | a | f | jtimestamptz | s | t | s jtimestamp | timestamp without time zone | a | b | | | | jtimestamptz | jdate | a | f | jdate | s | t | s jtimestamptz | jtimestamp | a | f | jtimestamp | s | t | s jtimestamptz | jtimestamptz | i | f | jtimestamptz | i | t | s jtimestamptz | timestamp with time zone | a | b | | | | timestamp with time zone | jtimestamptz | a | f | jtimestamptz | i | t | s timestamp without time zone | jtimestamp | a | f | jtimestamp | i | t | s (14 rows) -- the summary the table above shows: no implicit cast between different types SELECT count(*) AS casts, count(*) FILTER (WHERE c.castcontext = 'i' AND c.castsource <> c.casttarget) AS implicit_between_types, count(*) FILTER (WHERE c.castcontext = 'i' AND c.castsource = c.casttarget) AS implicit_scale, count(*) FILTER (WHERE c.castmethod = 'b') AS binary_casts FROM pg_cast c WHERE c.castsource IN ('jtimestamp'::regtype, 'jtimestamptz'::regtype) OR c.casttarget IN ('jtimestamp'::regtype, 'jtimestamptz'::regtype); casts | implicit_between_types | implicit_scale | binary_casts -------+------------------------+----------------+-------------- 14 | 0 | 2 | 2 (1 row) -- the scale functions take (value, typmod, explicit), as the parser expects SELECT p.proname, pg_get_function_identity_arguments(p.oid) AS args FROM pg_cast c JOIN pg_proc p ON p.oid = c.castfunc WHERE c.castsource = c.casttarget AND c.castsource IN ('jtimestamp'::regtype, 'jtimestamptz'::regtype) ORDER BY p.proname; proname | args --------------+-------------------------------- jtimestamp | jtimestamp, integer, boolean jtimestamptz | jtimestamptz, integer, boolean (2 rows) -- --------------------------------------------------------------------- -- volatility of every cast function, against core's -- --------------------------------------------------------------------- -- A cast that reads the session time zone is STABLE, every other one is -- IMMUTABLE. The oracle is core: each of our types is mapped to the core type -- it mirrors (jdate to date, jtimestamp to timestamp, jtimestamptz to -- timestamptz) and the core cast between the mapped types gives the volatility -- expected. Where the mapping leaves one type (date -> jdate, timestamp -> -- jtimestamp, timestamptz -> jtimestamptz) there is no core cast: the function -- is a range check and reads no zone, so IMMUTABLE is expected (this includes -- timestamptz -> jtimestamptz, unlike core's STABLE timestamp -> timestamptz). -- The scale casts map to core's scale casts, which are IMMUTABLE. WITH m(ours, core) AS ( VALUES ('jdate'::regtype, 'date'::regtype), ('jtimestamp'::regtype, 'timestamp'::regtype), ('jtimestamptz'::regtype, 'timestamptz'::regtype) ) SELECT c.castsource::regtype AS source, c.casttarget::regtype AS target, p.proname AS function, p.provolatile AS volatility, coalesce(cp.provolatile, 'i') AS expected, p.provolatile = coalesce(cp.provolatile, 'i') AS as_expected FROM pg_cast c JOIN pg_proc p ON p.oid = c.castfunc LEFT JOIN m ms ON ms.ours = c.castsource LEFT JOIN m mt ON mt.ours = c.casttarget LEFT JOIN pg_cast cc ON cc.castsource = coalesce(ms.core, c.castsource) AND cc.casttarget = coalesce(mt.core, c.casttarget) LEFT JOIN pg_proc cp ON cp.oid = cc.castfunc WHERE ms.ours IS NOT NULL OR mt.ours IS NOT NULL ORDER BY c.castsource::regtype::text COLLATE "C", c.casttarget::regtype::text COLLATE "C"; source | target | function | volatility | expected | as_expected -----------------------------+-----------------------------+--------------+------------+----------+------------- date | jdate | jdate | i | i | t date | jtimestamp | jtimestamp | i | i | t date | jtimestamptz | jtimestamptz | s | s | t jdate | jtimestamp | jtimestamp | i | i | t jdate | jtimestamptz | jtimestamptz | s | s | t jdate | timestamp with time zone | timestamptz | s | s | t jdate | timestamp without time zone | timestamp | i | i | t jtimestamp | jdate | jdate | i | i | t jtimestamp | jtimestamp | jtimestamp | i | i | t jtimestamp | jtimestamptz | jtimestamptz | s | s | t jtimestamptz | jdate | jdate | s | s | t jtimestamptz | jtimestamp | jtimestamp | s | s | t jtimestamptz | jtimestamptz | jtimestamptz | i | i | t timestamp with time zone | jdate | jdate | s | s | t timestamp with time zone | jtimestamptz | jtimestamptz | i | i | t timestamp without time zone | jdate | jdate | i | i | t timestamp without time zone | jtimestamp | jtimestamp | i | i | t (17 rows) -- the cast functions of the three types together: how many, and none that -- differs from core's volatility WITH m(ours, core) AS ( VALUES ('jdate'::regtype, 'date'::regtype), ('jtimestamp'::regtype, 'timestamp'::regtype), ('jtimestamptz'::regtype, 'timestamptz'::regtype) ) SELECT count(*) AS cast_functions, count(*) FILTER (WHERE p.provolatile = 's') AS stable, count(*) FILTER (WHERE p.provolatile = 'i') AS immutable, count(*) FILTER (WHERE p.provolatile = 'v') AS volatile, count(*) FILTER (WHERE p.provolatile <> coalesce(cp.provolatile, 'i')) AS differing_from_core FROM pg_cast c JOIN pg_proc p ON p.oid = c.castfunc LEFT JOIN m ms ON ms.ours = c.castsource LEFT JOIN m mt ON mt.ours = c.casttarget LEFT JOIN pg_cast cc ON cc.castsource = coalesce(ms.core, c.castsource) AND cc.casttarget = coalesce(mt.core, c.casttarget) LEFT JOIN pg_proc cp ON cp.oid = cc.castfunc WHERE ms.ours IS NOT NULL OR mt.ours IS NOT NULL; cast_functions | stable | immutable | volatile | differing_from_core ----------------+--------+-----------+----------+--------------------- 17 | 7 | 10 | 0 | 0 (1 row) -- the functions next to the casts that convert but are not casts: the epoch -- conversions read no zone (to_jdate with a zone name takes the zone as an -- argument, as core's timezone(text, timestamptz)), jdate_format on a -- timestamptz reads the session zone SELECT p.proname AS function, pg_get_function_identity_arguments(p.oid) AS args, p.provolatile AS volatility FROM pg_proc p WHERE p.proname IN ('to_jdate', 'to_jtimestamptz', 'jdate_format') AND p.oid IN (SELECT d.objid FROM pg_depend d WHERE d.classid = 'pg_proc'::regclass AND d.deptype = 'e' AND d.refobjid = (SELECT oid FROM pg_extension WHERE extname = 'jdate')) ORDER BY p.proname, pg_get_function_identity_arguments(p.oid) COLLATE "C"; function | args | volatility -----------------+---------------------------------------------+------------ jdate_format | timestamp with time zone, with_time boolean | s to_jdate | double precision | i to_jdate | double precision, text | i to_jtimestamptz | double precision | i (4 rows) -- --------------------------------------------------------------------- -- Nowruz 1403 (1403-01-01 is 2024-03-20) in five zones -- -- Every row is evaluated in its zone, with a fresh parse per statement (a -- literal is read in the session zone when the statement is parsed). %1$s in an -- expression is the UTC instant of local midnight of 2024-03-20 in that zone, -- in the "expect" it is the offset suffix of the zone's text and %2$s the same -- instant as a UTC clock time. -- --------------------------------------------------------------------- CREATE FUNCTION pg_temp.eval(zone text, q text) RETURNS text LANGUAGE plpgsql AS $f$ DECLARE r text; BEGIN PERFORM set_config('TimeZone', zone, false); EXECUTE q INTO r; RETURN r; EXCEPTION WHEN OTHERS THEN RETURN 'ERR ' || SQLSTATE; END $f$; CREATE TEMP TABLE nz_zone (id int, zone text, off text, utc text); INSERT INTO nz_zone VALUES (1, 'UTC', '+00', '2024-03-20 00:00:00'), (2, 'Asia/Tehran', '+03:30', '2024-03-19 20:30:00'), (3, 'America/New_York', '-04', '2024-03-20 04:00:00'), (4, 'Pacific/Kiritimati', '+14', '2024-03-19 10:00:00'), (5, 'Pacific/Pago_Pago', '-11', '2024-03-20 11:00:00'); -- expr is run with %1$s = the UTC clock time of local midnight, expect with -- %1$s = the zone's offset suffix CREATE TEMP TABLE nz_check (n int, what text, expr text, expect text); INSERT INTO nz_check VALUES (1, 'jtimestamp -> timestamp', $$SELECT ('1403-01-01 00:00:00'::jtimestamp::timestamp)::text$$, '2024-03-20 00:00:00'), (2, 'timestamp -> jtimestamp', $$SELECT ('2024-03-20 00:00:00'::timestamp::jtimestamp)::text$$, '1403-01-01 00:00:00'), (3, 'jtimestamp -> jtimestamptz (local midnight)', $$SELECT ('1403-01-01 00:00:00'::jtimestamp::jtimestamptz)::text$$, '1403-01-01 00:00:00%1$s'), (4, ' the same instant in UTC', $$SELECT (('1403-01-01 00:00:00'::jtimestamp::jtimestamptz)::timestamptz AT TIME ZONE 'UTC')::text$$, '%2$s'), (5, 'jtimestamptz -> jtimestamp', $$SELECT (('1403-01-01 00:00:00'::jtimestamp::jtimestamptz)::jtimestamp)::text$$, '1403-01-01 00:00:00'), (6, 'timestamptz -> jtimestamptz', $$SELECT ('%1$s+00'::timestamptz::jtimestamptz)::text$$, '1403-01-01 00:00:00%1$s'), (7, 'jtimestamptz -> timestamptz (binary)', $$SELECT (('1403-01-01 00:00:00'::jtimestamp::jtimestamptz)::timestamptz = '%1$s+00'::timestamptz)::text$$, 'true'), (8, 'date -> jtimestamp', $$SELECT (date '2024-03-20'::jtimestamp)::text$$, '1403-01-01 00:00:00'), (9, 'date -> jtimestamptz', $$SELECT (date '2024-03-20'::jtimestamptz)::text$$, '1403-01-01 00:00:00%1$s'), (10, 'jdate -> jtimestamp', $$SELECT ('1403-01-01'::jdate::jtimestamp)::text$$, '1403-01-01 00:00:00'), (11, 'jdate -> jtimestamptz', $$SELECT ('1403-01-01'::jdate::jtimestamptz)::text$$, '1403-01-01 00:00:00%1$s'), (12, ' the same instant in UTC', $$SELECT (('1403-01-01'::jdate::jtimestamptz)::timestamptz AT TIME ZONE 'UTC')::text$$, '%2$s'), (13, 'jtimestamp -> jdate', $$SELECT ('1403-01-01 23:59:59.999999'::jtimestamp::jdate)::text$$, '1403-01-01'), (14, 'jtimestamptz -> jdate at local midnight', $$SELECT ('%1$s+00'::timestamptz::jtimestamptz::jdate)::text$$, '1403-01-01'), (15, 'jtimestamptz -> jdate one microsecond before it', $$SELECT (('%1$s+00'::timestamptz - interval '1 microsecond')::jtimestamptz::jdate)::text$$, '1402-12-29'), (16, 'jtimestamptz -> jdate at the last microsecond of the day', $$SELECT (('%1$s+00'::timestamptz + interval '1 day' - interval '1 microsecond')::jtimestamptz::jdate)::text$$, '1403-01-01'), (17, 'function form jtimestamptz(jtimestamp)', $$SELECT jtimestamptz('1403-01-01 00:00:00'::jtimestamp)::text$$, '1403-01-01 00:00:00%1$s'), (18, 'function form jtimestamp(jtimestamptz)', $$SELECT jtimestamp('%1$s+00'::timestamptz::jtimestamptz)::text$$, '1403-01-01 00:00:00'), -- 1403 has 366 days; every one of them must round-trip in every zone (19, 'sweep 1403: jdate -> jtimestamp -> jdate, mismatches', $$SELECT count(*) FILTER (WHERE d::jtimestamp::jdate <> d)::text FROM (SELECT jdate '1403-01-01' + g AS d FROM generate_series(0, 365) g) s$$, '0'), (20, 'sweep 1403: jdate -> jtimestamptz -> jdate, mismatches', $$SELECT count(*) FILTER (WHERE d::jtimestamptz::jdate <> d)::text FROM (SELECT jdate '1403-01-01' + g AS d FROM generate_series(0, 365) g) s$$, '0'), (21, 'sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches', $$SELECT count(*) FILTER (WHERE d::jtimestamp::timestamp <> d::date::timestamp)::text FROM (SELECT jdate '1403-01-01' + g AS d FROM generate_series(0, 365) g) s$$, '0'), (22, 'sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches', $$SELECT count(*) FILTER (WHERE d::jtimestamptz::timestamptz <> d::date::timestamptz)::text FROM (SELECT jdate '1403-01-01' + g AS d FROM generate_series(0, 365) g) s$$, '0'), (23, 'sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches', $$SELECT count(*) FILTER (WHERE jtimestamp_send(t::jtimestamptz::jtimestamp) <> jtimestamp_send(t))::text FROM (SELECT ((date '2024-03-20' + g) + time '12:34:56.789')::jtimestamp AS t FROM generate_series(0, 365) g) s$$, '0'); -- the ok column is the comparison with what the zone has to give SELECT z.zone, c.n, c.what, r.got, r.got = format(c.expect, z.off, z.utc) AS ok FROM nz_zone z CROSS JOIN nz_check c, LATERAL (SELECT pg_temp.eval(z.zone, format(c.expr, z.utc)) AS got) r ORDER BY z.id, c.n; zone | n | what | got | ok --------------------+----+----------------------------------------------------------------------------------------------+---------------------------+---- UTC | 1 | jtimestamp -> timestamp | 2024-03-20 00:00:00 | t UTC | 2 | timestamp -> jtimestamp | 1403-01-01 00:00:00 | t UTC | 3 | jtimestamp -> jtimestamptz (local midnight) | 1403-01-01 00:00:00+00 | t UTC | 4 | the same instant in UTC | 2024-03-20 00:00:00 | t UTC | 5 | jtimestamptz -> jtimestamp | 1403-01-01 00:00:00 | t UTC | 6 | timestamptz -> jtimestamptz | 1403-01-01 00:00:00+00 | t UTC | 7 | jtimestamptz -> timestamptz (binary) | true | t UTC | 8 | date -> jtimestamp | 1403-01-01 00:00:00 | t UTC | 9 | date -> jtimestamptz | 1403-01-01 00:00:00+00 | t UTC | 10 | jdate -> jtimestamp | 1403-01-01 00:00:00 | t UTC | 11 | jdate -> jtimestamptz | 1403-01-01 00:00:00+00 | t UTC | 12 | the same instant in UTC | 2024-03-20 00:00:00 | t UTC | 13 | jtimestamp -> jdate | 1403-01-01 | t UTC | 14 | jtimestamptz -> jdate at local midnight | 1403-01-01 | t UTC | 15 | jtimestamptz -> jdate one microsecond before it | 1402-12-29 | t UTC | 16 | jtimestamptz -> jdate at the last microsecond of the day | 1403-01-01 | t UTC | 17 | function form jtimestamptz(jtimestamp) | 1403-01-01 00:00:00+00 | t UTC | 18 | function form jtimestamp(jtimestamptz) | 1403-01-01 00:00:00 | t UTC | 19 | sweep 1403: jdate -> jtimestamp -> jdate, mismatches | 0 | t UTC | 20 | sweep 1403: jdate -> jtimestamptz -> jdate, mismatches | 0 | t UTC | 21 | sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches | 0 | t UTC | 22 | sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches | 0 | t UTC | 23 | sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches | 0 | t Asia/Tehran | 1 | jtimestamp -> timestamp | 2024-03-20 00:00:00 | t Asia/Tehran | 2 | timestamp -> jtimestamp | 1403-01-01 00:00:00 | t Asia/Tehran | 3 | jtimestamp -> jtimestamptz (local midnight) | 1403-01-01 00:00:00+03:30 | t Asia/Tehran | 4 | the same instant in UTC | 2024-03-19 20:30:00 | t Asia/Tehran | 5 | jtimestamptz -> jtimestamp | 1403-01-01 00:00:00 | t Asia/Tehran | 6 | timestamptz -> jtimestamptz | 1403-01-01 00:00:00+03:30 | t Asia/Tehran | 7 | jtimestamptz -> timestamptz (binary) | true | t Asia/Tehran | 8 | date -> jtimestamp | 1403-01-01 00:00:00 | t Asia/Tehran | 9 | date -> jtimestamptz | 1403-01-01 00:00:00+03:30 | t Asia/Tehran | 10 | jdate -> jtimestamp | 1403-01-01 00:00:00 | t Asia/Tehran | 11 | jdate -> jtimestamptz | 1403-01-01 00:00:00+03:30 | t Asia/Tehran | 12 | the same instant in UTC | 2024-03-19 20:30:00 | t Asia/Tehran | 13 | jtimestamp -> jdate | 1403-01-01 | t Asia/Tehran | 14 | jtimestamptz -> jdate at local midnight | 1403-01-01 | t Asia/Tehran | 15 | jtimestamptz -> jdate one microsecond before it | 1402-12-29 | t Asia/Tehran | 16 | jtimestamptz -> jdate at the last microsecond of the day | 1403-01-01 | t Asia/Tehran | 17 | function form jtimestamptz(jtimestamp) | 1403-01-01 00:00:00+03:30 | t Asia/Tehran | 18 | function form jtimestamp(jtimestamptz) | 1403-01-01 00:00:00 | t Asia/Tehran | 19 | sweep 1403: jdate -> jtimestamp -> jdate, mismatches | 0 | t Asia/Tehran | 20 | sweep 1403: jdate -> jtimestamptz -> jdate, mismatches | 0 | t Asia/Tehran | 21 | sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches | 0 | t Asia/Tehran | 22 | sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches | 0 | t Asia/Tehran | 23 | sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches | 0 | t America/New_York | 1 | jtimestamp -> timestamp | 2024-03-20 00:00:00 | t America/New_York | 2 | timestamp -> jtimestamp | 1403-01-01 00:00:00 | t America/New_York | 3 | jtimestamp -> jtimestamptz (local midnight) | 1403-01-01 00:00:00-04 | t America/New_York | 4 | the same instant in UTC | 2024-03-20 04:00:00 | t America/New_York | 5 | jtimestamptz -> jtimestamp | 1403-01-01 00:00:00 | t America/New_York | 6 | timestamptz -> jtimestamptz | 1403-01-01 00:00:00-04 | t America/New_York | 7 | jtimestamptz -> timestamptz (binary) | true | t America/New_York | 8 | date -> jtimestamp | 1403-01-01 00:00:00 | t America/New_York | 9 | date -> jtimestamptz | 1403-01-01 00:00:00-04 | t America/New_York | 10 | jdate -> jtimestamp | 1403-01-01 00:00:00 | t America/New_York | 11 | jdate -> jtimestamptz | 1403-01-01 00:00:00-04 | t America/New_York | 12 | the same instant in UTC | 2024-03-20 04:00:00 | t America/New_York | 13 | jtimestamp -> jdate | 1403-01-01 | t America/New_York | 14 | jtimestamptz -> jdate at local midnight | 1403-01-01 | t America/New_York | 15 | jtimestamptz -> jdate one microsecond before it | 1402-12-29 | t America/New_York | 16 | jtimestamptz -> jdate at the last microsecond of the day | 1403-01-01 | t America/New_York | 17 | function form jtimestamptz(jtimestamp) | 1403-01-01 00:00:00-04 | t America/New_York | 18 | function form jtimestamp(jtimestamptz) | 1403-01-01 00:00:00 | t America/New_York | 19 | sweep 1403: jdate -> jtimestamp -> jdate, mismatches | 0 | t America/New_York | 20 | sweep 1403: jdate -> jtimestamptz -> jdate, mismatches | 0 | t America/New_York | 21 | sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches | 0 | t America/New_York | 22 | sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches | 0 | t America/New_York | 23 | sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches | 0 | t Pacific/Kiritimati | 1 | jtimestamp -> timestamp | 2024-03-20 00:00:00 | t Pacific/Kiritimati | 2 | timestamp -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Kiritimati | 3 | jtimestamp -> jtimestamptz (local midnight) | 1403-01-01 00:00:00+14 | t Pacific/Kiritimati | 4 | the same instant in UTC | 2024-03-19 10:00:00 | t Pacific/Kiritimati | 5 | jtimestamptz -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Kiritimati | 6 | timestamptz -> jtimestamptz | 1403-01-01 00:00:00+14 | t Pacific/Kiritimati | 7 | jtimestamptz -> timestamptz (binary) | true | t Pacific/Kiritimati | 8 | date -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Kiritimati | 9 | date -> jtimestamptz | 1403-01-01 00:00:00+14 | t Pacific/Kiritimati | 10 | jdate -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Kiritimati | 11 | jdate -> jtimestamptz | 1403-01-01 00:00:00+14 | t Pacific/Kiritimati | 12 | the same instant in UTC | 2024-03-19 10:00:00 | t Pacific/Kiritimati | 13 | jtimestamp -> jdate | 1403-01-01 | t Pacific/Kiritimati | 14 | jtimestamptz -> jdate at local midnight | 1403-01-01 | t Pacific/Kiritimati | 15 | jtimestamptz -> jdate one microsecond before it | 1402-12-29 | t Pacific/Kiritimati | 16 | jtimestamptz -> jdate at the last microsecond of the day | 1403-01-01 | t Pacific/Kiritimati | 17 | function form jtimestamptz(jtimestamp) | 1403-01-01 00:00:00+14 | t Pacific/Kiritimati | 18 | function form jtimestamp(jtimestamptz) | 1403-01-01 00:00:00 | t Pacific/Kiritimati | 19 | sweep 1403: jdate -> jtimestamp -> jdate, mismatches | 0 | t Pacific/Kiritimati | 20 | sweep 1403: jdate -> jtimestamptz -> jdate, mismatches | 0 | t Pacific/Kiritimati | 21 | sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches | 0 | t Pacific/Kiritimati | 22 | sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches | 0 | t Pacific/Kiritimati | 23 | sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches | 0 | t Pacific/Pago_Pago | 1 | jtimestamp -> timestamp | 2024-03-20 00:00:00 | t Pacific/Pago_Pago | 2 | timestamp -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Pago_Pago | 3 | jtimestamp -> jtimestamptz (local midnight) | 1403-01-01 00:00:00-11 | t Pacific/Pago_Pago | 4 | the same instant in UTC | 2024-03-20 11:00:00 | t Pacific/Pago_Pago | 5 | jtimestamptz -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Pago_Pago | 6 | timestamptz -> jtimestamptz | 1403-01-01 00:00:00-11 | t Pacific/Pago_Pago | 7 | jtimestamptz -> timestamptz (binary) | true | t Pacific/Pago_Pago | 8 | date -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Pago_Pago | 9 | date -> jtimestamptz | 1403-01-01 00:00:00-11 | t Pacific/Pago_Pago | 10 | jdate -> jtimestamp | 1403-01-01 00:00:00 | t Pacific/Pago_Pago | 11 | jdate -> jtimestamptz | 1403-01-01 00:00:00-11 | t Pacific/Pago_Pago | 12 | the same instant in UTC | 2024-03-20 11:00:00 | t Pacific/Pago_Pago | 13 | jtimestamp -> jdate | 1403-01-01 | t Pacific/Pago_Pago | 14 | jtimestamptz -> jdate at local midnight | 1403-01-01 | t Pacific/Pago_Pago | 15 | jtimestamptz -> jdate one microsecond before it | 1402-12-29 | t Pacific/Pago_Pago | 16 | jtimestamptz -> jdate at the last microsecond of the day | 1403-01-01 | t Pacific/Pago_Pago | 17 | function form jtimestamptz(jtimestamp) | 1403-01-01 00:00:00-11 | t Pacific/Pago_Pago | 18 | function form jtimestamp(jtimestamptz) | 1403-01-01 00:00:00 | t Pacific/Pago_Pago | 19 | sweep 1403: jdate -> jtimestamp -> jdate, mismatches | 0 | t Pacific/Pago_Pago | 20 | sweep 1403: jdate -> jtimestamptz -> jdate, mismatches | 0 | t Pacific/Pago_Pago | 21 | sweep 1403: jdate -> jtimestamp -> timestamp vs jdate -> date -> timestamp, mismatches | 0 | t Pacific/Pago_Pago | 22 | sweep 1403: jdate -> jtimestamptz -> timestamptz vs jdate -> date -> timestamptz, mismatches | 0 | t Pacific/Pago_Pago | 23 | sweep 1403: jtimestamp -> jtimestamptz -> jtimestamp at 12:34:56.789, mismatches | 0 | t (115 rows) SET timezone = 'UTC'; -- --------------------------------------------------------------------- -- edges of the range, without a zone: jtimestamp, and the core types -- --------------------------------------------------------------------- -- the Jalali range is -0061-01-01 .. 3177-12-29, Gregorian 0560-03-20 .. -- 3799-03-19 SELECT '-0061-01-01 00:00:00'::jtimestamp::timestamp AS first_start, '3177-12-29 23:59:59.999999'::jtimestamp::timestamp AS last_microsecond; first_start | last_microsecond ---------------------+---------------------------- 0560-03-20 00:00:00 | 3799-03-19 23:59:59.999999 (1 row) SELECT timestamp '0560-03-20 00:00:00'::jtimestamp AS first_valid, timestamp '3799-03-19 23:59:59.999999'::jtimestamp AS last_valid; first_valid | last_valid ----------------------+---------------------------- -0061-01-01 00:00:00 | 3177-12-29 23:59:59.999999 (1 row) SELECT date '0560-03-20'::jtimestamp AS first_date, date '3799-03-19'::jtimestamp AS last_date; first_date | last_date ----------------------+--------------------- -0061-01-01 00:00:00 | 3177-12-29 00:00:00 (1 row) SELECT '-0061-01-01'::jdate::jtimestamp AS first_jdate, '3177-12-29'::jdate::jtimestamp AS last_jdate; first_jdate | last_jdate ----------------------+--------------------- -0061-01-01 00:00:00 | 3177-12-29 00:00:00 (1 row) SELECT '-0061-01-01 00:00:00'::jtimestamp::jdate AS first_back, '3177-12-29 23:59:59.999999'::jtimestamp::jdate AS last_back; first_back | last_back -------------+------------ -0061-01-01 | 3177-12-29 (1 row) \set VERBOSITY sqlstate SELECT timestamp '0560-03-19 23:59:59.999999'::jtimestamp; -- one microsecond below ERROR: 22008 SELECT timestamp '3799-03-20 00:00:00'::jtimestamp; -- the first instant above ERROR: 22008 SELECT timestamp '0001-01-01 00:00:00'::jtimestamp; ERROR: 22008 SELECT timestamp '4714-11-24 00:00:00 BC'::jtimestamp; -- core's own minimum ERROR: 22008 SELECT timestamp '294276-12-31 23:59:59.999999'::jtimestamp; -- core's own maximum ERROR: 22008 SELECT date '0560-03-19'::jtimestamp; ERROR: 22008 SELECT date '3799-03-20'::jtimestamp; ERROR: 22008 SELECT date '4714-11-24 BC'::jtimestamp; ERROR: 22008 SELECT date '5874897-12-31'::jtimestamp; -- beyond timestamp's range: core's own error ERROR: 22008 \set VERBOSITY default -- --------------------------------------------------------------------- -- edges of the range for jtimestamptz: the safe range, in fixed-offset zones -- -- The valid instants are 0560-03-20 16:00:00+00 (the first day's start plus -- 16 hours) up to, not including, 3799-03-19 08:00:00+00 (the last day's end -- minus 16 hours). Fixed POSIX offsets are used, so no tzdata history matters: -- <+14>-14 is Pacific/Kiritimati's offset, <-11>+11 Pacific/Pago_Pago's. -- --------------------------------------------------------------------- CREATE TEMP TABLE edge (n int, zone text, what text, q text, expect text); INSERT INTO edge (n, zone, what, q, expect) VALUES -- UTC: the first day starts 16 hours before the first valid instant (1, 'UTC', 'jdate first day', $$SELECT ('-0061-01-01'::jdate::jtimestamptz)::text$$, 'ERR 22008'), (2, 'UTC', 'jdate second day', $$SELECT ('-0061-01-02'::jdate::jtimestamptz)::text$$, '-0061-01-02 00:00:00+00'), (3, 'UTC', 'jdate last but one day', $$SELECT ('3177-12-28'::jdate::jtimestamptz)::text$$, '3177-12-28 00:00:00+00'), (4, 'UTC', 'jdate last day', $$SELECT ('3177-12-29'::jdate::jtimestamptz)::text$$, '3177-12-29 00:00:00+00'), (5, 'UTC', 'date first day', $$SELECT (date '0560-03-20'::jtimestamptz)::text$$, 'ERR 22008'), (6, 'UTC', 'date second day', $$SELECT (date '0560-03-21'::jtimestamptz)::text$$, '-0061-01-02 00:00:00+00'), (7, 'UTC', 'date last day', $$SELECT (date '3799-03-19'::jtimestamptz)::text$$, '3177-12-29 00:00:00+00'), (8, 'UTC', 'date after the last', $$SELECT (date '3799-03-20'::jtimestamptz)::text$$, 'ERR 22008'), (9, 'UTC', 'jtimestamp first start', $$SELECT ('-0061-01-01 00:00:00'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (10, 'UTC', 'jtimestamp 15:59:59.999999 on the first day', $$SELECT ('-0061-01-01 15:59:59.999999'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (11, 'UTC', 'jtimestamp 16:00:00 on the first day', $$SELECT ('-0061-01-01 16:00:00'::jtimestamp::jtimestamptz)::text$$, '-0061-01-01 16:00:00+00'), (12, 'UTC', 'jtimestamp 07:59:59.999999 on the last day', $$SELECT ('3177-12-29 07:59:59.999999'::jtimestamp::jtimestamptz)::text$$, '3177-12-29 07:59:59.999999+00'), (13, 'UTC', 'jtimestamp 08:00:00 on the last day', $$SELECT ('3177-12-29 08:00:00'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (14, 'UTC', 'jtimestamp last microsecond', $$SELECT ('3177-12-29 23:59:59.999999'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (15, 'UTC', 'timestamptz first valid', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz)::text$$, '-0061-01-01 16:00:00+00'), (16, 'UTC', 'timestamptz one microsecond below', $$SELECT ('0560-03-20 15:59:59.999999+00'::timestamptz::jtimestamptz)::text$$, 'ERR 22008'), (17, 'UTC', 'timestamptz last valid', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz)::text$$, '3177-12-29 07:59:59.999999+00'), (18, 'UTC', 'timestamptz first above', $$SELECT ('3799-03-19 08:00:00+00'::timestamptz::jtimestamptz)::text$$, 'ERR 22008'), (19, 'UTC', 'timestamptz core minimum', $$SELECT ('4714-11-24 00:00:00+00 BC'::timestamptz::jtimestamptz)::text$$, 'ERR 22008'), -- jtimestamptz back to jtimestamp and jdate at the valid edges: never an error (20, 'UTC', 'first valid -> jtimestamp', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '-0061-01-01 16:00:00'), (21, 'UTC', 'last valid -> jtimestamp', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '3177-12-29 07:59:59.999999'), (22, 'UTC', 'first valid -> jdate', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jdate)::text$$, '-0061-01-01'), (23, 'UTC', 'last valid -> jdate', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jdate)::text$$, '3177-12-29'), -- <+14>-14: local midnight is 14 hours before the UTC midnight (31, '<+14>-14', 'jdate first day', $$SELECT ('-0061-01-01'::jdate::jtimestamptz)::text$$, 'ERR 22008'), (32, '<+14>-14', 'jdate second day', $$SELECT ('-0061-01-02'::jdate::jtimestamptz)::text$$, 'ERR 22008'), (33, '<+14>-14', 'jdate third day', $$SELECT ('-0061-01-03'::jdate::jtimestamptz)::text$$, '-0061-01-03 00:00:00+14'), (34, '<+14>-14', 'jdate last day', $$SELECT ('3177-12-29'::jdate::jtimestamptz)::text$$, '3177-12-29 00:00:00+14'), (35, '<+14>-14', 'jtimestamp 05:59:59.999999 on the second day', $$SELECT ('-0061-01-02 05:59:59.999999'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (36, '<+14>-14', 'jtimestamp 06:00:00 on the second day', $$SELECT ('-0061-01-02 06:00:00'::jtimestamp::jtimestamptz)::text$$, '-0061-01-02 06:00:00+14'), (37, '<+14>-14', 'jtimestamp 21:59:59.999999 on the last day', $$SELECT ('3177-12-29 21:59:59.999999'::jtimestamp::jtimestamptz)::text$$, '3177-12-29 21:59:59.999999+14'), (38, '<+14>-14', 'jtimestamp 22:00:00 on the last day', $$SELECT ('3177-12-29 22:00:00'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (39, '<+14>-14', 'first valid -> jtimestamp', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '-0061-01-02 06:00:00'), (40, '<+14>-14', 'last valid -> jdate', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jdate)::text$$, '3177-12-29'), -- <-11>+11: local midnight is 11 hours after the UTC midnight (51, '<-11>+11', 'jdate first day', $$SELECT ('-0061-01-01'::jdate::jtimestamptz)::text$$, 'ERR 22008'), (52, '<-11>+11', 'jdate second day', $$SELECT ('-0061-01-02'::jdate::jtimestamptz)::text$$, '-0061-01-02 00:00:00-11'), (53, '<-11>+11', 'jdate last but one day', $$SELECT ('3177-12-28'::jdate::jtimestamptz)::text$$, '3177-12-28 00:00:00-11'), (54, '<-11>+11', 'jdate last day', $$SELECT ('3177-12-29'::jdate::jtimestamptz)::text$$, 'ERR 22008'), (55, '<-11>+11', 'date last day', $$SELECT (date '3799-03-19'::jtimestamptz)::text$$, 'ERR 22008'), (56, '<-11>+11', 'jtimestamp 04:59:59.999999 on the first day', $$SELECT ('-0061-01-01 04:59:59.999999'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (57, '<-11>+11', 'jtimestamp 05:00:00 on the first day', $$SELECT ('-0061-01-01 05:00:00'::jtimestamp::jtimestamptz)::text$$, '-0061-01-01 05:00:00-11'), (58, '<-11>+11', 'jtimestamp 20:59:59.999999 on the last but one day', $$SELECT ('3177-12-28 20:59:59.999999'::jtimestamp::jtimestamptz)::text$$, '3177-12-28 20:59:59.999999-11'), (59, '<-11>+11', 'jtimestamp 21:00:00 on the last but one day', $$SELECT ('3177-12-28 21:00:00'::jtimestamp::jtimestamptz)::text$$, 'ERR 22008'), (60, '<-11>+11', 'first valid -> jtimestamp', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '-0061-01-01 05:00:00'), -- the extreme offsets PostgreSQL accepts, +/-15:59: a valid instant is -- always on a Jalali day, so jtimestamp and jdate never fail (71, '<+15:59>-15:59', 'first valid -> jtimestamp', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '-0061-01-02 07:59:00'), (72, '<+15:59>-15:59', 'first valid -> jdate', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jdate)::text$$, '-0061-01-02'), (73, '<+15:59>-15:59', 'last valid -> jtimestamp', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '3177-12-29 23:58:59.999999'), (74, '<+15:59>-15:59', 'last valid -> jdate', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jdate)::text$$, '3177-12-29'), (75, '<-15:59>+15:59', 'first valid -> jtimestamp', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '-0061-01-01 00:01:00'), (76, '<-15:59>+15:59', 'first valid -> jdate', $$SELECT ('0560-03-20 16:00:00+00'::timestamptz::jtimestamptz::jdate)::text$$, '-0061-01-01'), (77, '<-15:59>+15:59', 'last valid -> jtimestamp', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jtimestamp)::text$$, '3177-12-28 16:00:59.999999'), (78, '<-15:59>+15:59', 'last valid -> jdate', $$SELECT ('3799-03-19 07:59:59.999999+00'::timestamptz::jtimestamptz::jdate)::text$$, '3177-12-28'); SELECT e.zone, e.what, r.got, r.got = e.expect AS ok FROM edge e, LATERAL (SELECT pg_temp.eval(e.zone, e.q) AS got) r ORDER BY e.n; zone | what | got | ok ----------------+----------------------------------------------------+-------------------------------+---- UTC | jdate first day | ERR 22008 | t UTC | jdate second day | -0061-01-02 00:00:00+00 | t UTC | jdate last but one day | 3177-12-28 00:00:00+00 | t UTC | jdate last day | 3177-12-29 00:00:00+00 | t UTC | date first day | ERR 22008 | t UTC | date second day | -0061-01-02 00:00:00+00 | t UTC | date last day | 3177-12-29 00:00:00+00 | t UTC | date after the last | ERR 22008 | t UTC | jtimestamp first start | ERR 22008 | t UTC | jtimestamp 15:59:59.999999 on the first day | ERR 22008 | t UTC | jtimestamp 16:00:00 on the first day | -0061-01-01 16:00:00+00 | t UTC | jtimestamp 07:59:59.999999 on the last day | 3177-12-29 07:59:59.999999+00 | t UTC | jtimestamp 08:00:00 on the last day | ERR 22008 | t UTC | jtimestamp last microsecond | ERR 22008 | t UTC | timestamptz first valid | -0061-01-01 16:00:00+00 | t UTC | timestamptz one microsecond below | ERR 22008 | t UTC | timestamptz last valid | 3177-12-29 07:59:59.999999+00 | t UTC | timestamptz first above | ERR 22008 | t UTC | timestamptz core minimum | ERR 22008 | t UTC | first valid -> jtimestamp | -0061-01-01 16:00:00 | t UTC | last valid -> jtimestamp | 3177-12-29 07:59:59.999999 | t UTC | first valid -> jdate | -0061-01-01 | t UTC | last valid -> jdate | 3177-12-29 | t <+14>-14 | jdate first day | ERR 22008 | t <+14>-14 | jdate second day | ERR 22008 | t <+14>-14 | jdate third day | -0061-01-03 00:00:00+14 | t <+14>-14 | jdate last day | 3177-12-29 00:00:00+14 | t <+14>-14 | jtimestamp 05:59:59.999999 on the second day | ERR 22008 | t <+14>-14 | jtimestamp 06:00:00 on the second day | -0061-01-02 06:00:00+14 | t <+14>-14 | jtimestamp 21:59:59.999999 on the last day | 3177-12-29 21:59:59.999999+14 | t <+14>-14 | jtimestamp 22:00:00 on the last day | ERR 22008 | t <+14>-14 | first valid -> jtimestamp | -0061-01-02 06:00:00 | t <+14>-14 | last valid -> jdate | 3177-12-29 | t <-11>+11 | jdate first day | ERR 22008 | t <-11>+11 | jdate second day | -0061-01-02 00:00:00-11 | t <-11>+11 | jdate last but one day | 3177-12-28 00:00:00-11 | t <-11>+11 | jdate last day | ERR 22008 | t <-11>+11 | date last day | ERR 22008 | t <-11>+11 | jtimestamp 04:59:59.999999 on the first day | ERR 22008 | t <-11>+11 | jtimestamp 05:00:00 on the first day | -0061-01-01 05:00:00-11 | t <-11>+11 | jtimestamp 20:59:59.999999 on the last but one day | 3177-12-28 20:59:59.999999-11 | t <-11>+11 | jtimestamp 21:00:00 on the last but one day | ERR 22008 | t <-11>+11 | first valid -> jtimestamp | -0061-01-01 05:00:00 | t <+15:59>-15:59 | first valid -> jtimestamp | -0061-01-02 07:59:00 | t <+15:59>-15:59 | first valid -> jdate | -0061-01-02 | t <+15:59>-15:59 | last valid -> jtimestamp | 3177-12-29 23:58:59.999999 | t <+15:59>-15:59 | last valid -> jdate | 3177-12-29 | t <-15:59>+15:59 | first valid -> jtimestamp | -0061-01-01 00:01:00 | t <-15:59>+15:59 | first valid -> jdate | -0061-01-01 | t <-15:59>+15:59 | last valid -> jtimestamp | 3177-12-28 16:00:59.999999 | t <-15:59>+15:59 | last valid -> jdate | 3177-12-28 | t (51 rows) SET timezone = 'UTC'; -- --------------------------------------------------------------------- -- infinities through every cast -- --------------------------------------------------------------------- SELECT label, pos, neg FROM (VALUES (1, 'timestamp -> jtimestamp', ('infinity'::timestamp::jtimestamp)::text, ('-infinity'::timestamp::jtimestamp)::text), (2, 'jtimestamp -> timestamp', ('infinity'::jtimestamp::timestamp)::text, ('-infinity'::jtimestamp::timestamp)::text), (3, 'timestamptz -> jtimestamptz', ('infinity'::timestamptz::jtimestamptz)::text, ('-infinity'::timestamptz::jtimestamptz)::text), (4, 'jtimestamptz -> timestamptz', ('infinity'::jtimestamptz::timestamptz)::text, ('-infinity'::jtimestamptz::timestamptz)::text), (5, 'jtimestamp -> jtimestamptz', ('infinity'::jtimestamp::jtimestamptz)::text, ('-infinity'::jtimestamp::jtimestamptz)::text), (6, 'jtimestamptz -> jtimestamp', ('infinity'::jtimestamptz::jtimestamp)::text, ('-infinity'::jtimestamptz::jtimestamp)::text), (7, 'date -> jtimestamp', ('infinity'::date::jtimestamp)::text, ('-infinity'::date::jtimestamp)::text), (8, 'date -> jtimestamptz', ('infinity'::date::jtimestamptz)::text, ('-infinity'::date::jtimestamptz)::text), (9, 'jdate -> jtimestamp', ('infinity'::jdate::jtimestamp)::text, ('-infinity'::jdate::jtimestamp)::text), (10, 'jdate -> jtimestamptz', ('infinity'::jdate::jtimestamptz)::text, ('-infinity'::jdate::jtimestamptz)::text), (11, 'jtimestamp -> jdate', ('infinity'::jtimestamp::jdate)::text, ('-infinity'::jtimestamp::jdate)::text), (12, 'jtimestamptz -> jdate', ('infinity'::jtimestamptz::jdate)::text, ('-infinity'::jtimestamptz::jdate)::text), (13, 'jtimestamp(0)', ('infinity'::jtimestamp(0))::text, ('-infinity'::jtimestamp(0))::text), (14, 'jtimestamptz(0)', ('infinity'::jtimestamptz(0))::text, ('-infinity'::jtimestamptz(0))::text) ) v(n, label, pos, neg) ORDER BY n; label | pos | neg -----------------------------+----------+----------- timestamp -> jtimestamp | infinity | -infinity jtimestamp -> timestamp | infinity | -infinity timestamptz -> jtimestamptz | infinity | -infinity jtimestamptz -> timestamptz | infinity | -infinity jtimestamp -> jtimestamptz | infinity | -infinity jtimestamptz -> jtimestamp | infinity | -infinity date -> jtimestamp | infinity | -infinity date -> jtimestamptz | infinity | -infinity jdate -> jtimestamp | infinity | -infinity jdate -> jtimestamptz | infinity | -infinity jtimestamp -> jdate | infinity | -infinity jtimestamptz -> jdate | infinity | -infinity jtimestamp(0) | infinity | -infinity jtimestamptz(0) | infinity | -infinity (14 rows) -- the same in a zone with an offset: the infinities do not move SET timezone = 'Pacific/Kiritimati'; SELECT ('infinity'::date::jtimestamptz)::text AS date_inf, ('-infinity'::jtimestamptz::jdate)::text AS neg_to_jdate, ('infinity'::jtimestamptz::jtimestamp)::text AS tz_to_ts; date_inf | neg_to_jdate | tz_to_ts ----------+--------------+---------- infinity | -infinity | infinity (1 row) SET timezone = 'UTC'; -- --------------------------------------------------------------------- -- typmod (scale) casts: rounding, and the range checked after rounding -- --------------------------------------------------------------------- SELECT '1403-01-01 12:34:56.789'::jtimestamp(0) AS p0, '1403-01-01 12:34:56.789'::jtimestamp(1) AS p1, '1403-01-01 12:34:56.789'::jtimestamp(2) AS p2, '1403-01-01 12:34:56.789'::jtimestamp(3) AS p3, '1403-01-01 12:34:56.789012'::jtimestamp(6) AS p6; p0 | p1 | p2 | p3 | p6 ---------------------+-----------------------+------------------------+-------------------------+---------------------------- 1403-01-01 12:34:57 | 1403-01-01 12:34:56.8 | 1403-01-01 12:34:56.79 | 1403-01-01 12:34:56.789 | 1403-01-01 12:34:56.789012 (1 row) SELECT '1403-01-01 12:34:56.789+00'::jtimestamptz(0) AS p0, '1403-01-01 12:34:56.789+00'::jtimestamptz(1) AS p1, '1403-01-01 12:34:56.789+00'::jtimestamptz(2) AS p2, '1403-01-01 12:34:56.789+00'::jtimestamptz(3) AS p3, '1403-01-01 12:34:56.789012+00'::jtimestamptz(6) AS p6; p0 | p1 | p2 | p3 | p6 ------------------------+--------------------------+---------------------------+----------------------------+------------------------------- 1403-01-01 12:34:57+00 | 1403-01-01 12:34:56.8+00 | 1403-01-01 12:34:56.79+00 | 1403-01-01 12:34:56.789+00 | 1403-01-01 12:34:56.789012+00 (1 row) -- rounding carries into the next minute, day and year SELECT '1403-12-30 23:59:59.5'::jtimestamp(0) AS next_year, '1403-05-15 23:59:59.9996'::jtimestamp(3) AS next_day, '1403-05-15 12:59:59.5+00'::jtimestamptz(0) AS next_hour; next_year | next_day | next_hour ---------------------+---------------------+------------------------ 1404-01-01 00:00:00 | 1403-05-16 00:00:00 | 1403-05-15 13:00:00+00 (1 row) -- the explicit cast of a value that is not a literal, and the function form SELECT j::jtimestamp(0) AS from_column, j::jtimestamp(1) AS one_digit FROM (SELECT '1403-01-01 12:34:56.789'::jtimestamp AS j) s; from_column | one_digit ---------------------+----------------------- 1403-01-01 12:34:57 | 1403-01-01 12:34:56.8 (1 row) SELECT jtimestamp('1403-01-01 12:34:56.789'::jtimestamp, 0, true) AS function_form, jtimestamptz('1403-01-01 12:34:56.789+00'::jtimestamptz, 0, false) AS function_form_tz; function_form | function_form_tz ---------------------+------------------------ 1403-01-01 12:34:57 | 1403-01-01 12:34:57+00 (1 row) -- first valid value: rounding goes towards the day start, never below it SELECT '-0061-01-01 00:00:00.4'::jtimestamp(0) AS first_down, '-0061-01-01 00:00:00.6'::jtimestamp(0) AS first_up, '-0061-01-01 00:00:00.5'::jtimestamp(0) AS first_half; first_down | first_up | first_half ----------------------+----------------------+---------------------- -0061-01-01 00:00:00 | -0061-01-01 00:00:01 | -0061-01-01 00:00:00 (1 row) -- the last valid value survives its own precision, and nothing coarser SELECT '3177-12-29 23:59:59.999999'::jtimestamp(6) AS last_p6, '3177-12-29 23:59:59.999499'::jtimestamp(3) AS last_rounds_down, '3177-12-29 07:59:59.999999+00'::jtimestamptz(6) AS tz_last_p6, '3177-12-29 07:59:59.4+00'::jtimestamptz(0) AS tz_last_down; last_p6 | last_rounds_down | tz_last_p6 | tz_last_down ----------------------------+-------------------------+-------------------------------+------------------------ 3177-12-29 23:59:59.999999 | 3177-12-29 23:59:59.999 | 3177-12-29 07:59:59.999999+00 | 3177-12-29 07:59:59+00 (1 row) \set VERBOSITY sqlstate SELECT '3177-12-29 23:59:59.999999'::jtimestamp(0); -- 3178-01-01 00:00:00 ERROR: 22008 SELECT '3177-12-29 23:59:59.999999'::jtimestamp(1); ERROR: 22008 SELECT '3177-12-29 23:59:59.999999'::jtimestamp(3); ERROR: 22008 SELECT '3177-12-29 23:59:59.999999'::jtimestamp(5); ERROR: 22008 SELECT '3177-12-29 23:59:59.9995'::jtimestamp(3); ERROR: 22008 SELECT '3177-12-29 07:59:59.999999+00'::jtimestamptz(0); -- 08:00:00, the first instant above the safe range ERROR: 22008 SELECT '3177-12-29 07:59:59.999999+00'::jtimestamptz(3); ERROR: 22008 SELECT '3177-12-29 07:59:59.5+00'::jtimestamptz(0); ERROR: 22008 SELECT jtimestamp('3177-12-29 23:59:59.999999'::jtimestamp, 0, true); ERROR: 22008 SELECT jtimestamptz('3177-12-29 07:59:59.999999+00'::jtimestamptz, 0, false); ERROR: 22008 SELECT j::jtimestamp(0) FROM (SELECT '3177-12-29 23:59:59.999999'::jtimestamp AS j) s; ERROR: 22008 SELECT j::jtimestamptz(0) FROM (SELECT '3177-12-29 07:59:59.999999+00'::jtimestamptz AS j) s; ERROR: 22008 \set VERBOSITY default -- --------------------------------------------------------------------- -- INSERT and UPDATE: the assignment casts, and the column's typmod -- --------------------------------------------------------------------- CREATE TABLE jt_c_assign ( id int GENERATED ALWAYS AS IDENTITY, j0 jtimestamp(0), j3 jtimestamp(3), z0 jtimestamptz(0), z3 jtimestamptz(3), j jtimestamp, z jtimestamptz, d jdate ); -- literals take the column's typmod INSERT INTO jt_c_assign (j0, j3, z0, z3) VALUES ('1403-01-01 12:34:56.789', '1403-01-01 12:34:56.7896', '1403-01-01 12:34:56.789+00', '1403-01-01 12:34:56.7896+00'); -- core types in: timestamp -> jtimestamp(p), timestamptz -> jtimestamptz(p) INSERT INTO jt_c_assign (j0, j3, z0, z3) SELECT timestamp '2024-03-20 12:34:56.789', timestamp '2024-03-20 12:34:56.7896', timestamptz '2024-03-20 12:34:56.789+00', timestamptz '2024-03-20 12:34:56.7896+00'; -- jdate, date and the other timestamp type in INSERT INTO jt_c_assign (j, z) SELECT jdate '1403-01-01', jdate '1403-01-01'; INSERT INTO jt_c_assign (j, z) SELECT date '2024-03-20', date '2024-03-20'; INSERT INTO jt_c_assign (z, j) SELECT jtimestamp '1403-01-01 12:00:00', jtimestamptz '1403-01-01 12:00:00+00'; -- and out: jtimestamp / jtimestamptz -> jdate, in the session zone INSERT INTO jt_c_assign (d) SELECT jtimestamp '1403-01-01 23:59:59'; INSERT INTO jt_c_assign (d) SELECT jtimestamptz '1403-01-01 23:59:59+00'; SELECT id, j0, j3, z0, z3, j, z, d FROM jt_c_assign ORDER BY id; id | j0 | j3 | z0 | z3 | j | z | d ----+---------------------+------------------------+------------------------+---------------------------+---------------------+------------------------+------------ 1 | 1403-01-01 12:34:57 | 1403-01-01 12:34:56.79 | 1403-01-01 12:34:57+00 | 1403-01-01 12:34:56.79+00 | | | 2 | 1403-01-01 12:34:57 | 1403-01-01 12:34:56.79 | 1403-01-01 12:34:57+00 | 1403-01-01 12:34:56.79+00 | | | 3 | | | | | 1403-01-01 00:00:00 | 1403-01-01 00:00:00+00 | 4 | | | | | 1403-01-01 00:00:00 | 1403-01-01 00:00:00+00 | 5 | | | | | 1403-01-01 12:00:00 | 1403-01-01 12:00:00+00 | 6 | | | | | | | 1403-01-01 7 | | | | | | | 1403-01-01 (7 rows) -- UPDATE: the typmod of the target applies to a value of the same type UPDATE jt_c_assign SET j0 = j3 WHERE id = 1; UPDATE jt_c_assign SET z0 = z3 WHERE id = 1; SELECT id, j0, j3, z0, z3 FROM jt_c_assign WHERE id = 1; id | j0 | j3 | z0 | z3 ----+---------------------+------------------------+------------------------+--------------------------- 1 | 1403-01-01 12:34:57 | 1403-01-01 12:34:56.79 | 1403-01-01 12:34:57+00 | 1403-01-01 12:34:56.79+00 (1 row) -- a jtimestamp(3) column read back into a jtimestamp(0): the explicit cast rounds SELECT j3::jtimestamp(0) AS rounded FROM jt_c_assign WHERE id = 1; rounded --------------------- 1403-01-01 12:34:57 (1 row) \set VERBOSITY sqlstate -- the edge rounds out of the range: the INSERT fails whole INSERT INTO jt_c_assign (j0) VALUES ('3177-12-29 23:59:59.999999'); ERROR: 22008 INSERT INTO jt_c_assign (j3) VALUES ('3177-12-29 23:59:59.9996'); ERROR: 22008 INSERT INTO jt_c_assign (z0) VALUES ('3177-12-29 07:59:59.999999+00'); ERROR: 22008 INSERT INTO jt_c_assign (j0) SELECT timestamp '3799-03-19 23:59:59.999999'; ERROR: 22008 INSERT INTO jt_c_assign (z0) SELECT timestamptz '3799-03-19 07:59:59.999999+00'; ERROR: 22008 -- a core value outside the Jalali range INSERT INTO jt_c_assign (j) SELECT timestamp '0560-03-19 23:59:59.999999'; ERROR: 22008 INSERT INTO jt_c_assign (j) SELECT date '3799-03-20'; ERROR: 22008 INSERT INTO jt_c_assign (z) SELECT timestamptz '3799-03-19 08:00:00+00'; ERROR: 22008 -- a jdate whose local midnight is outside the safe range, in a zone east of UTC SET timezone = '<+14>-14'; INSERT INTO jt_c_assign (z) SELECT jdate '-0061-01-02'; ERROR: 22008 INSERT INTO jt_c_assign (z) SELECT date '0560-03-21'; ERROR: 22008 SET timezone = 'UTC'; \set VERBOSITY default SELECT count(*) AS rows_after_the_failed_inserts FROM jt_c_assign; rows_after_the_failed_inserts ------------------------------- 7 (1 row) -- arrays: the element cast, with and without typmod SELECT ARRAY[timestamp '2024-03-20 12:00:00', timestamp '2024-03-21 00:00:00']::jtimestamp[] AS from_timestamp_array; from_timestamp_array ----------------------------------------------- {"1403-01-01 12:00:00","1403-01-02 00:00:00"} (1 row) SELECT ARRAY[timestamptz '2024-03-20 12:00:00+00']::jtimestamptz[] AS from_timestamptz_array; from_timestamptz_array ---------------------------- {"1403-01-01 12:00:00+00"} (1 row) SELECT ARRAY['1403-01-01 12:34:56.789'::jtimestamp]::jtimestamp(0)[] AS rounded_array; rounded_array ------------------------- {"1403-01-01 12:34:57"} (1 row) SELECT '{"1403-01-01 12:34:56.789"}'::jtimestamp(0)[] AS input_array_with_typmod; input_array_with_typmod ------------------------- {"1403-01-01 12:34:57"} (1 row) \set VERBOSITY sqlstate SELECT ARRAY['3177-12-29 23:59:59.999999'::jtimestamp]::jtimestamp(0)[]; ERROR: 22008 SELECT ARRAY[timestamp '3799-03-20 00:00:00']::jtimestamp[]; ERROR: 22008 \set VERBOSITY default -- --------------------------------------------------------------------- -- round trips through the core types: the same bits come back -- --------------------------------------------------------------------- CREATE TABLE jt_c_vals (j jtimestamp, z jtimestamptz); INSERT INTO jt_c_vals (j) VALUES ('-infinity'), ('-0061-01-01 00:00:00'), ('0000-01-01 00:00:00'), ('1403-01-01 00:00:00'), ('1403-01-01 12:34:56.789012'), ('1403-12-29 23:59:59.999999'), ('3177-12-29 23:59:59.999999'), ('infinity'); INSERT INTO jt_c_vals (z) VALUES ('-infinity'), ('-0061-01-01 16:00:00+00'), ('0000-01-01 00:00:00+00'), ('1403-01-01 00:00:00+03:30'), ('1403-01-01 12:34:56.789012+00'), ('1403-12-29 23:59:59.999999-11'), ('3177-12-29 07:59:59.999999+00'), ('infinity'); -- jtimestamp -> timestamp -> jtimestamp, and jtimestamptz -> timestamptz -> jtimestamptz SELECT count(j) AS jtimestamps, count(z) AS jtimestamptzs, count(*) FILTER (WHERE jtimestamp_send(j::timestamp::jtimestamp) <> jtimestamp_send(j)) AS jtimestamp_mismatches, count(*) FILTER (WHERE jtimestamptz_send(z::timestamptz::jtimestamptz) <> jtimestamptz_send(z)) AS jtimestamptz_mismatches FROM jt_c_vals; jtimestamps | jtimestamptzs | jtimestamp_mismatches | jtimestamptz_mismatches -------------+---------------+-----------------------+------------------------- 8 | 8 | 0 | 0 (1 row) -- the Jalali value and the core value are the same bits: core's own send function agrees SELECT count(*) FILTER (WHERE timestamp_send(j::timestamp) <> jtimestamp_send(j)) AS jtimestamp_send_mismatches, count(*) FILTER (WHERE timestamptz_send(z::timestamptz) <> jtimestamptz_send(z)) AS jtimestamptz_send_mismatches FROM jt_c_vals; jtimestamp_send_mismatches | jtimestamptz_send_mismatches ----------------------------+------------------------------ 0 | 0 (1 row) -- core -> Jalali -> core SELECT count(*) AS core_values, count(*) FILTER (WHERE t::jtimestamp::timestamp <> t) AS timestamp_mismatches FROM (VALUES (timestamp '0560-03-20 00:00:00'), ('2024-03-20 00:00:00'), ('2024-03-20 12:34:56.789012'), ('3799-03-19 23:59:59.999999'), ('infinity'), ('-infinity')) v(t); core_values | timestamp_mismatches -------------+---------------------- 6 | 0 (1 row) SELECT count(*) AS core_values, count(*) FILTER (WHERE t::jtimestamptz::timestamptz <> t) AS timestamptz_mismatches FROM (VALUES (timestamptz '0560-03-20 16:00:00+00'), ('2024-03-19 20:30:00+00'), ('2024-03-20 12:34:56.789012+00'), ('3799-03-19 07:59:59.999999+00'), ('infinity'), ('-infinity')) v(t); core_values | timestamptz_mismatches -------------+------------------------ 6 | 0 (1 row) -- the day: jdate -> jtimestamp -> jdate, and the day of a core timestamp read through the types SELECT count(*) AS days, count(*) FILTER (WHERE d::jtimestamp::jdate <> d) AS mismatches, count(*) FILTER (WHERE d::jtimestamp::timestamp::date::jdate <> d) AS through_core_mismatches FROM (VALUES (jdate '-0061-01-01'), ('0000-01-01'), ('1402-12-29'), ('1403-01-01'), ('3177-12-29')) v(d); days | mismatches | through_core_mismatches ------+------------+------------------------- 5 | 0 | 0 (1 row) -- --------------------------------------------------------------------- -- what does not exist: no implicit cast, no cast to the core types by a chain -- --------------------------------------------------------------------- \set VERBOSITY sqlstate -- no operators between a Jalali type and a core type (the Jalali types compare -- with each other, see jt_crossops) SELECT '1403-01-01'::jtimestamp = timestamp '2024-03-20'; ERROR: 42883 SELECT timestamp '2024-03-20' = '1403-01-01'::jtimestamp; ERROR: 42883 SELECT '1403-01-01'::jtimestamptz = timestamptz '2024-03-20 00:00:00+00'; ERROR: 42883 SELECT '1403-01-01'::jtimestamp = date '2024-03-20'; ERROR: 42883 -- no function takes a jtimestamp where it wants a timestamp SELECT date_trunc('day', '1403-01-01'::jtimestamp); ERROR: 42883 SELECT date_trunc('day', '1403-01-01 00:00:00+00'::jtimestamptz); ERROR: 42883 SELECT age('1403-01-01'::jtimestamp); ERROR: 42883 -- and the casts that are not defined directly SELECT '1403-01-01'::jtimestamp::date; ERROR: 42846 SELECT '1403-01-01'::jtimestamp::timestamptz; ERROR: 42846 SELECT '1403-01-01 00:00:00+00'::jtimestamptz::timestamp; ERROR: 42846 SELECT '1403-01-01 00:00:00+00'::jtimestamptz::date; ERROR: 42846 SELECT '2024-03-20'::date::jtimestamp::timestamptz; ERROR: 42846 \set VERBOSITY default -- through an explicit second step they do work SELECT ('1403-01-01 00:00:00'::jtimestamp::timestamp)::date AS via_timestamp, ('1403-01-01 00:00:00+00'::jtimestamptz::timestamptz)::timestamp AS via_timestamptz; via_timestamp | via_timestamptz ---------------+--------------------- 2024-03-20 | 2024-03-20 00:00:00 (1 row) -- jdate(timestamp) and jdate(timestamptz) still resolve, next to the new overloads SELECT jdate(timestamp '2024-03-20 12:00:00') AS from_timestamp, jdate(timestamptz '2024-03-20 12:00:00+00') AS from_timestamptz, jdate('1403-01-01 12:00:00'::jtimestamp) AS from_jtimestamp, jdate('1403-01-01 12:00:00+00'::jtimestamptz) AS from_jtimestamptz; from_timestamp | from_timestamptz | from_jtimestamp | from_jtimestamptz ----------------+------------------+-----------------+------------------- 1403-01-01 | 1403-01-01 | 1403-01-01 | 1403-01-01 (1 row) -- --------------------------------------------------------------------- -- clean up: extension_schema drops the extension, which needs no column of -- its types left behind -- --------------------------------------------------------------------- DROP TABLE jt_c_assign, jt_c_vals;