1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
12 #include <tools/time.hxx>
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
26 /** Assumes that DateTime are normalized and there are no Time out-of-range
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 /** Difference in days, like DateTime()-DateTime().
36 @param nAccuracyEpsilonNanoseconds
37 Round for example by 1 nanosecond if it's just 1 off to a
38 second, i.e. 0999999999 or 0000000001. This can be loosened if
39 necessary. For example, if fTimeInDays is a date+time in
40 "today's" range with a significant seconds resolution, an
41 accuracy epsilon (=unsharpness) of ~300 is required. Hence default.
42 Must be 0 <= nAccuracyEpsilonNanoseconds <= Time::nanoSecPerSec - 1.
44 explicit Duration(double fTimeInDays
, sal_uInt64 nAccuracyEpsilonNanoseconds
= 300);
46 /** Time can be a limited duration as well and can have out-of-range
47 values, it will be normalized. Sign of both days and Time must be equal
49 Duration(sal_Int32 nDays
, const Time
& rTime
);
51 /** Individual time values can be out-of-range, all will be normalized.
52 Additionally, the resulting time overall hour value is not restricted
53 to sal_uInt16 like it is with Time, as values >=24 flow over into days.
54 For a negative duration only a negative nDays can be given, thus a
55 negative duration of less than one day is not possible. */
56 Duration(sal_Int32 nDays
, sal_uInt32 nHours
, sal_uInt32 nMinutes
, sal_uInt32 nSeconds
,
57 sal_uInt64 nNanoseconds
);
59 bool IsNegative() const { return mnDays
< 0 || maTime
.GetTime() < 0; }
60 sal_Int32
GetDays() const { return mnDays
; }
61 const Time
& GetTime() const { return maTime
; }
62 double GetInDays() const { return static_cast<double>(GetDays()) + GetTime().GetTimeInDays(); }
64 /** Whether a duration is set. */
65 operator bool() const { return maTime
.GetTime() != 0 || mnDays
!= 0; }
68 Duration
operator-() const;
70 /** Add a duration to this instance. */
71 Duration
& Add(const Duration
& rDuration
, bool& rbOverflow
);
73 /** Get multiple of duration. */
74 Duration
Mult(sal_Int32 nMult
, bool& rbOverflow
) const;
77 /** Internal days and Time values. */
78 Duration(sal_Int32 nDays
, sal_Int64 nTime
);
80 /** Prerequisite: mnDays is already set. */
81 void Normalize(sal_uInt64 nHours
, sal_uInt64 nMinutes
, sal_uInt64 nSeconds
,
82 sal_uInt64 nNanoseconds
, bool bNegative
);
84 /** Prerequisite: mnDays is already correctly set and absolute value of
85 nanoseconds less than one day. */
86 void ApplyTime(sal_Int64 nNS
);
88 /** Prerequisite: mnDays is already correctly set and Time hour values
90 void SetTimeDiff(const Time
& rStart
, const Time
& rEnd
);
93 Time maTime
= Time(0);
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */