Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/include/tools/time.hxx
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
#ifndef INCLUDED_TOOLS_TIME_HXX
20
#define INCLUDED_TOOLS_TIME_HXX
21
22
#include <sal/config.h>
23
24
#include <cmath>
25
26
#include <tools/toolsdllapi.h>
27
#include <com/sun/star/util/Time.hpp>
28
29
#include <compare>
30
31
namespace com::sun::star::util { struct DateTime; }
32
33
/**
34
 @WARNING: This class can serve both as wall clock time and time duration, and
35
           the mixing of these concepts leads to problems such as there being
36
           25 hours or 10 minus 20 seconds being (non-negative) 10 seconds.
37
*/
38
39
namespace tools {
40
41
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Time
42
{
43
private:
44
    sal_Int64       nTime;
45
4.91k
    explicit Time(sal_Int64 _nTime) { nTime = _nTime; }
46
    static sal_Int64 assemble(sal_uInt32 h, sal_uInt32 m, sal_uInt32 s, sal_uInt64 ns);
47
14.1M
    short GetSign() const { return (nTime >= 0) ? +1 : -1; }
48
49
public:
50
    enum TimeInitSystem
51
    {
52
        SYSTEM
53
    };
54
55
    // temporary until all uses are inspected and resolved
56
    enum TimeInitEmpty
57
    {
58
        EMPTY
59
    };
60
    static const sal_Int64 hourPerDay = 24;
61
    static const sal_Int64 minutePerHour = 60;
62
    static const sal_Int64 secondPerMinute = 60;
63
64
    static const sal_Int64 nanoSecPerSec = 1000000000;
65
    static const sal_Int64 nanoSecPerMinute = nanoSecPerSec * secondPerMinute;
66
    static const sal_Int64 nanoSecPerHour = nanoSecPerSec * secondPerMinute * minutePerHour;
67
    static const sal_Int64 nanoSecPerDay = nanoSecPerSec * secondPerMinute * minutePerHour * hourPerDay;
68
    static const sal_Int64 secondPerHour = secondPerMinute * minutePerHour;
69
    static const sal_Int64 secondPerDay  = secondPerMinute * minutePerHour * hourPerDay;
70
    static const sal_Int64 minutePerDay  = minutePerHour * hourPerDay;
71
    static const sal_Int64 nanoPerMicro  = 1000;
72
    static const sal_Int64 nanoPerMilli  = 1000000;
73
    static const sal_Int64 nanoPerCenti  = 10000000;
74
75
    static const sal_Int64 milliSecPerSec = 1000;
76
    static const sal_Int64 milliSecPerMinute = milliSecPerSec * secondPerMinute;
77
    static const sal_Int64 milliSecPerHour = milliSecPerMinute * minutePerHour;
78
    static const sal_Int64 milliSecPerDay = milliSecPerHour * hourPerDay;
79
80
81
                    explicit Time( TimeInitEmpty )
82
9.41M
                        { nTime = 0; }
83
                    explicit Time( TimeInitSystem );
84
                    Time( const tools::Time& rTime ) = default;
85
                    explicit Time( const css::util::Time& rTime );
86
                    explicit Time( const css::util::DateTime& rDateTime );
87
                    Time( sal_uInt32 nHour, sal_uInt32 nMin,
88
                          sal_uInt32 nSec = 0, sal_uInt64 nNanoSec = 0 );
89
90
    // The argument is not nanoseconds, it's what nTime must contain!
91
4.91k
    static Time fromEncodedTime(sal_Int64 _nTime) { return Time(_nTime); }
92
93
14.6M
    void            SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; }
94
15.5M
    sal_Int64       GetTime() const { return nTime; }
95
0
    css::util::Time GetUNOTime() const { return css::util::Time(GetNanoSec(),GetSec(),GetMin(),GetHour(),false); }
96
97
    void            SetHour( sal_uInt16 nNewHour );
98
    void            SetMin( sal_uInt16 nNewMin );
99
    void            SetSec( sal_uInt16 nNewSec );
100
    void            SetNanoSec( sal_uInt32 nNewNanoSec );
101
22.7M
    sal_uInt16      GetHour() const { return std::abs(nTime) / SAL_CONST_UINT64(10000000000000); }
102
15.7M
    sal_uInt16      GetMin() const { return (std::abs(nTime) / SAL_CONST_UINT64(100000000000)) % 100; }
103
15.7M
    sal_uInt16      GetSec() const { return (std::abs(nTime) / SAL_CONST_UINT64(1000000000)) % 100; }
104
14.2M
    sal_uInt32      GetNanoSec() const { return std::abs(nTime) % SAL_CONST_UINT64(1000000000); }
105
106
    // TODO: consider removing GetMSFromTime and MakeTimeFromMS?
107
    sal_Int32       GetMSFromTime() const;
108
    void            MakeTimeFromMS( sal_Int32 nMS );
109
    sal_Int64       GetNSFromTime() const;
110
    void            MakeTimeFromNS( sal_Int64 nNS );
111
112
                    /// 12 hours == 0.5 days
113
    double          GetTimeInDays() const;
114
115
    /** Get the wall clock time particles for a (date+)time value.
116
117
        Does the necessary rounding and truncating to obtain hour, minute,
118
        second and fraction of second from a double time value (time in days,
119
        0.5 == 12h) such that individual values are not rounded up, i.e.
120
        x:59:59.999 does not yield x+1:0:0.00
121
122
        A potential date component (fTimeInDays >= 1.0) is discarded.
123
124
        @param  nFractionDecimals
125
                If > 0 fFractionOfSecond is truncated to that amount of
126
                decimals.
127
                Else fFractionOfSecond returns the full remainder of the
128
                fractional second.
129
     */
130
    static void     GetClock( double fTimeInDays,
131
                              sal_uInt16& nHour, sal_uInt16& nMinute, sal_uInt16& nSecond,
132
                              double& fFractionOfSecond, int nFractionDecimals );
133
134
    bool            IsEqualIgnoreNanoSec( const tools::Time& rTime ) const;
135
136
0
    auto            operator <=> ( const Time& rTime ) const = default;
137
138
    static Time     GetUTCOffset();
139
140
    /**
141
     * Elapsed time in milliseconds (1e-3) since some unspecified starting point
142
     *
143
     * Convenience function, which just calls GetMonotonicTicks() / 1000.
144
     */
145
    static sal_uInt64 GetSystemTicks();
146
147
    /**
148
     * Elapsed time in microseconds (1e-6) since some unspecified starting point
149
     *
150
     * Uses the high-precision, monotonic time sources provided by the OS, if
151
     * available. Don't try to relate it to the system time, and also it's long
152
     * time accuracy is not the best.
153
     *
154
     * Currently used to measure the runtime of OpenCL shaders and to set a
155
     * message creation timestamp to allow filtering of invalid timer messages.
156
     *
157
     * @return current system ticks in microseconds (1e-6s)
158
     */
159
    static sal_uInt64 GetMonotonicTicks();
160
161
    tools::Time&           operator =( const tools::Time& rTime ) = default;
162
    Time            operator -() const
163
0
                        { return Time( -nTime ); }
164
    tools::Time&           operator +=( const tools::Time& rTime );
165
    tools::Time&           operator -=( const tools::Time& rTime );
166
    TOOLS_DLLPUBLIC friend Time     operator +( const tools::Time& rTime1, const tools::Time& rTime2 );
167
    TOOLS_DLLPUBLIC friend Time     operator -( const tools::Time& rTime1, const tools::Time& rTime2 );
168
};
169
170
} /* namespace tools */
171
172
#endif
173
174
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */