/src/libreoffice/include/tools/duration.hxx
Line | Count | Source |
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. |
4 | | * |
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 | | */ |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #include <tools/time.hxx> |
13 | | |
14 | | class DateTime; |
15 | | |
16 | | namespace tools |
17 | | { |
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. |
20 | | */ |
21 | | class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Duration |
22 | | { |
23 | | public: |
24 | 0 | Duration() {} |
25 | | |
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); |
29 | | |
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); |
33 | | |
34 | | constexpr static sal_uInt64 kAccuracyEpsilonNanoseconds = 300; |
35 | | constexpr static sal_uInt64 kAccuracyEpsilonNanosecondsMicroseconds = 999; |
36 | | |
37 | | /** Difference in days, like DateTime()-DateTime(). |
38 | | |
39 | | Can also be used to round a date+time value to, for example, microseconds. |
40 | | |
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. |
48 | | */ |
49 | | explicit Duration(double fTimeInDays, |
50 | | sal_uInt64 nAccuracyEpsilonNanoseconds = kAccuracyEpsilonNanoseconds); |
51 | | |
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); |
56 | | |
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); |
64 | | |
65 | 0 | bool IsNegative() const { return mnDays < 0 || maTime.GetTime() < 0; } |
66 | 40.5k | sal_Int32 GetDays() const { return mnDays; } |
67 | 40.5k | const Time& GetTime() const { return maTime; } |
68 | 40.5k | double GetInDays() const { return static_cast<double>(GetDays()) + GetTime().GetTimeInDays(); } |
69 | | |
70 | | /** Whether a duration is set. */ |
71 | 0 | operator bool() const { return maTime.GetTime() != 0 || mnDays != 0; } |
72 | | |
73 | | /** Unary minus. */ |
74 | | Duration operator-() const; |
75 | | |
76 | | /** Add a duration to this instance. */ |
77 | | Duration& Add(const Duration& rDuration, bool& rbOverflow); |
78 | | |
79 | | /** Get multiple of duration. */ |
80 | | Duration Mult(sal_Int32 nMult, bool& rbOverflow) const; |
81 | | |
82 | | private: |
83 | | /** Internal days and Time values. */ |
84 | | Duration(sal_Int32 nDays, sal_Int64 nTime); |
85 | | |
86 | | /** Prerequisite: mnDays is already set. */ |
87 | | void Normalize(sal_uInt64 nHours, sal_uInt64 nMinutes, sal_uInt64 nSeconds, |
88 | | sal_uInt64 nNanoseconds, bool bNegative); |
89 | | |
90 | | /** Prerequisite: mnDays is already correctly set and absolute value of |
91 | | nanoseconds less than one day. */ |
92 | | void ApplyTime(sal_Int64 nNS); |
93 | | |
94 | | /** Prerequisite: mnDays is already correctly set and Time hour values |
95 | | are adjusted. */ |
96 | | void SetTimeDiff(const Time& rStart, const Time& rEnd); |
97 | | |
98 | | private: |
99 | | Time maTime = Time(Time::EMPTY); |
100 | | sal_Int32 mnDays = 0; |
101 | | }; |
102 | | } |
103 | | |
104 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |