| 1 |
.. title:: clang-tidy - abseil-time-subtraction |
| 2 |
|
| 3 |
abseil-time-subtraction |
| 4 |
======================= |
| 5 |
|
| 6 |
Finds and fixes ``absl::Time`` subtraction expressions to do subtraction |
| 7 |
in the Time domain instead of the numeric domain. |
| 8 |
|
| 9 |
There are two cases of Time subtraction in which deduce additional type |
| 10 |
information: |
| 11 |
|
| 12 |
- When the result is an ``absl::Duration`` and the first argument is an |
| 13 |
``absl::Time``. |
| 14 |
- When the second argument is a ``absl::Time``. |
| 15 |
|
| 16 |
In the first case, we must know the result of the operation, since without that |
| 17 |
the second operand could be either an ``absl::Time`` or an ``absl::Duration``. |
| 18 |
In the second case, the first operand *must* be an ``absl::Time``, because |
| 19 |
subtracting an ``absl::Time`` from an ``absl::Duration`` is not defined. |
| 20 |
|
| 21 |
Examples: |
| 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); |