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

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 
)

Definition at line 863 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().

864 {
865  if (TIMESTAMP_NOT_FINITE(*tin))
866  *tout = *tin;
867 
868 
869  else
870  {
871  if (span->month != 0)
872  {
873  struct tm tt,
874  *tm = &tt;
875  fsec_t fsec;
876 
877 
878  if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) != 0)
879  return -1;
880  tm->tm_mon += span->month;
881  if (tm->tm_mon > MONTHS_PER_YEAR)
882  {
883  tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
884  tm->tm_mon = (tm->tm_mon - 1) % MONTHS_PER_YEAR + 1;
885  }
886  else if (tm->tm_mon < 1)
887  {
888  tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
889  tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
890  }
891 
892 
893  /* adjust for end of month boundary problems... */
894  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
895  tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
896 
897 
898  if (tm2timestamp(tm, fsec, NULL, tin) != 0)
899  return -1;
900  }
901 
902 
903  *tin += span->time;
904  *tout = *tin;
905  }
906  return 0;
907 
908 }
#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:1777
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:60
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1873

◆ PGTYPEStimestamp_current()

void PGTYPEStimestamp_current ( timestamp )

Definition at line 294 of file timestamp.c.

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

Referenced by dtcurrent(), and main().

295 {
296  struct tm tm;
297 
299  if (errno == 0)
300  tm2timestamp(&tm, 0, NULL, ts);
301  return;
302 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:348
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:1873

◆ PGTYPEStimestamp_defmt_asc()

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

Definition at line 811 of file timestamp.c.

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

Referenced by dtcvfmtasc(), and main().

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

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

Referenced by dttofmtasc(), and main().

784 {
785  struct tm tm;
786  fsec_t fsec;
787  date dDate;
788  int dow;
789 
790  dDate = PGTYPESdate_from_timestamp(*ts);
791  dow = PGTYPESdate_dayofweek(dDate);
792  timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
793 
794  return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);
795 }
static int dttofmtasc_replace(timestamp *ts, date dDate, int dow, struct tm *tm, char *output, int *pstr_len, const char *fmtstr)
Definition: timestamp.c:305
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:1777
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 206 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().

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

◆ PGTYPEStimestamp_sub()

int PGTYPEStimestamp_sub ( timestamp ,
timestamp ,
interval  
)

Definition at line 798 of file timestamp.c.

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

Referenced by dtsub().

799 {
800  if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))
802  else
803  iv->time = (*ts1 - *ts2);
804 
805  iv->month = 0;
806 
807  return 0;
808 }
#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 922 of file timestamp.c.

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

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

◆ PGTYPEStimestamp_to_asc()

char* PGTYPEStimestamp_to_asc ( timestamp  )

Definition at line 272 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().

273 {
274  struct tm tt,
275  *tm = &tt;
276  char buf[MAXDATELEN + 1];
277  fsec_t fsec;
278  int DateStyle = 1; /* this defaults to USE_ISO_DATES, shall we
279  * make it an option? */
280 
281  if (TIMESTAMP_NOT_FINITE(tstamp))
282  EncodeSpecialTimestamp(tstamp, buf);
283  else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
284  EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
285  else
286  {
287  errno = PGTYPES_TS_BAD_TIMESTAMP;
288  return NULL;
289  }
290  return pgtypes_strdup(buf);
291 }
#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:1777
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1522
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:68
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3973
int32 fsec_t
Definition: timestamp.h:41
int DateStyle
Definition: globals.c:115
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15