From 8d71db462e33d3cac5549e12eddf1d699c439516 Mon Sep 17 00:00:00 2001 From: skidder Date: Sun, 14 Nov 2004 20:49:47 +0000 Subject: [PATCH] Add time routines to TimeStamp class --- src/common/classes/timestamp.cpp | 34 ++++++++++++++++++++++++++++++++ src/common/classes/timestamp.h | 5 ++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/common/classes/timestamp.cpp b/src/common/classes/timestamp.cpp index d16b75e5e6..5f72565a32 100644 --- a/src/common/classes/timestamp.cpp +++ b/src/common/classes/timestamp.cpp @@ -176,6 +176,40 @@ ISC_DATE TimeStamp::encode_date(const struct tm* times) (153 * month + 2) / 5 + day + 1721119 - 2400001); } +void TimeStamp::decode_time( + ISC_TIME ntime, int* hours, int* minutes, int* seconds, int* fractions) +{ + *hours = ntime / (3600 * ISC_TIME_SECONDS_PRECISION); + ntime %= 3600 * ISC_TIME_SECONDS_PRECISION; + *minutes = ntime / (60 * ISC_TIME_SECONDS_PRECISION); + ntime %= 60 * ISC_TIME_SECONDS_PRECISION; + *seconds = ntime / ISC_TIME_SECONDS_PRECISION; + *fractions = ntime % ISC_TIME_SECONDS_PRECISION; +} + +ISC_TIME TimeStamp::round_time(ISC_TIME ntime, int precision) +{ + int scale = -ISC_TIME_SECONDS_PRECISION_SCALE - precision; + + // for the moment, if greater precision was requested than we can + // provide return what we have. + if (scale < 0) return ntime; + + static ISC_TIME pow10table[] = + {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; + + fb_assert(scale < FB_NELEM(pow10table)); + + ISC_TIME period = pow10table[scale]; + + return ntime - (ntime % period); +} + +ISC_TIME TimeStamp::encode_time(int hours, int minutes, int seconds, int fractions) +{ + return ((hours * 60 + minutes) * 60 + seconds) * ISC_TIME_SECONDS_PRECISION; +} + // Encode timestamp from UNIX datetime structure void TimeStamp::encode(const struct tm* times) { mValue.timestamp_date = encode_date(times); diff --git a/src/common/classes/timestamp.h b/src/common/classes/timestamp.h index 9328795a57..9e9946a2b7 100644 --- a/src/common/classes/timestamp.h +++ b/src/common/classes/timestamp.h @@ -98,9 +98,12 @@ public: // Read access to timestamp structure we wrap const ISC_TIMESTAMP& value() const { return mValue; } - // ISC date/time helper routines. Both functions are signal-safe. + // ISC date/time helper routines. These functions are signal-safe. static void decode_date(ISC_DATE nday, struct tm* times); static ISC_DATE encode_date(const struct tm* times); + static void decode_time(ISC_TIME ntime, int* hours, int* minutes, int* seconds, int* fractions); + static ISC_TIME encode_time(int hours, int minutes, int seconds, int fractions); + static ISC_TIME round_time(ISC_TIME ntime, int precision); private: ISC_TIMESTAMP mValue;