From 56a15c33dc33f2b88c488d2c08d84b435db7d536 Mon Sep 17 00:00:00 2001 From: dimitr Date: Wed, 3 Nov 2004 17:28:05 +0000 Subject: [PATCH] Improved TimeStamp slightly to suit non-engine needs. --- src/common/classes/timestamp.cpp | 145 +++++++++++++++++++++++++++++++ src/common/classes/timestamp.h | 83 ++---------------- src/jrd/req.h | 3 +- 3 files changed, 152 insertions(+), 79 deletions(-) create mode 100644 src/common/classes/timestamp.cpp diff --git a/src/common/classes/timestamp.cpp b/src/common/classes/timestamp.cpp new file mode 100644 index 0000000000..eadcc5be72 --- /dev/null +++ b/src/common/classes/timestamp.cpp @@ -0,0 +1,145 @@ +/* + * PROGRAM: Client/Server Common Code + * MODULE: timestamp.cpp + * DESCRIPTION: Date/time handling class + * + * The contents of this file are subject to the Initial + * Developer's Public License Version 1.0 (the "License"); + * you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl. + * + * Software distributed under the License is distributed AS IS, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. + * See the License for the specific language governing rights + * and limitations under the License. + * + * The Original Code was created by Dmitry Yemanov + * for the Firebird Open Source RDBMS project. + * + * Copyright (c) 2004 Dmitry Yemanov + * and all contributors signed below. + * + * All Rights Reserved. + * Contributor(s): ______________________________________. + */ + +#include "firebird.h" +#include "../jrd/common.h" +#include "../jrd/dsc.h" +#include "../jrd/gdsassert.h" + +#if defined(TIME_WITH_SYS_TIME) +#include +#include +#else +#if defined(HAVE_SYS_TIME_H) +#include +#else +#include +#endif +#endif + +#ifdef HAVE_SYS_TIMES_H +#include +#endif +#ifdef HAVE_SYS_TIMEB_H +#include +#endif + +#include "../common/classes/timestamp.h" + +namespace Firebird { + +TimeStamp::TimeStamp(bool empty) +{ + if (empty) + { + invalidate(); + } + else + { + generate(); + } +} + +void TimeStamp::invalidate() +{ + seconds = fractions = 0; +} + +void TimeStamp::validate() +{ + if (!seconds) + { + generate(); + } +} + +bool TimeStamp::encode(tm* times) const +{ + fb_assert(seconds > 0); + const tm* t = localtime(&seconds); + if (!t) + { + return false; + } + *times = *t; + return true; +} + +bool TimeStamp::encode(ISC_TIMESTAMP* ts, bool precise) const +{ + tm times; + if (!encode(×)) + { + return false; + } + + const int day = times.tm_mday; + int month = times.tm_mon + 1; + int year = times.tm_year + 1900; + + if (month > 2) + { + month -= 3; + } + else + { + month += 9; + year -= 1; + } + + const int c = year / 100; + const int ya = year - 100 * c; + + ts->timestamp_date = + (((SINT64) 146097 * c) / 4 + (1461 * ya) / 4 + + (153 * month + 2) / 5 + day + 1721119 - 2400001); + + ts->timestamp_time = + ((times.tm_hour * 60 + times.tm_min) * 60 + + times.tm_sec) * ISC_TIME_SECONDS_PRECISION + + (precise ? fractions * 10 : 0); + + return true; +} + +void TimeStamp::generate() +{ + // Only millisecond precision is supported currently +#ifdef HAVE_GETTIMEOFDAY + struct timeval tp; + GETTIMEOFDAY(&tp); + seconds = tp.tv_sec; + fractions = tp.tv_usec / 1000; +#else + struct timeb time_buffer; + ftime(&time_buffer); + seconds = time_buffer.time; + fractions = time_buffer.millitm; +#endif + fb_assert(seconds > 0); +} + +} // namespace diff --git a/src/common/classes/timestamp.h b/src/common/classes/timestamp.h index dbc81a2c72..ec9a3af9c2 100644 --- a/src/common/classes/timestamp.h +++ b/src/common/classes/timestamp.h @@ -27,97 +27,26 @@ #ifndef CLASSES_TIMESTAMP_H #define CLASSES_TIMESTAMP_H -#if defined(TIME_WITH_SYS_TIME) -#include -#include -#else -#if defined(HAVE_SYS_TIME_H) -#include -#else -#include -#endif -#endif - -#ifdef HAVE_SYS_TIMES_H -#include -#endif -#ifdef HAVE_SYS_TIMEB_H -#include -#endif - namespace Firebird { class TimeStamp { public: - TimeStamp() - { - generate(); - } + TimeStamp(bool = false); - void invalidate() - { - seconds = fractions = 0; - } + void invalidate(); + void validate(); - void validate() - { - if (!seconds) - { - generate(); - } - } - - bool encode(tm* times) const - { - fb_assert(seconds > 0); - const tm* t = localtime(&seconds); - if (!t) - { - return false; - } - *times = *t; - return true; - } - - bool encode(ISC_TIMESTAMP* ts, bool precise) const - { - tm times; - if (!encode(×)) - { - return false; - } - isc_encode_timestamp(×, ts); - if (precise) - { - ts->timestamp_time += fractions * 10; - } - return true; - } + bool encode(tm* times) const; + bool encode(ISC_TIMESTAMP* ts, bool precise) const; private: time_t seconds; time_t fractions; - void generate() - { - // Only millisecond precision is supported currently -#ifdef HAVE_GETTIMEOFDAY - struct timeval tp; - GETTIMEOFDAY(&tp); - seconds = tp.tv_sec; - fractions = tp.tv_usec / 1000; -#else - struct timeb time_buffer; - ftime(&time_buffer); - seconds = time_buffer.time; - fractions = time_buffer.millitm; -#endif - fb_assert(seconds > 0); - } - + void generate(); }; } // namespace Firebird diff --git a/src/jrd/req.h b/src/jrd/req.h index 3189b00d4d..c14bee3649 100644 --- a/src/jrd/req.h +++ b/src/jrd/req.h @@ -153,8 +153,7 @@ class jrd_req : public pool_alloc_rpt public: jrd_req(JrdMemoryPool* pool) : req_blobs(pool), req_external(*pool), req_access(*pool), req_resources(*pool), - req_fors(*pool), req_invariants(*pool) - { req_timestamp.invalidate(); }; + req_fors(*pool), req_invariants(*pool), req_timestamp(true) {} Attachment* req_attachment; // database attachment USHORT req_count; // number of streams