Skip to content

Commit

Permalink
Move Duration ToInt64<unit> functions to be inline.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 706782054
Change-Id: Iade7f9068a81df57d7b84df9ea50e088e33cac9c
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 16, 2024
1 parent 85c701d commit d30f578
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
44 changes: 0 additions & 44 deletions absl/time/duration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,50 +549,6 @@ Duration DurationFromTimeval(timeval tv) {
//
// Conversion to other duration types.
//

int64_t ToInt64Nanoseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 33 == 0) {
return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) +
(time_internal::GetRepLo(d) / kTicksPerNanosecond);
}
return d / Nanoseconds(1);
}
int64_t ToInt64Microseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 43 == 0) {
return (time_internal::GetRepHi(d) * 1000 * 1000) +
(time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000));
}
return d / Microseconds(1);
}
int64_t ToInt64Milliseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 53 == 0) {
return (time_internal::GetRepHi(d) * 1000) +
(time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000 * 1000));
}
return d / Milliseconds(1);
}
int64_t ToInt64Seconds(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi;
}
int64_t ToInt64Minutes(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi / 60;
}
int64_t ToInt64Hours(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi / (60 * 60);
}

double ToDoubleNanoseconds(Duration d) {
return FDivDuration(d, Nanoseconds(1));
}
Expand Down
62 changes: 56 additions & 6 deletions absl/time/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,12 @@ ABSL_ATTRIBUTE_CONST_FUNCTION Duration Hours(T n) {
//
// absl::Duration d = absl::Milliseconds(1500);
// int64_t isec = absl::ToInt64Seconds(d); // isec == 1
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Nanoseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Microseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Seconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Minutes(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Hours(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Nanoseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Microseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Milliseconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Seconds(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Minutes(Duration d);
ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Hours(Duration d);

// ToDoubleNanoseconds()
// ToDoubleMicroseconds()
Expand Down Expand Up @@ -1864,6 +1864,56 @@ ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t) {
return time_internal::FromUnixDuration(Seconds(t));
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Nanoseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 33 == 0) {
return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) +
(time_internal::GetRepLo(d) / time_internal::kTicksPerNanosecond);
}
return d / Nanoseconds(1);
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Microseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 43 == 0) {
return (time_internal::GetRepHi(d) * 1000 * 1000) +
(time_internal::GetRepLo(d) /
(time_internal::kTicksPerNanosecond * 1000));
}
return d / Microseconds(1);
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Milliseconds(Duration d) {
if (time_internal::GetRepHi(d) >= 0 &&
time_internal::GetRepHi(d) >> 53 == 0) {
return (time_internal::GetRepHi(d) * 1000) +
(time_internal::GetRepLo(d) /
(time_internal::kTicksPerNanosecond * 1000 * 1000));
}
return d / Milliseconds(1);
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Seconds(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi;
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Minutes(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi / 60;
}

ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Hours(Duration d) {
int64_t hi = time_internal::GetRepHi(d);
if (time_internal::IsInfiniteDuration(d)) return hi;
if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
return hi / (60 * 60);
}

ABSL_NAMESPACE_END
} // namespace absl

Expand Down

0 comments on commit d30f578

Please sign in to comment.