PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgtypes_timestamp.h File Reference
#include <pgtypes.h>
#include <pgtypes_interval.h>
Include dependency graph for pgtypes_timestamp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef int64 timestamp
 
typedef int64 TimestampTz
 

Functions

timestamp PGTYPEStimestamp_from_asc (char *str, char **endptr)
 
charPGTYPEStimestamp_to_asc (timestamp tstamp)
 
int PGTYPEStimestamp_sub (timestamp *ts1, timestamp *ts2, interval *iv)
 
int PGTYPEStimestamp_fmt_asc (timestamp *ts, char *output, int str_len, const char *fmtstr)
 
void PGTYPEStimestamp_current (timestamp *ts)
 
int PGTYPEStimestamp_defmt_asc (const char *str, const char *fmt, timestamp *d)
 
int PGTYPEStimestamp_add_interval (timestamp *tin, interval *span, timestamp *tout)
 
int PGTYPEStimestamp_sub_interval (timestamp *tin, interval *span, timestamp *tout)
 

Typedef Documentation

◆ timestamp

Definition at line 10 of file pgtypes_timestamp.h.

◆ TimestampTz

Definition at line 11 of file pgtypes_timestamp.h.

Function Documentation

◆ PGTYPEStimestamp_add_interval()

int PGTYPEStimestamp_add_interval ( timestamp tin,
interval span,
timestamp tout 
)
extern

Definition at line 854 of file timestamp.c.

855{
857 *tout = *tin;
858 else
859 {
860 if (span->month != 0)
861 {
862 struct tm tt,
863 *tm = &tt;
864 fsec_t fsec;
865
866 if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) != 0)
867 return -1;
868 tm->tm_mon += span->month;
870 {
871 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
872 tm->tm_mon = (tm->tm_mon - 1) % MONTHS_PER_YEAR + 1;
873 }
874 else if (tm->tm_mon < 1)
875 {
878 }
879
880
881 /* adjust for end of month boundary problems... */
882 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
883 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
884
885
886 if (tm2timestamp(tm, fsec, NULL, tin) != 0)
887 return -1;
888 }
889
890 *tin += span->time;
891 *tout = *tin;
892 }
893
894 return 0;
895}
const int day_tab[2][13]
Definition datetime.c:76
int32 fsec_t
Definition timestamp.h:41
#define MONTHS_PER_YEAR
Definition timestamp.h:108
#define TIMESTAMP_NOT_FINITE(j)
Definition timestamp.h:169
#define isleap(y)
Definition datetime.h:271
static int timestamp2tm(timestamp dt, int *tzp, struct tm *tm, fsec_t *fsec, const char **tzn)
Definition timestamp.c:88
int tm2timestamp(struct tm *tm, fsec_t fsec, int *tzp, timestamp *result)
Definition timestamp.c:37
static struct pg_tm tm
Definition localtime.c:104
static int fb(int x)
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_year
Definition pgtime.h:41

References day_tab, fb(), isleap, MONTHS_PER_YEAR, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by main(), and PGTYPEStimestamp_sub_interval().

◆ PGTYPEStimestamp_current()

void PGTYPEStimestamp_current ( timestamp ts)
extern

Definition at line 286 of file timestamp.c.

287{
288 struct tm tm;
289
291 if (errno == 0)
292 tm2timestamp(&tm, 0, NULL, ts);
293}
void GetCurrentDateTime(struct pg_tm *tm)
Definition datetime.c:377

References fb(), GetCurrentDateTime(), tm, and tm2timestamp().

Referenced by dtcurrent(), and main().

◆ PGTYPEStimestamp_defmt_asc()

int PGTYPEStimestamp_defmt_asc ( const char str,
const char fmt,
timestamp d 
)
extern

Definition at line 802 of file timestamp.c.

803{
804 int year,
805 month,
806 day;
807 int hour,
808 minute,
809 second;
810 int tz;
811
812 int i;
813 char *mstr;
814 char *mfmt;
815
816 if (!fmt)
817 fmt = "%Y-%m-%d %H:%M:%S";
818 if (!fmt[0])
819 return 1;
820
823
824 /*
825 * initialize with impossible values so that we can see if the fields
826 * where specified at all
827 */
828 /* XXX ambiguity with 1 BC for year? */
829 year = -1;
830 month = -1;
831 day = -1;
832 hour = 0;
833 minute = -1;
834 second = -1;
835 tz = 0;
836
837 i = PGTYPEStimestamp_defmt_scan(&mstr, mfmt, d, &year, &month, &day, &hour, &minute, &second, &tz);
838 free(mstr);
839 free(mfmt);
840 return i;
841}
int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp *d, int *year, int *month, int *day, int *hour, int *minute, int *second, int *tz)
Definition dt_common.c:2521
const char * str
char * pgtypes_strdup(const char *str)
Definition common.c:20
int i
Definition isn.c:77
#define free(a)

References fb(), free, i, pgtypes_strdup(), PGTYPEStimestamp_defmt_scan(), and str.

Referenced by dtcvfmtasc(), and main().

◆ PGTYPEStimestamp_fmt_asc()

int PGTYPEStimestamp_fmt_asc ( timestamp ts,
char output,
int  str_len,
const char fmtstr 
)
extern

Definition at line 774 of file timestamp.c.

