A temporal value can be transformed to another subtype. An error is raised if the subtypes are incompatible.
Transform a temporal value to another subtype
ttypeInst(ttype) → ttypeInst ttypeSeq(ttype) → ttypeSeq ttypeSeq(ttype,interp text) → ttypeSeq ttypeSeqSet(ttype) → ttypeSeqSet ttypeSeqSet(ttype,interp text) → ttypeSeqSet
Without an interpolation the value keeps its own, an instant taking the default interpolation of its type; with one, the value takes the interpolation given.
SELECT tboolInst(tbool '{[true@2001-01-01]}');
-- t@2001-01-01
SELECT tboolInst(tbool '{[true@2001-01-01, true@2001-01-02]}');
-- ERROR: Cannot transform input value to a temporal instant
SELECT tintSeq(tint '1@2001-01-01');
-- [1@2001-01-01]
SELECT tfloatSeqSet(tfloat '2.5@2001-01-01');
-- {[2.5@2001-01-01]}
SELECT tfloatSeqSet(tfloat '{2.5@2001-01-01, 1.5@2001-01-02, 3.5@2001-01-03}');
-- {[2.5@2001-01-01],[1.5@2001-01-02],[3.5@2001-01-03]}
Transform a temporal value to another interpolation
setInterp(ttype,interp text) → ttype
SELECT setInterp(tbool 'true@2001-01-01','discrete');
-- {t@2001-01-01}
SELECT setInterp(tfloat '{[1@2001-01-01], [2@2001-01-02], [1@2001-01-03]}', 'discrete');
-- {1@2001-01-01, 2@2001-01-02, 1@2001-01-03}
SELECT setInterp(tfloat 'Interp=Step;
[1@2001-01-01, 2@2001-01-02, 1@2001-01-03, 2@2001-01-04]', 'linear');
/* {[1@2001-01-01, 1@2001-01-02), [2@2001-01-02, 2@2001-01-03),
[1@2001-01-03, 1@2001-01-04), [2@2001-01-04]} */
SELECT asText(setInterp(tgeompoint 'Interp=Step;{[Point(1 1)@2001-01-01,
Point(2 2)@2001-01-02], [Point(3 3)@2001-01-05, Point(4 4)@2001-01-06]}', 'linear'));
/* {[POINT(1 1)@2001-01-01, POINT(1 1)@2001-01-02), [POINT(2 2)@2001-01-02],
[POINT(3 3)@2001-01-05, POINT(3 3)@2001-01-06), [POINT(4 4)@2001-01-06]} */
SELECT setInterp(tgeometry '[Point(1 1)@2001-01-01, Linestring(1 1,2 2)@2001-01-02]',
'linear');
-- ERROR: The temporal type cannot have linear interpolation
Return a temporal boolean stating whether each segment of a continuous temporal sequence (set) has, respectively, at least or at most a given duration
segmentMinDuration(ttype,interval,strict boolean=true) → tbool segmentMaxDuration(ttype,interval,strict boolean=true) → tbool
If strict is not specified, the value true is assumed by default, and therefore the function checks whether the segment duration is strictly less than or greater than the interval. If the argument is set to false, the function checks whether the segment duration is less/greater than or equal to the interval.
SELECT segmentMinDuration(tfloat '[1@2001-01-01, 0@2001-01-03, -1@2001-01-04,
-1@2001-01-06]', '1 day');
-- {[t@2001-01-01, f@2001-01-03, t@2001-01-04, t@2001-01-06]}
SELECT segmentMinDuration(tfloat '[1@2001-01-01, 0@2001-01-03, -1@2001-01-04,
-1@2001-01-06]', '1 day', false);
-- {[t@2001-01-01, t@2001-01-06]}
SELECT segmentMaxDuration(tfloat '[1@2001-01-01, 0@2001-01-03, -1@2001-01-04,
-1@2001-01-06]', '1 day');
-- {[f@2001-01-01, f@2001-01-06]}
SELECT segmentMaxDuration(tfloat '[1@2001-01-01, 0@2001-01-03, -1@2001-01-04,
-1@2001-01-06]', '1 day', false);
-- {[f@2001-01-01, t@2001-01-03, f@2001-01-04, f@2001-01-06]}
Shift and/or scale the time span of a temporal value by one or two intervals
shiftTime(ttype,interval) → ttype scaleTime(ttype,interval) → ttype shiftScaleTime(ttype,interval,interval) → ttype
The given intervals must be strictly greater than zero. If the time span of the temporal value is zero (for example, for a temporal instant), the result of a scale operation is the temporal value.
SELECT shiftTime(tint '{1@2001-01-01, 2@2001-01-03, 1@2001-01-05}', '1 day');
-- {1@2001-01-02, 2@2001-01-04, 1@2001-01-06}
SELECT shiftTime(tfloat '[1@2001-01-01, 2@2001-01-03]', '1 day');
-- [1@2001-01-02, 2@2001-01-04]
SELECT asText(shiftTime(tgeompoint '{[Point(1 1)@2001-01-01, Point(2 2)@2001-01-03],
[Point(2 2)@2001-01-04, Point(1 1)@2001-01-05]}', '1 day'));
/* {[POINT(1 1)@2001-01-02, POINT(2 2)@2001-01-04],
[POINT(2 2)@2001-01-05, POINT(1 1)@2001-01-06]} */
SELECT scaleTime(tint '1@2001-01-01', '1 day');
-- 1@2001-01-01
SELECT scaleTime(tint '{1@2001-01-01, 2@2001-01-03, 1@2001-01-05}', '1 day');
-- {1@2001-01-01, 2@2001-01-01 12:00:00, 1@2001-01-02}
SELECT scaleTime(tfloat '[1@2001-01-01, 2@2001-01-03]', '1 day');
-- [1@2001-01-01, 2@2001-01-02]
SELECT asText(scaleTime(tgeompoint '{[Point(1 1)@2001-01-01, Point(2 2)@2001-01-02,
Point(1 1)@2001-01-03], [Point(2 2)@2001-01-04, Point(1 1)@2001-01-05]}', '1 day'));
/* {[POINT(1 1)@2001-01-01, POINT(2 2)@2001-01-01 06:00:00,
POINT(1 1)@2001-01-01 12:00:00], [POINT(2 2) @2001-01-01 18:00:00,
POINT(1 1)@2001-01-02]} */
SELECT scaleTime(tint '1@2001-01-01', '-1 day');
-- ERROR: The interval must be positive: -1 days
SELECT shiftScaleTime(tint '1@2001-01-01', '1 day', '1 day');
-- 1@2001-01-02
SELECT shiftScaleTime(tint '{1@2001-01-01, 2@2001-01-03, 1@2001-01-05}', '1 day','1 day');
-- {1@2001-01-02, 2@2001-01-02 12:00:00, 1@2001-01-03}
SELECT shiftScaleTime(tfloat '[1@2001-01-01, 2@2001-01-03]', '1 day', '1 day');
-- [1@2001-01-02, 2@2001-01-03]
SELECT asText(shiftScaleTime(tgeometry '{[Point(1 1)@2001-01-01,
Linestring(1 1,2 2)@2001-01-02, Point(1 1)@2001-01-03],
[Point(2 2)@2001-01-04, Polygon((1 1,2 2,3 1,1 1))@2001-01-05]}', '1 day', '1 day'));
/* {[POINT(1 1)@2001-01-02 00:00:00, LINESTRING(1 1,2 2)@2001-01-02 06:00:00,
POINT(1 1)@2001-01-02 12:00:00], [POINT(2 2)@2001-01-02 18:00:00,
POLYGON((1 1,2 2,3 1,1 1))@2001-01-03 00:00:00]} */
Return the occurrences of a periodic temporal value
weekdays(datespan,integer) → dateset
origins(dateset,zone text) → tstzset
occurrence(ttype,ref timestamptz,origin timestamptz) → ttype
occurrenceOrigins(ttype,ref timestamptz,origins tstzset,period tstzspan) → tstzset
occurrences(ttype,ref timestamptz,origins tstzset,period tstzspan) →
{(timestamptz,ttype)}
A periodic phenomenon, such as a trip of a timetable run on many days, is a temporal value describing one run from a reference origin, its template, with the set of origins at which it runs again. The function weekdays returns the dates of a date span that fall on the days of the week given as a mask, bit 0 for Monday to bit 6 for Sunday as ISO 8601 numbers them, so that 31 is Monday to Friday and 96 the weekend, or NULL when no date does: it is the weekly rule of a schedule, to which the dates added and removed apply as the union and difference of date sets. The function origins returns the origins of a set of service days in a time zone: the origin of a day is its local noon minus twelve hours, as the General Transit Feed Specification measures the times of a service day, which is the local midnight except on the days of a daylight-saving change. The function occurrence returns the template shifted by the elapsed duration from the reference origin to an origin, so that every instant keeps its distance from the origin. The function occurrenceOrigins returns the origins whose occurrence has a period overlapping the period given, found by binary search, or NULL when there is none, and the table function occurrences returns those occurrences with their origins, building no other occurrence.
SET timezone TO 'UTC';
SELECT weekdays(datespan '[2026-12-24, 2027-01-06)', 96);
-- {2026-12-26, 2026-12-27, 2027-01-02, 2027-01-03}
SELECT origins(dateset '{2026-03-28, 2026-03-29, 2026-03-30}', 'Europe/Zurich');
-- {"2026-03-27 23:00:00+00", "2026-03-28 22:00:00+00", "2026-03-29 22:00:00+00"}
SELECT occurrence(tint '[1@2026-01-14 23:30:00, 2@2026-01-15 00:30:00]',
'2026-01-14 23:00:00', '2026-03-28 22:00:00');
-- [1@2026-03-28 22:30:00+00, 2@2026-03-28 23:30:00+00]
SELECT occurrenceOrigins(tint '[1@2026-01-14 23:30:00, 2@2026-01-15 00:30:00]',
'2026-01-14 23:00:00', tstzset '{2026-01-14 23:00:00, 2026-01-15 23:00:00,
2026-01-16 23:00:00}', tstzspan '[2026-01-15 23:00:00, 2026-01-15 23:30:00]');
-- {"2026-01-15 23:00:00+00"}
SELECT * FROM occurrences(tint '[1@2026-01-14 23:30:00, 2@2026-01-15 00:30:00]',
'2026-01-14 23:00:00', tstzset '{2026-01-14 23:00:00, 2026-01-15 23:00:00,
2026-01-16 23:00:00}', tstzspan '[2026-01-15 00:00:00, 2026-01-16 00:00:00]');
/* 2026-01-14 23:00:00+00 | [1@2026-01-14 23:30:00+00, 2@2026-01-15 00:30:00+00]
2026-01-15 23:00:00+00 | [1@2026-01-15 23:30:00+00, 2@2026-01-16 00:30:00+00] */
Transform a nonlinear temporal value into a set of rows, each one is a pair composed of a base value and a period set during which the temporal value has the base value
unnest(ttype) → {(value,time)}
SELECT (un).value, (un).time
FROM (SELECT unnest(tfloat '{1@2001-01-01, 2@2001-01-02, 1@2001-01-03}') AS un) t;
-- 1 | {[2001-01-01, 2001-01-01], [2001-01-03, 2001-01-03]}
-- 2 | {[2001-01-02, 2001-01-02]}
SELECT (un).value, (un).time
FROM (SELECT unnest(tint '[1@2001-01-01, 2@2001-01-02, 1@2001-01-03]') AS un) t;
-- 1 | {[2001-01-01, 2001-01-02), [2001-01-03, 2001-01-03]}
-- 2 | {[2001-01-02, 2001-01-03)}
SELECT (un).value, (un).time
FROM (SELECT unnest(tfloat '[1@2001-01-01, 2@2001-01-02, 1@2001-01-03]') AS un) t;
-- ERROR: Operation not supported for temporal values with linear interpolation
SELECT ST_AsText((un).value),
(un).time FROM (SELECT unnest(tgeompoint 'Interp=Step;
[Point(1 1)@2001-01-01, Point(2 2)@2001-01-02, Point(1 1)@2001-01-03]') AS un) t;
-- POINT(1 1) | {[2001-01-01, 2001-01-02), [2001-01-03, 2001-01-03]}
-- POINT(2 2) | {[2001-01-02, 2001-01-03)}
SELECT ST_AsText((un).value),
(un).time FROM (SELECT unnest(tgeometry '[Point(1 1)@2001-01-01,
Linestring(1 1,2 2)@2001-01-02, Point(1 1)@2001-01-03]') AS un) t;
-- POINT(1 1) | {[2001-01-01, 2001-01-02), [2001-01-03, 2001-01-03]}
-- LINESTRING(1 1,2 2) | {[2001-01-02, 2001-01-03)}