40 lines · plain
1.. title:: clang-tidy - abseil-time-subtraction2 3abseil-time-subtraction4=======================5 6Finds and fixes ``absl::Time`` subtraction expressions to do subtraction7in the Time domain instead of the numeric domain.8 9There are two cases of Time subtraction in which deduce additional type10information:11 12- When the result is an ``absl::Duration`` and the first argument is an13 ``absl::Time``.14- When the second argument is a ``absl::Time``.15 16In the first case, we must know the result of the operation, since without that17the second operand could be either an ``absl::Time`` or an ``absl::Duration``.18In the second case, the first operand *must* be an ``absl::Time``, because19subtracting an ``absl::Time`` from an ``absl::Duration`` is not defined.20 21Examples:22 23.. code-block:: c++24 25 int x;26 absl::Time t;27 28 // Original - absl::Duration result and first operand is an absl::Time.29 absl::Duration d = absl::Seconds(absl::ToUnixSeconds(t) - x);30 31 // Suggestion - Perform subtraction in the Time domain instead.32 absl::Duration d = t - absl::FromUnixSeconds(x);33 34 35 // Original - Second operand is an absl::Time.36 int i = x - absl::ToUnixSeconds(t);37 38 // Suggestion - Perform subtraction in the Time domain instead.39 int i = absl::ToInt64Seconds(absl::FromUnixSeconds(x) - t);40