aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/timestamp.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2005-06-29 22:51:57 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2005-06-29 22:51:57 +0000
commitb5f7cff84f57a189ed5c9dd59efe8d2568649d0d (patch)
tree77b5a25a7c4a62145ba89a578018121b3246b82d /src/backend/utils/adt/timestamp.c
parentc33d575899593a46a5b9a76e4e0ef6f9d81e55dd (diff)
downloadpostgresql-b5f7cff84f57a189ed5c9dd59efe8d2568649d0d.tar.gz
postgresql-b5f7cff84f57a189ed5c9dd59efe8d2568649d0d.zip
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.
Diffstat (limited to 'src/backend/utils/adt/timestamp.c')
-rw-r--r--src/backend/utils/adt/timestamp.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 035a422bfcc..f9f4ec24b1a 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -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