MobilityDB  1.0
period.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * This MobilityDB code is provided under The PostgreSQL License.
4  *
5  * Copyright (c) 2016-2021, Université libre de Bruxelles and MobilityDB
6  * contributors
7  *
8  * MobilityDB includes portions of PostGIS version 3 source code released
9  * under the GNU General Public License (GPLv2 or later).
10  * Copyright (c) 2001-2021, PostGIS contributors
11  *
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose, without fee, and without a written
14  * agreement is hereby granted, provided that the above copyright notice and
15  * this paragraph and the following two paragraphs appear in all copies.
16  *
17  * IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
18  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
19  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20  * EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
21  * OF SUCH DAMAGE.
22  *
23  * UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
26  * AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
27  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
28  *
29  *****************************************************************************/
30 
37 #ifndef __PERIOD_H__
38 #define __PERIOD_H__
39 
40 #include <postgres.h>
41 #include <lib/stringinfo.h>
42 #include <catalog/pg_type.h>
43 
44 #include "timetypes.h"
45 
46 /*****************************************************************************/
47 
48 /* Input/output functions */
49 
50 extern Datum period_in(PG_FUNCTION_ARGS);
51 extern Datum period_out(PG_FUNCTION_ARGS);
52 extern Datum period_recv(PG_FUNCTION_ARGS);
53 extern Datum period_send(PG_FUNCTION_ARGS);
54 
55 void period_write(const Period *p, StringInfo buf);
56 Period *period_read(StringInfo buf);
57 
58 char *period_to_string(const Period *p);
59 
60 /* Constructors */
61 extern Datum period_constructor2(PG_FUNCTION_ARGS);
62 extern Datum period_constructor4(PG_FUNCTION_ARGS);
63 
64 /* Casting */
65 extern Datum timestamp_to_period(PG_FUNCTION_ARGS);
66 extern Datum period_to_tstzrange(PG_FUNCTION_ARGS);
67 extern Datum tstzrange_to_period(PG_FUNCTION_ARGS);
68 
69 /* period -> timestamptz */
70 extern Datum period_lower(PG_FUNCTION_ARGS);
71 extern Datum period_upper(PG_FUNCTION_ARGS);
72 
73 /* period -> bool */
74 extern Datum period_lower_inc(PG_FUNCTION_ARGS);
75 extern Datum period_upper_inc(PG_FUNCTION_ARGS);
76 
77 /* period -> period */
78 extern Datum period_shift(PG_FUNCTION_ARGS);
79 
80 extern Period *period_shift_internal(const Period *p,
81  const Interval *start);
82 extern void period_shift_tscale(Period *result, const Interval *start,
83  const Interval *duration);
84 
85 /* period -> interval */
86 
87 extern Datum period_duration(PG_FUNCTION_ARGS);
88 
89 /* Functions for defining B-tree index */
90 
91 extern Datum period_eq(PG_FUNCTION_ARGS);
92 extern Datum period_ne(PG_FUNCTION_ARGS);
93 extern Datum period_cmp(PG_FUNCTION_ARGS);
94 extern Datum period_lt(PG_FUNCTION_ARGS);
95 extern Datum period_le(PG_FUNCTION_ARGS);
96 extern Datum period_ge(PG_FUNCTION_ARGS);
97 extern Datum period_gt(PG_FUNCTION_ARGS);
98 
99 extern bool period_eq_internal(const Period *p1, const Period *p2);
100 extern bool period_ne_internal(const Period *p1, const Period *p2);
101 extern int period_cmp_internal(const Period *p1, const Period *p2);
102 extern bool period_lt_internal(const Period *p1, const Period *p2);
103 extern bool period_le_internal(const Period *p1, const Period *p2);
104 extern bool period_ge_internal(const Period *p1, const Period *p2);
105 extern bool period_gt_internal(const Period *p1, const Period *p2);
106 
107 /* Assorted support functions */
108 
109 extern void period_deserialize(const Period *p, PeriodBound *lower, PeriodBound *upper);
110 extern int period_cmp_bounds(const PeriodBound *lower, const PeriodBound *upper);
111 extern Period *period_make(TimestampTz lower, TimestampTz upper,
112  bool lower_inc, bool upper_inc);
113 extern void period_set(Period *p, TimestampTz lower, TimestampTz upper,
114  bool lower_inc, bool upper_inc);
115 extern Period *period_copy(const Period *p);
116 extern float8 period_to_secs(TimestampTz t1, TimestampTz t2);
117 extern Interval *period_duration_internal(const Period *p);
118 extern Period **periodarr_normalize(Period **periods, int count, int *newcount);
119 extern Period *period_super_union(const Period *p1, const Period *p2);
120 extern void period_expand(Period *p1, const Period *p2);
121 
122 extern int period_bound_qsort_cmp(const void *a1, const void *a2);
123 
124 #endif
125 
126 /*****************************************************************************/
Datum period_constructor2(PG_FUNCTION_ARGS)
Construct a period from the two arguments.
Definition: period.c:431
Structure to represent periods.
Definition: timetypes.h:52
Datum period_out(PG_FUNCTION_ARGS)
Output function for periods.
Definition: period.c:361
Datum period_gt(PG_FUNCTION_ARGS)
Returns true if the first period is greater than the second one.
Definition: period.c:828
Internal representation of either bound of a period (not what&#39;s on disk)
Definition: timetypes.h:63
void period_deserialize(const Period *p, PeriodBound *lower, PeriodBound *upper)
Deconstruct the period.
Definition: period.c:85
Datum period_in(PG_FUNCTION_ARGS)
Input function for periods.
Definition: period.c:316
int period_cmp_internal(const Period *p1, const Period *p2)
Returns -1, 0, or 1 depending on whether the first period is less than, equal, or greater than the se...
Definition: period.c:713
Datum timestamp_to_period(PG_FUNCTION_ARGS)
Cast the timestamp value as a period.
Definition: period.c:470
Datum period_lower_inc(PG_FUNCTION_ARGS)
Returns true if the lower bound value is inclusive.
Definition: period.c:561
void period_write(const Period *p, StringInfo buf)
Send function for periods (internal function)
Definition: period.c:371
int period_bound_qsort_cmp(const void *a1, const void *a2)
Comparison function for sorting period bounds.
Definition: period.c:159
Datum period_send(PG_FUNCTION_ARGS)
Send function for periods.
Definition: period.c:388
void period_set(Period *p, TimestampTz lower, TimestampTz upper, bool lower_inc, bool upper_inc)
Set the period from the argument values.
Definition: period.c:182
bool period_ne_internal(const Period *p1, const Period *p2)
Returns true if the first period is different from the second one (internal function) ...
Definition: period.c:686
int period_cmp_bounds(const PeriodBound *lower, const PeriodBound *upper)
Compare two period boundary points, returning <0, 0, or >0 according to whether b1 is less than...
Definition: period.c:124
bool period_eq_internal(const Period *p1, const Period *p2)
Returns true if the first period is equal to the second one (internal function)
Definition: period.c:661
Period * period_shift_internal(const Period *p, const Interval *start)
Shift the period by the interval (internal function)
Definition: period.c:582
Datum period_ge(PG_FUNCTION_ARGS)
Returns true if the first period is greater than or equal to the second one.
Definition: period.c:805
Datum period_upper(PG_FUNCTION_ARGS)
Returns the upper bound value.
Definition: period.c:548
bool period_lt_internal(const Period *p1, const Period *p2)
Returns true if the first period is less than the second one (internal function)
Definition: period.c:748
Datum period_duration(PG_FUNCTION_ARGS)
Returns the duration of the period.
Definition: period.c:642
Datum period_lower(PG_FUNCTION_ARGS)
Returns the lower bound value.
Definition: period.c:537
Period * period_make(TimestampTz lower, TimestampTz upper, bool lower_inc, bool upper_inc)
Construct a period from the bounds.
Definition: period.c:170
Period * period_read(StringInfo buf)
Receive function for periods (internal function)
Definition: period.c:401
Datum period_constructor4(PG_FUNCTION_ARGS)
Construct a period from the four arguments.
Definition: period.c:448
void period_shift_tscale(Period *result, const Interval *start, const Interval *duration)
Shift and/or scale the period by the two intervals (internal function)
Definition: period.c:611
Datum period_cmp(PG_FUNCTION_ARGS)
Returns -1, 0, or 1 depending on whether the first period is less than, equal, or greater than the se...
Definition: period.c:734
Datum period_recv(PG_FUNCTION_ARGS)
Receive function for periods.
Definition: period.c:416
Functions for time types based on TimestampTz, that is, TimestampSet, Period, and PeriodSet...
Datum period_ne(PG_FUNCTION_ARGS)
Returns true if the first period is different from the second one.
Definition: period.c:696
bool period_gt_internal(const Period *p1, const Period *p2)
Returns true if the first period is greater than the second one (internal function) ...
Definition: period.c:817
Datum tstzrange_to_period(PG_FUNCTION_ARGS)
Convert the tstzrange value as a period.
Definition: period.c:497
Interval * period_duration_internal(const Period *p)
Returns the duration of the period (internal function)
Definition: period.c:631
void period_expand(Period *p1, const Period *p2)
Expand the first period with the second one.
Definition: period.c:295
Datum period_shift(PG_FUNCTION_ARGS)
Shift the period by the interval.
Definition: period.c:599
bool period_le_internal(const Period *p1, const Period *p2)
Returns true if the first period is less than or equal to the second one (internal function) ...
Definition: period.c:771
Period ** periodarr_normalize(Period **periods, int count, int *newcount)
Normalize an array of periods.
Definition: period.c:235
Period * period_super_union(const Period *p1, const Period *p2)
Returns the smallest period that contains p1 and p2.
Definition: period.c:284
Datum period_upper_inc(PG_FUNCTION_ARGS)
Returns true if the upper bound value is inclusive.
Definition: period.c:572
Datum period_le(PG_FUNCTION_ARGS)
Returns true if the first period is less than or equal to the second one.
Definition: period.c:782
Period * period_copy(const Period *p)
Returns a copy of the period.
Definition: period.c:207
bool upper_inc(RangeType *range)
Returns true if the upper bound of the range value is inclusive.
Definition: rangetypes_ext.c:104
bool period_ge_internal(const Period *p1, const Period *p2)
Returns true if the first period is greater than or equal to the second one (internal function) ...
Definition: period.c:794
bool lower_inc(RangeType *range)
Returns true if the lower bound of the range value is inclusive.
Definition: rangetypes_ext.c:91
Datum period_to_tstzrange(PG_FUNCTION_ARGS)
Convert the period as a tstzrange value.
Definition: period.c:482
char * period_to_string(const Period *p)
Returns the string representation of the period.
Definition: period.c:346
Datum period_lt(PG_FUNCTION_ARGS)
Returns true if the first period is less than the second one.
Definition: period.c:759
Datum period_eq(PG_FUNCTION_ARGS)
Returns true if the first period is equal to the second one.
Definition: period.c:674
float8 period_to_secs(TimestampTz t1, TimestampTz t2)
Returns the number of seconds of the period as a float8 value.
Definition: period.c:218