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 *, char **)
 
char * PGTYPEStimestamp_to_asc (timestamp)
 
int PGTYPEStimestamp_sub (timestamp *, timestamp *, interval *)
 
int PGTYPEStimestamp_fmt_asc (timestamp *, char *, int, const char *)
 
void PGTYPEStimestamp_current (timestamp *)
 
int PGTYPEStimestamp_defmt_asc (const char *, const char *, timestamp *)
 
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 862 of file timestamp.c.

References day_tab, isleap, interval::month, MONTHS_PER_YEAR, interval::time, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), and pg_tm::tm_mon.

Referenced by main(), and PGTYPEStimestamp_sub_interval().

863 {
864  if (TIMESTAMP_NOT_FINITE(*tin))
865  *tout = *tin;
866 
867 
868  else
869  {
870  if (span->month != 0)
871  {
872  struct tm tt,
873  *tm = &tt;
874  fsec_t fsec;
875 
876 
877  if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) != 0)
878  return -1;
879  tm->tm_mon += span->month;
880  if (tm->tm_mon > MONTHS_PER_YEAR)
881  {
882  tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
883  tm->tm_mon = (tm->tm_mon - 1) % MONTHS_PER_YEAR + 1;
884  }
885  else if (tm->tm_mon < 1)
886  {
887  tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
888  tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
889  }
890 
891 
892  /* adjust for end of month boundary problems... */
893  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
894  tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
895 
896 
897  if (tm2timestamp(tm, fsec, NULL, tin) != 0)
898  return -1;
899  }
900 
901 
902  *tin += span->time;
903  *tout = *tin;
904  }
905  return 0;
906 
907 }
#define isleap(y)
Definition: datetime.h:271
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1791
static struct pg_tm tm
Definition: localtime.c:108
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
const int day_tab[2][13]
Definition: datetime.c:59
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1887

◆ PGTYPEStimestamp_current()

void PGTYPEStimestamp_current ( timestamp )

Definition at line 293 of file timestamp.c.

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

Referenced by dtcurrent(), and main().

294 {
295  struct tm tm;
296 
298  if (errno == 0)
299  tm2timestamp(&tm, 0, NULL, ts);
300  return;
301 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:347
static struct pg_tm tm
Definition: localtime.c:108
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1887

◆ PGTYPEStimestamp_defmt_asc()

int PGTYPEStimestamp_defmt_asc ( const char *  ,
const char *  ,
timestamp  
)

Definition at line 810 of file timestamp.c.

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

Referenced by dtcvfmtasc(), and main().

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

◆ PGTYPEStimestamp_fmt_asc()

int PGTYPEStimestamp_fmt_asc ( timestamp ,
char *  ,
int  ,
const char *   
)

Definition at line 782 of file timestamp.c.

References dttofmtasc_replace(), PGTYPESdate_dayofweek(), PGTYPESdate_from_timestamp(), timestamp2tm(), and tm.

Referenced by dttofmtasc(), and main().

783 {
784  struct tm tm;
785  fsec_t fsec;
786  date dDate;
787  int dow;
788 
789  dDate = PGTYPESdate_from_timestamp(*ts);
790  dow = PGTYPESdate_dayofweek(dDate);
791  timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
792 
793  return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);
794 }
static int dttofmtasc_replace(timestamp *ts, date dDate, int dow, struct tm *tm, char *output, int *pstr_len, const char *fmtstr)
Definition: timestamp.c:304
static void output(uint64 loop_count)
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1791
long date
Definition: pgtypes_date.h:9
static struct pg_tm tm
Definition: localtime.c:108
int PGTYPESdate_dayofweek(date)
Definition: datetime.c:138
int32 fsec_t
Definition: timestamp.h:41
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition: snprintf.c:969
date PGTYPESdate_from_timestamp(timestamp)
Definition: datetime.c:31

◆ PGTYPEStimestamp_from_asc()

timestamp PGTYPEStimestamp_from_asc ( char *  ,
char **   
)

Definition at line 205 of file timestamp.c.

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

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

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

◆ PGTYPEStimestamp_sub()

int PGTYPEStimestamp_sub ( timestamp ,
timestamp ,
interval  
)

Definition at line 797 of file timestamp.c.

References interval::month, PGTYPES_TS_ERR_EINFTIME, interval::time, and TIMESTAMP_NOT_FINITE.

Referenced by dtsub().

798 {
799  if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))
801  else
802  iv->time = (*ts1 - *ts2);
803 
804  iv->month = 0;
805 
806  return 0;
807 }
#define PGTYPES_TS_ERR_EINFTIME
Definition: pgtypes_error.h:16
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122

◆ PGTYPEStimestamp_sub_interval()

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

Definition at line 921 of file timestamp.c.

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

922 {
923  interval tspan;
924 
925  tspan.month = -span->month;
926  tspan.time = -span->time;
927 
928 
929  return PGTYPEStimestamp_add_interval(tin, &tspan, tout);
930 }
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:862

◆ PGTYPEStimestamp_to_asc()

char* PGTYPEStimestamp_to_asc ( timestamp  )

Definition at line 271 of file timestamp.c.

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().

272 {
273  struct tm tt,
274  *tm = &tt;
275  char buf[MAXDATELEN + 1];
276  fsec_t fsec;
277  int DateStyle = 1; /* this defaults to USE_ISO_DATES, shall we
278  * make it an option? */
279 
280  if (TIMESTAMP_NOT_FINITE(tstamp))
281  EncodeSpecialTimestamp(tstamp, buf);
282  else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
283  EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
284  else
285  {
286  errno = PGTYPES_TS_BAD_TIMESTAMP;
287  return NULL;
288  }
289  return pgtypes_strdup(buf);
290 }
#define MAXDATELEN
Definition: datetime.h:201
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1791
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1536
static struct pg_tm tm
Definition: localtime.c:108
char * pgtypes_strdup(const char *str)
Definition: common.c:20
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static char * buf
Definition: pg_test_fsync.c:67
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3972
int32 fsec_t
Definition: timestamp.h:41
int DateStyle
Definition: globals.c:115
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15