PostgreSQL Source Code  git master
pgtypes_timestamp.h File Reference
#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 9 of file pgtypes_timestamp.h.

◆ TimestampTz

Definition at line 10 of file pgtypes_timestamp.h.

Function Documentation

◆ PGTYPEStimestamp_add_interval()

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

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

869 {
870  if (TIMESTAMP_NOT_FINITE(*tin))
871  *tout = *tin;
872 
873 
874  else
875  {
876  if (span->month != 0)
877  {
878  struct tm tt,
879  *tm = &tt;
880  fsec_t fsec;
881 
882 
883  if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) != 0)
884  return -1;
885  tm->tm_mon += span->month;
886  if (tm->tm_mon > MONTHS_PER_YEAR)
887  {
888  tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
889  tm->tm_mon = (tm->tm_mon - 1) % MONTHS_PER_YEAR + 1;
890  }
891  else if (tm->tm_mon < 1)
892  {
893  tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
894  tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
895  }
896 
897 
898  /* adjust for end of month boundary problems... */
899  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
900  tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
901 
902 
903  if (tm2timestamp(tm, fsec, NULL, tin) != 0)
904  return -1;
905  }
906 
907 
908  *tin += span->time;
909  *tout = *tin;
910  }
911  return 0;
912 
913 }
#define isleap(y)
Definition: datetime.h:273
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
static struct pg_tm tm
Definition: localtime.c:107
#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:1854

◆ PGTYPEStimestamp_current()

void PGTYPEStimestamp_current ( timestamp )

Definition at line 299 of file timestamp.c.

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

Referenced by dtcurrent(), and main().

300 {
301  struct tm tm;
302 
304  if (errno == 0)
305  tm2timestamp(&tm, 0, NULL, ts);
306  return;
307 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:370
static struct pg_tm tm
Definition: localtime.c:107
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1854

◆ PGTYPEStimestamp_defmt_asc()

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

Definition at line 816 of file timestamp.c.

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

Referenced by dtcvfmtasc(), and main().

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

◆ PGTYPEStimestamp_fmt_asc()

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

Definition at line 788 of file timestamp.c.

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

Referenced by dttofmtasc(), and main().

789 {
790  struct tm tm;
791  fsec_t fsec;
792  date dDate;
793  int dow;
794 
795  dDate = PGTYPESdate_from_timestamp(*ts);
796  dow = PGTYPESdate_dayofweek(dDate);
797  timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
798 
799  return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);
800 }
static int dttofmtasc_replace(timestamp *ts, date dDate, int dow, struct tm *tm, char *output, int *pstr_len, const char *fmtstr)
Definition: timestamp.c:310
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:1758
long date
Definition: pgtypes_date.h:8
static struct pg_tm tm
Definition: localtime.c:107
int PGTYPESdate_dayofweek(date)
Definition: datetime.c:139
int32 fsec_t
Definition: timestamp.h:41
static void fmtstr(char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition: snprintf.c:794
date PGTYPESdate_from_timestamp(timestamp)
Definition: datetime.c:32

◆ PGTYPEStimestamp_from_asc()

timestamp PGTYPEStimestamp_from_asc ( char *  ,
char **   
)

Definition at line 207 of file timestamp.c.

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

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

◆ PGTYPEStimestamp_sub()

int PGTYPEStimestamp_sub ( timestamp ,
timestamp ,
interval  
)

Definition at line 803 of file timestamp.c.

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

Referenced by dtsub().

804 {
805  if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))
807  else
808  iv->time = (*ts1 - *ts2);
809 
810  iv->month = 0;
811 
812  return 0;
813 }
#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 927 of file timestamp.c.

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

928 {
929  interval tspan;
930 
931  tspan.month = -span->month;
932  tspan.time = -span->time;
933 
934 
935  return PGTYPEStimestamp_add_interval(tin, &tspan, tout);
936 }
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:868

◆ PGTYPEStimestamp_to_asc()

char* PGTYPEStimestamp_to_asc ( timestamp  )

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

278 {
279  struct tm tt,
280  *tm = &tt;
281  char buf[MAXDATELEN + 1];
282  fsec_t fsec;
283  int DateStyle = 1; /* this defaults to ISO_DATES, shall we make
284  * it an option? */
285 
286  if (TIMESTAMP_NOT_FINITE(tstamp))
287  EncodeSpecialTimestamp(tstamp, buf);
288  else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
289  EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
290  else
291  {
292  errno = PGTYPES_TS_BAD_TIMESTAMP;
293  return NULL;
294  }
295  return pgtypes_strdup(buf);
296 }
#define MAXDATELEN
Definition: datetime.h:203
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1523
static struct pg_tm tm
Definition: localtime.c:107
char * pgtypes_strdup(const char *str)
Definition: common.c:19
#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:3999
int32 fsec_t
Definition: timestamp.h:41
int DateStyle
Definition: globals.c:107
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15