xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/abseil/time-subtraction.rst (revision 6e566bc5523f743bc34a7e26f050f1f2b4d699a8)
1.. title:: clang-tidy - abseil-time-subtraction
2
3abseil-time-subtraction
4=======================
5
6Finds and fixes ``absl::Time`` subtraction expressions to do subtraction
7in the Time domain instead of the numeric domain.
8
9There are two cases of Time subtraction in which deduce additional type
10information:
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
16In the first case, we must know the result of the operation, since without that
17the second operand could be either an ``absl::Time`` or an ``absl::Duration``.
18In the second case, the first operand *must* be an ``absl::Time``, because
19subtracting 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