PostgreSQL Source Code  git master
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)
 
char * PGTYPEStimestamp_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

typedef int64 timestamp

Definition at line 10 of file pgtypes_timestamp.h.

◆ TimestampTz

typedef int64 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 
)

Definition at line 858 of file timestamp.c.

859 {
860  if (TIMESTAMP_NOT_FINITE(*tin))
861  *tout = *tin;
862  else
863  {
864  if (span->month != 0)
865  {
866  struct tm tt,
867  *tm = &tt;
868  fsec_t fsec;
869 
870  if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) != 0)
871  return -1;
872  tm->tm_mon += span->month;
873  if (tm->tm_mon > MONTHS_PER_YEAR)
874  {
875  tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
876  tm->tm_mon = (tm->tm_mon - 1) % MONTHS_PER_YEAR + 1;
877  }
878  else if (tm->tm_mon < 1)
879  {
880  tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
882  }
883 
884 
885  /* adjust for end of month boundary problems... */
886  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
887  tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
888 
889 
890  if (tm2timestamp(tm, fsec, NULL, tin) != 0)
891  return -1;
892  }
893 
894  *tin += span->time;
895  *tout = *tin;
896  }
897 
898  return 0;
899 }
const int day_tab[2][13]
Definition: datetime.c:75
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1987
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1891
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 struct pg_tm tm
Definition: localtime.c:104
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_year
Definition: pgtime.h:41

References day_tab, isleap, interval::month, MONTHS_PER_YEAR, interval::time, 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)

Definition at line 290 of file timestamp.c.

291 {
292  struct tm tm;
293 
295  if (errno == 0)
296  tm2timestamp(&tm, 0, NULL, ts);
297 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:366

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

Referenced by dtcurrent(), and main().

◆ PGTYPEStimestamp_defmt_asc()

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

Definition at line 806 of file timestamp.c.

807 {
808  int year,
809  month,
810  day;
811  int hour,
812  minute,
813  second;
814  int tz;
815 
816  int i;
817  char *mstr;
818  char *mfmt;
819 
820  if (!fmt)
821  fmt = "%Y-%m-%d %H:%M:%S";
822  if (!fmt[0])
823  return 1;
824 
825  mstr = pgtypes_strdup(str);
826  mfmt = pgtypes_strdup(fmt);
827 
828  /*
829  * initialize with impossible values so that we can see if the fields
830  * where specified at all
831  */
832  /* XXX ambiguity with 1 BC for year? */
833  year = -1;
834  month = -1;
835  day = -1;
836  hour = 0;
837  minute = -1;
838  second = -1;
839  tz = 0;
840 
841  i = PGTYPEStimestamp_defmt_scan(&mstr, mfmt, d, &year, &month, &day, &hour, &minute, &second, &tz);
842  free(mstr);
843  free(mfmt);
844  return i;
845 }
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
#define free(a)
Definition: header.h:65
char * pgtypes_strdup(const char *str)
Definition: common.c:20
int i
Definition: isn.c:73
static void const char * fmt

References fmt, 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 
)

Definition at line 778 of file timestamp.c.

779 {
780  struct tm tm;
781  fsec_t fsec;
782  date dDate;
783  int dow;
784 
785  dDate = PGTYPESdate_from_timestamp(*ts);
786  dow = PGTYPESdate_dayofweek(dDate);
787  timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
788 
789  return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);
790 }
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:300
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:967

References dttofmtasc_replace(), 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 
)

Definition at line 202 of file timestamp.c.

203 {
204  timestamp result;
205  int64 noresult = 0;
206  fsec_t fsec;
207  struct tm tt,
208  *tm = &tt;
209  int dtype;
210  int nf;
211  char *field[MAXDATEFIELDS];
212  int ftype[MAXDATEFIELDS];
213  char lowstr[MAXDATELEN + MAXDATEFIELDS];
214  char *realptr;
215  char **ptr = (endptr != NULL) ? endptr : &realptr;
216 
217  if (strlen(str) > MAXDATELEN)
218  {
219  errno = PGTYPES_TS_BAD_TIMESTAMP;
220  return noresult;
221  }
222 
223  if (ParseDateTime(str, lowstr, field, ftype, &nf, ptr) != 0 ||
224  DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, 0) != 0)
225  {
226  errno = PGTYPES_TS_BAD_TIMESTAMP;
227  return noresult;
228  }
229 
230  switch (dtype)
231  {
232  case DTK_DATE:
233  if (tm2timestamp(tm, fsec, NULL, &result) != 0)
234  {
235  errno = PGTYPES_TS_BAD_TIMESTAMP;
236  return noresult;
237  }
238  break;
239 
240  case DTK_EPOCH:
241  result = SetEpochTimestamp();
242  break;
243 
244  case DTK_LATE:
245  TIMESTAMP_NOEND(result);
246  break;
247 
248  case DTK_EARLY:
249  TIMESTAMP_NOBEGIN(result);
250  break;
251 
252  default:
253  errno = PGTYPES_TS_BAD_TIMESTAMP;
254  return noresult;
255  }
256 
257  /* AdjustTimestampForTypmod(&result, typmod); */
258 
259  /*
260  * Since it's difficult to test for noresult, make sure errno is 0 if no
261  * error occurred.
262  */
263  errno = 0;
264  return result;
265 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:754
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition: datetime.c:978
Timestamp SetEpochTimestamp(void)
Definition: timestamp.c:2171
#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
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15
int64 timestamp

References DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, 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 
)

Definition at line 793 of file timestamp.c.

794 {
795  if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))
797  else
798  iv->time = (*ts1 - *ts2);
799 
800  iv->month = 0;
801 
802  return 0;
803 }
#define PGTYPES_TS_ERR_EINFTIME
Definition: pgtypes_error.h:16

References 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 
)

Definition at line 913 of file timestamp.c.

914 {
915  interval tspan;
916 
917  tspan.month = -span->month;
918  tspan.time = -span->time;
919 
920  return PGTYPEStimestamp_add_interval(tin, &tspan, tout);
921 }
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:858

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

◆ PGTYPEStimestamp_to_asc()

char* PGTYPEStimestamp_to_asc ( timestamp  tstamp)

Definition at line 268 of file timestamp.c.

269 {
270  struct tm tt,
271  *tm = &tt;
272  char buf[MAXDATELEN + 1];
273  fsec_t fsec;
274  int DateStyle = 1; /* this defaults to USE_ISO_DATES, shall we
275  * make it an option? */
276 
277  if (TIMESTAMP_NOT_FINITE(tstamp))
278  EncodeSpecialTimestamp(tstamp, buf);
279  else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
280  EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
281  else
282  {
283  errno = PGTYPES_TS_BAD_TIMESTAMP;
284  return NULL;
285  }
286  return pgtypes_strdup(buf);
287 }
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4342
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1586
int DateStyle
Definition: globals.c:124
static char * buf
Definition: pg_test_fsync.c:73

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

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