PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 (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

Definition at line 9 of file pgtypes_timestamp.h.

Definition at line 10 of file pgtypes_timestamp.h.

Function Documentation

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

Definition at line 870 of file timestamp.c.

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

Referenced by main(), and PGTYPEStimestamp_sub_interval().

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

Definition at line 301 of file timestamp.c.

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

Referenced by dtcurrent(), and main().

302 {
303  struct tm tm;
304 
306  if (errno == 0)
307  tm2timestamp(&tm, 0, NULL, ts);
308  return;
309 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:370
static struct pg_tm tm
Definition: localtime.c:111
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1854
#define NULL
Definition: c.h:229
int PGTYPEStimestamp_defmt_asc ( char *  ,
const char *  ,
timestamp  
)

Definition at line 818 of file timestamp.c.

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

Referenced by dtcvfmtasc(), and main().

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

Definition at line 790 of file timestamp.c.

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

Referenced by dttofmtasc(), and main().

791 {
792  struct tm tm;
793  fsec_t fsec;
794  date dDate;
795  int dow;
796 
797  dDate = PGTYPESdate_from_timestamp(*ts);
798  dow = PGTYPESdate_dayofweek(dDate);
799  timestamp2tm(*ts, NULL, &tm, &fsec, NULL);
800 
801  return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);
802 }
static int dttofmtasc_replace(timestamp *ts, date dDate, int dow, struct tm *tm, char *output, int *pstr_len, const char *fmtstr)
Definition: timestamp.c:312
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:111
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:804
#define NULL
Definition: c.h:229
date PGTYPESdate_from_timestamp(timestamp)
Definition: datetime.c:32
timestamp PGTYPEStimestamp_from_asc ( char *  ,
char **   
)

Definition at line 209 of file timestamp.c.

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

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

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

Definition at line 805 of file timestamp.c.

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

Referenced by dtsub().

806 {
807  if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))
809  else
810  iv->time = (*ts1 - *ts2);
811 
812  iv->month = 0;
813 
814  return 0;
815 }
#define PGTYPES_TS_ERR_EINFTIME
Definition: pgtypes_error.h:16
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
int PGTYPEStimestamp_sub_interval ( timestamp tin,
interval span,
timestamp tout 
)

Definition at line 929 of file timestamp.c.

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

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

Definition at line 279 of file timestamp.c.

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

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

280 {
281  struct tm tt,
282  *tm = &tt;
283  char buf[MAXDATELEN + 1];
284  fsec_t fsec;
285  int DateStyle = 1; /* this defaults to ISO_DATES, shall we make
286  * it an option? */
287 
288  if (TIMESTAMP_NOT_FINITE(tstamp))
289  EncodeSpecialTimestamp(tstamp, buf);
290  else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
291  EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
292  else
293  {
294  errno = PGTYPES_TS_BAD_TIMESTAMP;
295  return NULL;
296  }
297  return pgtypes_strdup(buf);
298 }
#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:111
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:66
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
#define NULL
Definition: c.h:229
int DateStyle
Definition: globals.c:107
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15