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 862 of file timestamp.c.

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

295 {
296  struct tm tm;
297 
299  if (errno == 0)
300  tm2timestamp(&tm, 0, NULL, ts);
301 }
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 810 of file timestamp.c.

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 }
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:2519
#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 generate_unaccent_rules::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 782 of file timestamp.c.

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 }
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:304
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 206 of file timestamp.c.

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 }
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:2190
#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(), generate_unaccent_rules::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 797 of file timestamp.c.

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

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 917 of file timestamp.c.

918 {
919  interval tspan;
920 
921  tspan.month = -span->month;
922  tspan.time = -span->time;
923 
924  return PGTYPEStimestamp_add_interval(tin, &tspan, tout);
925 }
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:862

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

◆ PGTYPEStimestamp_to_asc()

char* PGTYPEStimestamp_to_asc ( timestamp  tstamp)

Definition at line 272 of file timestamp.c.

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 }
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4331
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1596
int DateStyle
Definition: globals.c:122
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().