8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 00:43:02 +01:00

Improved TimeStamp slightly to suit non-engine needs.

This commit is contained in:
dimitr 2004-11-03 17:28:05 +00:00
parent 7495c1911b
commit 56a15c33dc
3 changed files with 152 additions and 79 deletions

View File

@ -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 <dimitr@users.sourceforge.net>
* 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 <sys/time.h>
#include <time.h>
#else
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#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(&times))
{
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

View File

@ -27,97 +27,26 @@
#ifndef CLASSES_TIMESTAMP_H
#define CLASSES_TIMESTAMP_H
#if defined(TIME_WITH_SYS_TIME)
#include <sys/time.h>
#include <time.h>
#else
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#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(&times))
{
return false;
}
isc_encode_timestamp(&times, 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

View File

@ -153,8 +153,7 @@ class jrd_req : public pool_alloc_rpt<record_param, type_req>
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