775{
776 struct tm tm;
777 fsec_t fsec;
778 date dDate;
779 int dow;
780
783 timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
784
786}
FILE * output
static int dttofmtasc_replace(timestamp *ts, date dDate, int dow, struct tm *tm, char *output, int *pstr_len, const char *fmtstr)
Definition timestamp.c:296
date PGTYPESdate_from_timestamp(timestamp dt)
Definition datetime.c:31
int PGTYPESdate_dayofweek(date dDate)
Definition datetime.c:138
long date
Definition pgtypes_date.h:9
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition snprintf.c:974

References dttofmtasc_replace(), fb(), fmtstr(), output, PGTYPESdate_dayofweek(), PGTYPESdate_from_timestamp(), timestamp2tm(), and tm.

Referenced by dttofmtasc(), and main().

◆ PGTYPEStimestamp_from_asc()

timestamp PGTYPEStimestamp_from_asc ( char str,
char **  endptr 
)
extern

Definition at line 198 of file timestamp.c.

199{
200 timestamp result;
201 int64 noresult = 0;
202 fsec_t fsec;
203 struct tm tt,
204 *tm = &tt;
205 int dtype;
206 int nf;
207 char *field[MAXDATEFIELDS];
208 int ftype[MAXDATEFIELDS];
210 char *realptr;
211 char **ptr = (endptr != NULL) ? endptr : &realptr;
212
213 if (strlen(str) > MAXDATELEN)
214 {
216 return noresult;
217 }
218
219 if (ParseDateTime(str, lowstr, field, ftype, &nf, ptr) != 0 ||
220 DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, 0) != 0)
221 {
223 return noresult;
224 }
225
226 switch (dtype)
227 {
228 case DTK_DATE:
229 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
230 {
232 return noresult;
233 }
234 break;
235
236 case DTK_EPOCH:
237 result = SetEpochTimestamp();
238 break;
239
240 case DTK_LATE:
241 TIMESTAMP_NOEND(result);
242 break;
243
244 case DTK_EARLY:
245 TIMESTAMP_NOBEGIN(result);
246 break;
247
248 default:
250 return noresult;
251 }
252
253 /* AdjustTimestampForTypmod(&result, typmod); */
254
255 /*
256 * Since it's difficult to test for noresult, make sure errno is 0 if no
257 * error occurred.
258 */
259 errno = 0;
260 return result;
261}
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition datetime.c:774
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition datetime.c:998
int64_t int64
Definition c.h:615
#define TIMESTAMP_NOBEGIN(j)
Definition timestamp.h:159
#define TIMESTAMP_NOEND(j)
Definition timestamp.h:164
#define MAXDATEFIELDS
Definition datetime.h:202
#define DTK_EPOCH
Definition datetime.h:152
#define DTK_LATE
Definition datetime.h:151
#define DTK_DATE
Definition datetime.h:144
#define DTK_EARLY
Definition datetime.h:150
#define MAXDATELEN
Definition datetime.h:200
static timestamp SetEpochTimestamp(void)
Definition timestamp.c:62
#define PGTYPES_TS_BAD_TIMESTAMP
int64 timestamp

References DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, fb(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PGTYPES_TS_BAD_TIMESTAMP, SetEpochTimestamp(), str, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by dtcvasc(), ecpg_get_data(), and main().

◆ PGTYPEStimestamp_sub()

int PGTYPEStimestamp_sub ( timestamp ts1,
timestamp ts2,
interval iv 
)
extern

Definition at line 789 of file timestamp.c.

790{
793 else
794 iv->time = (*ts1 - *ts2);
795
796 iv->month = 0;
797
798 return 0;
799}
#define PGTYPES_TS_ERR_EINFTIME

References fb(), interval::month, PGTYPES_TS_ERR_EINFTIME, interval::time, and TIMESTAMP_NOT_FINITE.

Referenced by dtsub().

◆ PGTYPEStimestamp_sub_interval()

int PGTYPEStimestamp_sub_interval ( timestamp tin,
interval span,
timestamp tout 
)
extern

Definition at line 909 of file timestamp.c.

910{
912
913 tspan.month = -span->month;
914 tspan.time = -span->time;
915
917}
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition timestamp.c:854

References fb(), interval::month, and PGTYPEStimestamp_add_interval().

◆ PGTYPEStimestamp_to_asc()

char * PGTYPEStimestamp_to_asc ( timestamp  tstamp)
extern

Definition at line 264 of file timestamp.c.

265{
266 struct tm tt,
267 *tm = &tt;
268 char buf[MAXDATELEN + 1];
269 fsec_t fsec;
270 int DateStyle = 1; /* this defaults to USE_ISO_DATES, shall we
271 * make it an option? */
272
275 else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
276 EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
277 else
278 {
280 return NULL;
281 }
282 return pgtypes_strdup(buf);
283}
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition datetime.c:4465
int DateStyle
Definition globals.c:125
static void EncodeSpecialTimestamp(timestamp dt, char *str)
Definition timestamp.c:187
static char buf[DEFAULT_XLOG_SEG_SIZE]

References buf, DateStyle, EncodeDateTime(), EncodeSpecialTimestamp(), fb(), MAXDATELEN, pgtypes_strdup(), PGTYPES_TS_BAD_TIMESTAMP, timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

Referenced by dttoasc(), ecpg_store_input(), and main().