tdf#143148 Use pragma once instead of include guards
[LibreOffice.git] / include / tools / duration.hxx
blobca0d7088b7e130cbdb2a20a2caa9c5a8dc0c747c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include <tools/time.hxx>
14 class DateTime;
16 namespace tools
18 /** Duration in days and time. Can be negative in which case days is 0 and time
19 is negative or both days and time are negative.
21 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Duration
23 public:
24 Duration() {}
26 /** Assumes that DateTime are normalized and there are no Time out-of-range
27 field values. */
28 Duration(const ::DateTime& rStart, const ::DateTime& rEnd);
30 /** Time can be a limited duration as well. We don't cater for out-of-range
31 minutes and seconds values here though. */
32 Duration(const Time& rStart, const Time& rEnd);
34 constexpr static sal_uInt64 kAccuracyEpsilonNanoseconds = 300;
35 constexpr static sal_uInt64 kAccuracyEpsilonNanosecondsMicroseconds = 999;
37 /** Difference in days, like DateTime()-DateTime().
39 Can also be used to round a date+time value to, for example, microseconds.
41 @param nAccuracyEpsilonNanoseconds
42 Round for example by 1 nanosecond if it's just 1 off to a
43 second, i.e. 0999999999 or 0000000001. This can be loosened if
44 necessary. For example, if fTimeInDays is a date+time in
45 "today's" range with a significant seconds resolution, an
46 accuracy epsilon (=unsharpness) of ~300 is required. Hence default.
47 Must be 0 <= nAccuracyEpsilonNanoseconds <= Time::nanoSecPerSec - 1.
49 explicit Duration(double fTimeInDays,
50 sal_uInt64 nAccuracyEpsilonNanoseconds = kAccuracyEpsilonNanoseconds);
52 /** Time can be a limited duration as well and can have out-of-range
53 values, it will be normalized. Sign of both days and Time must be equal
54 unless one is 0. */
55 Duration(sal_Int32 nDays, const Time& rTime);
57 /** Individual time values can be out-of-range, all will be normalized.
58 Additionally, the resulting time overall hour value is not restricted
59 to sal_uInt16 like it is with Time, as values >=24 flow over into days.
60 For a negative duration only a negative nDays can be given, thus a
61 negative duration of less than one day is not possible. */
62 Duration(sal_Int32 nDays, sal_uInt32 nHours, sal_uInt32 nMinutes, sal_uInt32 nSeconds,
63 sal_uInt64 nNanoseconds);
65 bool IsNegative() const { return mnDays < 0 || maTime.GetTime() < 0; }
66 sal_Int32 GetDays() const { return mnDays; }
67 const Time& GetTime() const { return maTime; }
68 double GetInDays() const { return static_cast<double>(GetDays()) + GetTime().GetTimeInDays(); }
70 /** Whether a duration is set. */
71 operator bool() const { return maTime.GetTime() != 0 || mnDays != 0; }
73 /** Unary minus. */
74 Duration operator-() const;
76 /** Add a duration to this instance. */
77 Duration& Add(const Duration& rDuration, bool& rbOverflow);
79 /** Get multiple of duration. */
80 Duration Mult(sal_Int32 nMult, bool& rbOverflow) const;
82 private:
83 /** Internal days and Time values. */
84 Duration(sal_Int32 nDays, sal_Int64 nTime);
86 /** Prerequisite: mnDays is already set. */
87 void Normalize(sal_uInt64 nHours, sal_uInt64 nMinutes, sal_uInt64 nSeconds,
88 sal_uInt64 nNanoseconds, bool bNegative);
90 /** Prerequisite: mnDays is already correctly set and absolute value of
91 nanoseconds less than one day. */
92 void ApplyTime(sal_Int64 nNS);
94 /** Prerequisite: mnDays is already correctly set and Time hour values
95 are adjusted. */
96 void SetTimeDiff(const Time& rStart, const Time& rEnd);
98 private:
99 Time maTime = Time(Time::EMPTY);
100 sal_Int32 mnDays = 0;
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */