Skip to content

Commit

Permalink
util/time: Improve usecs handling in time macros
Browse files Browse the repository at this point in the history
Fix SCTIME_ADD_SECS zeroing subsecond part

When adding s seconds to SCtime_t ts, don't zero out the ts.usecs field.

Fixes Redmine Bug #6584

Fix SCTIME_FROM_TIMESPEC garbage microseconds part

When converting nanosecond to microseconds divide by 1000 instead
of multiplying by 1000.

Fixes Redmine bug #6585
  • Loading branch information
sfd authored and jlucovsky committed Dec 5, 2023
1 parent 66896c6 commit b982cb5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/util-time.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ typedef struct {
#define SCTIME_USECS(t) ((uint64_t)(t).usecs)
#define SCTIME_SECS(t) ((uint64_t)(t).secs)
#define SCTIME_MSECS(t) (SCTIME_SECS(t) * 1000 + SCTIME_USECS(t) / 1000)
#define SCTIME_ADD_SECS(ts, s) SCTIME_FROM_SECS((ts).secs + (s))
#define SCTIME_ADD_USECS(ts, us) \
(SCTime_t) \
{ \
.secs = (ts).secs + ((ts).usecs + (us)) / 1000000, .usecs = ((ts).usecs + (us)) % 1000000 \
}
#define SCTIME_ADD_SECS(ts, s) \
(SCTime_t) \
{ \
.secs = (ts).secs + (s), .usecs = (ts).usecs \
}
#define SCTIME_FROM_SECS(s) \
(SCTime_t) \
{ \
Expand All @@ -87,7 +91,7 @@ typedef struct {
#define SCTIME_FROM_TIMESPEC(ts) \
(SCTime_t) \
{ \
.secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec * 1000 \
.secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec / 1000 \
}

#define SCTIME_TO_TIMEVAL(tv, t) \
Expand Down

0 comments on commit b982cb5

Please sign in to comment.