Skip to content

Commit 6bc762f

Browse files
committed
constexpr + noexcept in NoThrowTimeStamp
1 parent 4575ac8 commit 6bc762f

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

src/common/classes/NoThrowTimeStamp.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ ISC_TIMESTAMP NoThrowTimeStamp::encode_timestamp(const struct tm* times, const i
299299
return ts;
300300
}
301301

302-
void NoThrowTimeStamp::add10msec(ISC_TIMESTAMP* v, SINT64 msec, SINT64 multiplier)
302+
void NoThrowTimeStamp::add10msec(ISC_TIMESTAMP* v, SINT64 msec, SINT64 multiplier) noexcept
303303
{
304304
const SINT64 full = msec * multiplier;
305305
const int days = full / (SECONDS_PER_DAY * ISC_TIME_SECONDS_PRECISION);
@@ -321,7 +321,7 @@ void NoThrowTimeStamp::add10msec(ISC_TIMESTAMP* v, SINT64 msec, SINT64 multiplie
321321
}
322322
}
323323

324-
void NoThrowTimeStamp::round_time(ISC_TIME &ntime, const int precision)
324+
void NoThrowTimeStamp::round_time(ISC_TIME &ntime, const int precision) noexcept
325325
{
326326
const int scale = -ISC_TIME_SECONDS_PRECISION_SCALE - precision;
327327

@@ -337,7 +337,7 @@ void NoThrowTimeStamp::round_time(ISC_TIME &ntime, const int precision)
337337
ntime -= (ntime % period);
338338
}
339339

