mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Clean up the rather historically encumbered interface to now() and
current time: provide a GetCurrentTimestamp() function that returns current time in the form of a TimestampTz, instead of separate time_t and microseconds fields. This is what all the callers really want anyway, and it eliminates low-level dependencies on AbsoluteTime, which is a deprecated datatype that will have to disappear eventually.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.150 2005/05/27 21:31:23 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.151 2005/06/29 22:51:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "access/xact.h"
|
||||
#include "miscadmin.h"
|
||||
#include "utils/datetime.h"
|
||||
#include "utils/guc.h"
|
||||
@@ -674,6 +675,41 @@ j2day(int date)
|
||||
} /* j2day() */
|
||||
|
||||
|
||||
/*
|
||||
* GetCurrentDateTime()
|
||||
*
|
||||
* Get the transaction start time ("now()") broken down as a struct pg_tm.
|
||||
*/
|
||||
void
|
||||
GetCurrentDateTime(struct pg_tm * tm)
|
||||
{
|
||||
int tz;
|
||||
fsec_t fsec;
|
||||
|
||||
timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, &fsec,
|
||||
NULL, NULL);
|
||||
/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
|
||||
}
|
||||
|
||||
/*
|
||||
* GetCurrentTimeUsec()
|
||||
*
|
||||
* Get the transaction start time ("now()") broken down as a struct pg_tm,
|
||||
* including fractional seconds and timezone offset.
|
||||
*/
|
||||
void
|
||||
GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp)
|
||||
{
|
||||
int tz;
|
||||
|
||||
timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, fsec,
|
||||
NULL, NULL);
|
||||
/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
|
||||
if (tzp != NULL)
|
||||
*tzp = tz;
|
||||
}
|
||||
|
||||
|
||||
/* TrimTrailingZeros()
|
||||
* ... resulting from printing numbers with full precision.
|
||||
*/
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.133 2005/06/15 00:34:08 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.134 2005/06/29 22:51:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -27,7 +27,7 @@
|
||||
#include "miscadmin.h"
|
||||
#include "pgtime.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/timestamp.h"
|
||||
#include "utils/nabstime.h"
|
||||
|
||||
#define MIN_DAYNUM -24856 /* December 13, 1901 */
|
||||
#define MAX_DAYNUM 24854 /* January 18, 2038 */
|
||||
@@ -99,84 +99,6 @@ GetCurrentAbsoluteTime(void)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* GetCurrentAbsoluteTimeUsec()
|
||||
*
|
||||
* Get the current system time (relative to Unix epoch), including fractional
|
||||
* seconds expressed as microseconds.
|
||||
*/
|
||||
AbsoluteTime
|
||||
GetCurrentAbsoluteTimeUsec(int *usec)
|
||||
{
|
||||
time_t now;
|
||||
struct timeval tp;
|
||||
|
||||
gettimeofday(&tp, NULL);
|
||||
now = tp.tv_sec;
|
||||
*usec = tp.tv_usec;
|
||||
return (AbsoluteTime) now;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* AbsoluteTimeUsecToTimestampTz()
|
||||
*
|
||||
* Convert system time including microseconds to TimestampTz representation.
|
||||
*/
|
||||
TimestampTz
|
||||
AbsoluteTimeUsecToTimestampTz(AbsoluteTime sec, int usec)
|
||||
{
|
||||
TimestampTz result;
|
||||
|
||||
#ifdef HAVE_INT64_TIMESTAMP
|
||||
result = ((sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY))
|
||||
* USECS_PER_SEC) + usec;
|
||||
#else
|
||||
result = sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY)
|
||||
+ (usec / 1000000.0);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* GetCurrentDateTime()
|
||||
*
|
||||
* Get the transaction start time ("now()") broken down as a struct pg_tm.
|
||||
*/
|
||||
void
|
||||
GetCurrentDateTime(struct pg_tm * tm)
|
||||
{
|
||||
int tz;
|
||||
|
||||
abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* GetCurrentTimeUsec()
|
||||
*
|
||||
* Get the transaction start time ("now()") broken down as a struct pg_tm,
|
||||
* including fractional seconds and timezone offset.
|
||||
*/
|
||||
void
|
||||
GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp)
|
||||
{
|
||||
int tz;
|
||||
int usec;
|
||||
|
||||
abstime2tm(GetCurrentTransactionStartTimeUsec(&usec), &tz, tm, NULL);
|
||||
/* Note: don't pass NULL tzp to abstime2tm; affects behavior */
|
||||
if (tzp != NULL)
|
||||
*tzp = tz;
|
||||
#ifdef HAVE_INT64_TIMESTAMP
|
||||
*fsec = usec;
|
||||
#else
|
||||
*fsec = usec / 1000000.0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
abstime2tm(AbsoluteTime _time, int *tzp, struct pg_tm * tm, char **tzn)
|
||||
{
|
||||
@@ -458,15 +380,6 @@ abstime_cmp_internal(AbsoluteTime a, AbsoluteTime b)
|
||||
if (b == INVALID_ABSTIME)
|
||||
return -1; /* non-INVALID < INVALID */
|
||||
|
||||
#if 0
|
||||
/* CURRENT is no longer stored internally... */
|
||||
/* XXX this is broken, should go away: */
|
||||
if (a == CURRENT_ABSTIME)
|
||||
a = GetCurrentTransactionStartTime();
|
||||
if (b == CURRENT_ABSTIME)
|
||||
b = GetCurrentTransactionStartTime();
|
||||
#endif
|
||||
|
||||
if (a > b)
|
||||
return 1;
|
||||
else if (a == b)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.23 2005/06/28 05:09:00 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.24 2005/06/29 22:51:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -341,13 +341,9 @@ pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PgStat_StatBeEntry *beentry;
|
||||
int32 beid;
|
||||
AbsoluteTime sec;
|
||||
int usec;
|
||||
int32 beid = PG_GETARG_INT32(0);
|
||||
TimestampTz result;
|
||||
|
||||
beid = PG_GETARG_INT32(0);
|
||||
PgStat_StatBeEntry *beentry;
|
||||
|
||||
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
||||
PG_RETURN_NULL();
|
||||
@@ -355,31 +351,24 @@ pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
|
||||
if (!superuser() && beentry->userid != GetUserId())
|
||||
PG_RETURN_NULL();
|
||||
|
||||
sec = beentry->activity_start_sec;
|
||||
usec = beentry->activity_start_usec;
|
||||
result = beentry->activity_start_timestamp;
|
||||
|
||||
/*
|
||||
* No time recorded for start of current query -- this is the case if
|
||||
* the user hasn't enabled query-level stats collection.
|
||||
*/
|
||||
if (sec == 0 && usec == 0)
|
||||
if (result == 0)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
|
||||
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
Datum
|
||||
pg_stat_get_backend_start(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PgStat_StatBeEntry *beentry;
|
||||
int32 beid;
|
||||
AbsoluteTime sec;
|
||||
int usec;
|
||||
int32 beid = PG_GETARG_INT32(0);
|
||||
TimestampTz result;
|
||||
|
||||
beid = PG_GETARG_INT32(0);
|
||||
PgStat_StatBeEntry *beentry;
|
||||
|
||||
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
||||
PG_RETURN_NULL();
|
||||
@@ -387,14 +376,11 @@ pg_stat_get_backend_start(PG_FUNCTION_ARGS)
|
||||
if (!superuser() && beentry->userid != GetUserId())
|
||||
PG_RETURN_NULL();
|
||||
|
||||
sec = beentry->start_sec;
|
||||
usec = beentry->start_usec;
|
||||
result = beentry->start_timestamp;
|
||||
|
||||
if (sec == 0 && usec == 0)
|
||||
if (result == 0) /* probably can't happen? */
|
||||
PG_RETURN_NULL();
|
||||
|
||||
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
|
||||
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.182 2005/06/13 23:14:48 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.183 2005/06/29 22:51:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -105,6 +105,7 @@
|
||||
#include "utils/datum.h"
|
||||
#include "utils/int8.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/nabstime.h"
|
||||
#include "utils/pg_locale.h"
|
||||
#include "utils/selfuncs.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.126 2005/06/15 00:34:09 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.127 2005/06/29 22:51:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -28,6 +28,8 @@
|
||||
#include "parser/scansup.h"
|
||||
#include "utils/array.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/datetime.h"
|
||||
|
||||
|
||||
/*
|
||||
* gcc's -ffast-math switch breaks routines that expect exact results from
|
||||
@@ -38,6 +40,10 @@
|
||||
#endif
|
||||
|
||||
|
||||
/* Set at postmaster start */
|
||||
TimestampTz PgStartTime;
|
||||
|
||||
|
||||
#ifdef HAVE_INT64_TIMESTAMP
|
||||
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec);
|
||||
|
||||
@@ -927,21 +933,39 @@ EncodeSpecialTimestamp(Timestamp dt, char *str)
|
||||
Datum
|
||||
now(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TimestampTz result;
|
||||
AbsoluteTime sec;
|
||||
int usec;
|
||||
|
||||
sec = GetCurrentTransactionStartTimeUsec(&usec);
|
||||
|
||||
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
|
||||
|
||||
PG_RETURN_TIMESTAMPTZ(result);
|
||||
PG_RETURN_TIMESTAMPTZ(GetCurrentTransactionStartTimestamp());
|
||||
}
|
||||
|
||||
Datum
|
||||
pgsql_postmaster_start_time(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_TIMESTAMPTZ(StartTime);
|
||||
PG_RETURN_TIMESTAMPTZ(PgStartTime);
|
||||
}
|
||||
|
||||
/*
|
||||
* GetCurrentTimestamp -- get the current operating system time
|
||||
*
|
||||
* Result is in the form of a TimestampTz value, and is expressed to the
|
||||
* full precision of the gettimeofday() syscall
|
||||
*/
|
||||
TimestampTz
|
||||
GetCurrentTimestamp(void)
|
||||
{
|
||||
TimestampTz result;
|
||||
struct timeval tp;
|
||||
|
||||
gettimeofday(&tp, NULL);
|
||||
|
||||
result = tp.tv_sec -
|
||||
((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
|
||||
|
||||
#ifdef HAVE_INT64_TIMESTAMP
|
||||
result = (result * USECS_PER_SEC) + tp.tv_usec;
|
||||
#else
|
||||
result = result + (tp.tv_usec / 1000000.0);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user