Repository: SerenityOS/serenity
File: AK/Time.h (C/C++)

Code before:
    }
    [[nodiscard]] constexpr static Duration from_milliseconds(i64 milliseconds)
    {
        i64 seconds = sane_mod(milliseconds, 1'000);
        return Duration(seconds, milliseconds * 1'000'000);
    }
    [[nodiscard]] static Duration from_ticks(clock_t, time_t);
    [[nodiscard]] static Duration from_timespec(const struct timespec&);
    [[nodiscard]] static Duration from_timeval(const struct timeval&);
    // We don't pull in <stdint.h> for the pretty min/max definitions because this file is also included in the Kernel
    [[nodiscard]] constexpr static Duration min() { return Duration(-__INT64_MAX__ - 1LL, 0); }
    [[nodiscard]] constexpr static Duration zero() { return Duration(0, 0); }
    [[nodiscard]] constexpr static Duration max() { return Duration(__INT64_MAX__, 999'999'999); }

    // Truncates towards zero (2.8s to 2s, -2.8s to -2s).
    [[nodiscard]] i64 to_truncated_seconds() const;
    [[nodiscard]] i64 to_truncated_milliseconds() const;
    [[nodiscard]] i64 to_truncated_microseconds() const;
    // Rounds away from zero (2.3s to 3s, -2.3s to -3s).
    [[nodiscard]] constexpr i64 to_seconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        if (m_seconds >= 0 && m_nanoseconds) {
            Checked<i64> seconds(m_seconds);
            seconds++;
            return seconds.has_overflow() ? 0x7fff'ffff'ffff'ffffLL : seconds.value();
        }
        return m_seconds;
    }
    [[nodiscard]] constexpr i64 to_milliseconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        Checked<i64> milliseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
        milliseconds *= 1'000;
        milliseconds += m_nanoseconds / 1'000'000;
        if (m_seconds >= 0 && m_nanoseconds % 1'000'000 != 0)
            milliseconds++;
        if (m_seconds < 0) {
            // We dropped one second previously, put it back in now that we have handled the rounding.
            milliseconds -= 1'000;
        }
        if (!milliseconds.has_overflow())
            return milliseconds.value();
        return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
    }
    [[nodiscard]] constexpr i64 to_microseconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        Checked<i64> microseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
        microseconds *= 1'000'000;
        microseconds += m_nanoseconds / 1'000;

Review comment (suggestion):
```suggestion
            return Checked<i64>::saturating_add(m_seconds, 1);
```

Code after:
    }
    [[nodiscard]] constexpr static Duration from_milliseconds(i64 milliseconds)
    {
        i64 seconds = sane_mod(milliseconds, 1'000);
        return Duration(seconds, milliseconds * 1'000'000);
    }
    [[nodiscard]] static Duration from_ticks(clock_t, time_t);
    [[nodiscard]] static Duration from_timespec(const struct timespec&);
    [[nodiscard]] static Duration from_timeval(const struct timeval&);
    // We don't pull in <stdint.h> for the pretty min/max definitions because this file is also included in the Kernel
    [[nodiscard]] constexpr static Duration min() { return Duration(-__INT64_MAX__ - 1LL, 0); }
    [[nodiscard]] constexpr static Duration zero() { return Duration(0, 0); }
    [[nodiscard]] constexpr static Duration max() { return Duration(__INT64_MAX__, 999'999'999); }

    // Truncates towards zero (2.8s to 2s, -2.8s to -2s).
    [[nodiscard]] i64 to_truncated_seconds() const;
    [[nodiscard]] i64 to_truncated_milliseconds() const;
    [[nodiscard]] i64 to_truncated_microseconds() const;

    // Rounds away from zero (2.3s to 3s, -2.3s to -3s).
    [[nodiscard]] constexpr i64 to_seconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        if (m_seconds >= 0 && m_nanoseconds)
            return Checked<i64>::saturating_add(m_seconds, 1);

        return m_seconds;
    }

    [[nodiscard]] constexpr i64 to_milliseconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        Checked<i64> milliseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
        milliseconds *= 1'000;
        milliseconds += m_nanoseconds / 1'000'000;
        if (m_seconds >= 0 && m_nanoseconds % 1'000'000 != 0)
            milliseconds++;
        if (m_seconds < 0) {
            // We dropped one second previously, put it back in now that we have handled the rounding.
            milliseconds -= 1'000;
        }
        if (!milliseconds.has_overflow())
            return milliseconds.value();
        return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
    }

    [[nodiscard]] constexpr i64 to_microseconds() const
    {
        VERIFY(m_nanoseconds < 1'000'000'000);
        Checked<i64> microseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
        microseconds *= 1'000'000;