340-
int NoThrowTimeStamp::convertGregorianDateToWeekDate(const struct tm& times)
340+
int NoThrowTimeStamp::convertGregorianDateToWeekDate(const struct tm& times) noexcept
341341
{
342342
// Algorithm for Converting Gregorian Dates to ISO 8601 Week Date by Rick McCarty, 1999
343343
// http://personal.ecu.edu/mccartyr/ISOwdALG.txt
@@ -369,7 +369,7 @@ int NoThrowTimeStamp::convertGregorianDateToWeekDate(const struct tm& times)
369369
yearNumber = y;
370370

371371
// Find if y m d falls in yearNumber y+1, weekNumber 1
372-
int i = isLeapYear(y) ? 366 : 365;
372+
const int i = isLeapYear(y) ? 366 : 365;
373373

374374
if ((i - dayOfYearNumber) < (4 - weekday))
375375
{
@@ -381,7 +381,7 @@ int NoThrowTimeStamp::convertGregorianDateToWeekDate(const struct tm& times)
381381
// Find if y m d falls in yearNumber y, weekNumber 1 through 53
382382
if (yearNumber == y)
383383
{
384-
int j = dayOfYearNumber + (7 - weekday) + (jan1Weekday - 1);
384+
const int j = dayOfYearNumber + (7 - weekday) + (jan1Weekday - 1);
385385
weekNumber = j / 7;
386386
if (jan1Weekday > 4)
387387
weekNumber--;
@@ -390,35 +390,34 @@ int NoThrowTimeStamp::convertGregorianDateToWeekDate(const struct tm& times)
390390
return weekNumber;
391391
}
392392

393-
int NoThrowTimeStamp::convertGregorianDateToJulianDate(int year, int month, int day)
393+
int NoThrowTimeStamp::convertGregorianDateToJulianDate(int year, int month, int day) noexcept
394394
{
395-
int jdn = (1461 * (year + 4800 + (month - 14)/12))/4 + (367 * (month - 2 - 12 * ((month - 14)/12)))
395+
return (1461 * (year + 4800 + (month - 14)/12))/4 + (367 * (month - 2 - 12 * ((month - 14)/12)))
396396
/ 12 - (3 * ((year + 4900 + (month - 14)/12)/100))/4 + day - 32075;
397-
return jdn;
398397
}
399398

400-
void NoThrowTimeStamp::convertJulianDateToGregorianDate(int jdn, int& outYear, int& outMonth, int& outDay)
399+
void NoThrowTimeStamp::convertJulianDateToGregorianDate(int jdn, int& outYear, int& outMonth, int& outDay) noexcept
401400
{
402-
int a = jdn + 32044;
403-
int b = (4 * a +3 ) / 146097;
404-
int c = a - (146097 * b) / 4;
405-
int d = (4 * c + 3) / 1461;
406-
int e = c - (1461 * d) / 4;
407-
int m = (5 * e + 2) / 153;
401+
const int a = jdn + 32044;
402+
const int b = (4 * a +3 ) / 146097;
403+
const int c = a - (146097 * b) / 4;
404+
const int d = (4 * c + 3) / 1461;
405+
const int e = c - (1461 * d) / 4;
406+
const int m = (5 * e + 2) / 153;
408407

409408
outDay = e - (153 * m + 2) / 5 + 1;
410409
outMonth = m + 3 - 12 * (m / 10);
411410
outYear = 100 * b + d - 4800 + (m / 10);
412411
}
413412

414413
// Encode timestamp from UNIX datetime structure
415-
void NoThrowTimeStamp::encode(const struct tm* times, int fractions)
414+
void NoThrowTimeStamp::encode(const struct tm* times, int fractions) noexcept
416415
{
417416
mValue = encode_timestamp(times, fractions);
418417
}
419418

420419
// Decode timestamp into UNIX datetime structure
421-
void NoThrowTimeStamp::decode(struct tm* times, int* fractions) const
420+
void NoThrowTimeStamp::decode(struct tm* times, int* fractions) const noexcept
422421
{
423422
fb_assert(mValue.timestamp_date != BAD_DATE);
424423
fb_assert(mValue.timestamp_time != BAD_TIME);

src/common/classes/NoThrowTimeStamp.h

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ namespace Firebird {
5757
class NoThrowTimeStamp
5858
{
5959
public:
60-
static const ISC_DATE MIN_DATE = -678575; // 01.01.0001
61-
static const ISC_DATE MAX_DATE = 2973483; // 31.12.9999
62-
static const ISC_DATE UNIX_DATE = 40587; // 01.01.1970
60+
static constexpr ISC_DATE MIN_DATE = -678575; // 01.01.0001
61+
static constexpr ISC_DATE MAX_DATE = 2973483; // 31.12.9999
62+
static constexpr ISC_DATE UNIX_DATE = 40587; // 01.01.1970
6363

64-
static const SINT64 SECONDS_PER_DAY = 24 * 60 * 60;
65-
static const SINT64 ISC_TICKS_PER_DAY = SECONDS_PER_DAY * ISC_TIME_SECONDS_PRECISION;
64+
static constexpr SINT64 SECONDS_PER_DAY = 24 * 60 * 60;
65+
static constexpr SINT64 ISC_TICKS_PER_DAY = SECONDS_PER_DAY * ISC_TIME_SECONDS_PRECISION;
6666

6767
static const ISC_TIMESTAMP MIN_TIMESTAMP;
6868
static const ISC_TIMESTAMP MAX_TIMESTAMP;
6969

7070
private:
71-
static const ISC_DATE BAD_DATE = MAX_SLONG;
72-
static const ISC_TIME BAD_TIME = MAX_ULONG;
71+
static constexpr ISC_DATE BAD_DATE = MAX_SLONG;
72+
static constexpr ISC_TIME BAD_TIME = MAX_ULONG;
7373

7474
public:
7575
// Number of the first day of UNIX epoch in GDS counting
@@ -78,39 +78,39 @@ class NoThrowTimeStamp
7878
static const ISC_TIME POW_10_TABLE[];
7979

8080
// Constructors
81-
NoThrowTimeStamp()
81+
NoThrowTimeStamp() noexcept
8282
{
8383
invalidate();
8484
}
8585

86-
NoThrowTimeStamp(const ISC_TIMESTAMP& from)
86+
NoThrowTimeStamp(const ISC_TIMESTAMP& from) noexcept
8787
: mValue(from)
8888
{}
8989

90-
NoThrowTimeStamp(ISC_DATE date, ISC_TIME time)
90+
NoThrowTimeStamp(ISC_DATE date, ISC_TIME time) noexcept
9191
{
9292
mValue.timestamp_date = date;
9393
mValue.timestamp_time = time;
9494
}
9595

96-
explicit NoThrowTimeStamp(const struct tm& times, int fractions = 0)
96+
explicit NoThrowTimeStamp(const struct tm& times, int fractions = 0) noexcept
9797
{
9898
encode(&times, fractions);
9999
}
100100

101-
bool isValid() const
101+
bool isValid() const noexcept
102102
{
103103
return isValidTimeStamp(mValue);
104104
}
105105

106106
// Check if timestamp contains a non-existing value
107-
bool isEmpty() const
107+
bool isEmpty() const noexcept
108108
{
109109
return (mValue.timestamp_date == BAD_DATE && mValue.timestamp_time == BAD_TIME);
110110
}
111111

112112
// Set value of timestamp to a non-existing value
113-
void invalidate()
113+
void invalidate() noexcept
114114
{
115115
mValue.timestamp_date = BAD_DATE;
116116
mValue.timestamp_time = BAD_TIME;
@@ -126,32 +126,32 @@ class NoThrowTimeStamp
126126
}
127127

128128
// Encode timestamp from UNIX datetime structure
129-
void encode(const struct tm* times, int fractions = 0);
129+
void encode(const struct tm* times, int fractions = 0) noexcept;
130130

131131
// Decode timestamp into UNIX datetime structure
132-
void decode(struct tm* times, int* fractions = NULL) const;
132+
void decode(struct tm* times, int* fractions = NULL) const noexcept;
133133

134134
// Write access to timestamp structure we wrap
135-
ISC_TIMESTAMP& value() { return mValue; }
135+
ISC_TIMESTAMP& value() noexcept { return mValue; }
136136

137137
// Read access to timestamp structure we wrap
138-
const ISC_TIMESTAMP& value() const { return mValue; }
138+
const ISC_TIMESTAMP& value() const noexcept { return mValue; }
139139

140140
// Return current timestamp value
141141
static NoThrowTimeStamp getCurrentTimeStamp(const char** error) noexcept;
142142

143143
// Validation routines
144-
static bool isValidDate(const ISC_DATE ndate)
144+
static constexpr bool isValidDate(const ISC_DATE ndate) noexcept
145145
{
146146
return (ndate >= MIN_DATE && ndate <= MAX_DATE);
147147
}
148148

149-
static bool isValidTime(const ISC_TIME ntime)
149+
static constexpr bool isValidTime(const ISC_TIME ntime) noexcept
150150
{
151151
return (ntime < 24 * 3600 * ISC_TIME_SECONDS_PRECISION);
152152
}
153153

154-
static bool isValidTimeStamp(const ISC_TIMESTAMP ts)
154+
static constexpr bool isValidTimeStamp(const ISC_TIMESTAMP ts) noexcept
155155
{
156156
return (isValidDate(ts.timestamp_date) && isValidTime(ts.timestamp_time));
157157
}
@@ -165,25 +165,24 @@ class NoThrowTimeStamp
165165
static void decode_time(ISC_TIME ntime, int* hours, int* minutes, int* seconds, int* fractions = NULL) noexcept;
166166
static void decode_timestamp(const ISC_TIMESTAMP ntimestamp, struct tm* times, int* fractions = NULL) noexcept;
167167

168-
static void add10msec(ISC_TIMESTAMP* v, SINT64 msec, SINT64 multiplier);
169-
static void round_time(ISC_TIME& ntime, const int precision);
168+
static void add10msec(ISC_TIMESTAMP* v, SINT64 msec, SINT64 multiplier) noexcept;
169+
static void round_time(ISC_TIME& ntime, const int precision) noexcept;
170170

171-
static int convertGregorianDateToWeekDate(const struct tm& times);
172-
static int convertGregorianDateToJulianDate(int year, int month, int day);
173-
static void convertJulianDateToGregorianDate(int jdn, int& outYear, int& outMonth, int& outDay);
171+
static int convertGregorianDateToWeekDate(const struct tm& times) noexcept;
172+
static int convertGregorianDateToJulianDate(int year, int month, int day) noexcept;
173+
static void convertJulianDateToGregorianDate(int jdn, int& outYear, int& outMonth, int& outDay) noexcept;
174174

175-
static inline bool isLeapYear(const int year) noexcept
175+
static constexpr bool isLeapYear(const int year) noexcept
176176
{
177177
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
178178
}
179179

180-
static SINT64 timeStampToTicks(ISC_TIMESTAMP ts)
180+
static constexpr SINT64 timeStampToTicks(ISC_TIMESTAMP ts) noexcept
181181
{
182-
const SINT64 ticks = (ts.timestamp_date - MIN_DATE) * ISC_TICKS_PER_DAY + ts.timestamp_time;
183-
return ticks;
182+
return (static_cast<SINT64>(ts.timestamp_date) - MIN_DATE) * ISC_TICKS_PER_DAY + ts.timestamp_time;
184183
}
185184

186-
static ISC_TIMESTAMP ticksToTimeStamp(SINT64 ticks)
185+
static constexpr ISC_TIMESTAMP ticksToTimeStamp(SINT64 ticks) noexcept
187186
{
188187
ISC_TIMESTAMP ts;
189188
ts.timestamp_date = (ticks / ISC_TICKS_PER_DAY) + MIN_DATE;

0 commit comments

Comments
 (0)