Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/tools/datetime.hxx
Line
Count
Source
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_DATETIME_HXX
20
#define INCLUDED_TOOLS_DATETIME_HXX
21
22
#include <tools/toolsdllapi.h>
23
#include <tools/date.hxx>
24
#include <tools/time.hxx>
25
#include <com/sun/star/util/DateTime.hpp>
26
27
#include <ostream>
28
29
namespace tools
30
{
31
class Duration;
32
}
33
34
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC DateTime : public Date, public tools::Time
35
{
36
public:
37
    enum DateTimeInitSystem
38
    {
39
        SYSTEM
40
    };
41
42
    enum DateTimeInitEmpty
43
    {
44
        EMPTY
45
    };
46
47
75.9k
                    explicit DateTime( DateTimeInitEmpty ) : Date( Date::EMPTY ), Time( Time::EMPTY ) {}
48
                    explicit DateTime( DateTimeInitSystem );
49
                    DateTime( const DateTime& rDateTime ) :
50
2.97M
                        Date( rDateTime ), Time( rDateTime ) {}
51
320k
                    explicit DateTime( const Date& rDate ) : Date( rDate ), Time(Time::EMPTY) {}
52
0
                    explicit DateTime( const tools::Time& rTime ) : Date(0), Time( rTime ) {}
53
                    DateTime( const Date& rDate, const tools::Time& rTime ) :
54
1.07M
                        Date( rDate ), Time( rTime ) {}
55
                    explicit DateTime( const css::util::DateTime& rDateTime );
56
57
    css::util::DateTime
58
                    GetUNODateTime() const
59
39.6k
                        { return css::util::DateTime(GetNanoSec(), GetSec(), GetMin(), GetHour(),
60
39.6k
                              GetDay(), GetMonth(), GetYear(), false); }
61
62
    bool            IsBetween( const DateTime& rFrom,
63
                               const DateTime& rTo ) const;
64
65
    bool            IsEqualIgnoreNanoSec( const DateTime& rDateTime ) const
66
0
                    {
67
0
                        if ( GetDate() != rDateTime.GetDate() )
68
0
                            return false;
69
0
                        return Time::IsEqualIgnoreNanoSec( rDateTime );
70
0
                    }
71
72
    auto            operator <=>( const DateTime& rDateTime ) const
73
575k
                    {
74
575k
                        return std::make_pair(GetDate(), GetTime()) <=>
75
575k
                            std::make_pair(rDateTime.GetDate(), rDateTime.GetTime());
76
575k
                    }
77
    bool            operator==(const DateTime& rDateTime) const
78
3.54M
                    {
79
3.54M
                        return (Date::operator==(rDateTime) && tools::Time::operator==(rDateTime));
80
3.54M
                    }
81
82
    sal_Int64       GetSecFromDateTime( const Date& rDate ) const;
83
84
4.95M
    void            ConvertToUTC()       { *this -= Time::GetUTCOffset(); }
85
15.2k
    void            ConvertToLocalTime() { *this += Time::GetUTCOffset(); }
86
87
    void            AddTime( double fTimeInDays );
88
    DateTime&       operator +=( const tools::Time& rTime );
89
    DateTime&       operator -=( const tools::Time& rTime );
90
    /** Duration can be negative, so adding it will subtract its value. */
91
    DateTime&       operator +=( const tools::Duration& rDuration );
92
private:
93
    void            NormalizeTimeRemainderAndApply( tools::Time& rTime );
94
public:
95
96
    TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays );
97
    TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays );
98
    TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, double fTimeInDays );
99
    TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, double fTimeInDays )
100
0
                        { return operator+( rDateTime, -fTimeInDays ); }
101
    TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime );
102
    TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime );
103
    /** Use operator-() if a duration is to be remembered or processed. */
104
    TOOLS_DLLPUBLIC friend tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 );
105
    /** Use Sub() if the floating point "time in days" value is to be
106
        processed. This also takes a shortcut for whole days values (equal
107
        times), and only for times inflicted values uses an intermediary
108
        tools::Duration for conversion. Note that the resulting floating point
109
        value nevertheless in many cases is not an exact representation down to
110
        nanoseconds. */
111
    static  double  Sub( const DateTime& rDateTime1, const DateTime& rDateTime2 );
112
    TOOLS_DLLPUBLIC friend sal_Int64 operator -( const DateTime& rDateTime, const Date& rDate )
113
37
                        { return static_cast<const Date&>(rDateTime) - rDate; }
114
    /** Duration can be negative, so adding it will subtract its value. */
115
    TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration );
116
117
    DateTime&       operator =( const DateTime& rDateTime );
118
    DateTime&       operator =( const css::util::DateTime& rUDateTime );
119
120
    void            GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const;
121
    static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper );
122
123
    /// Creates DateTime given a unix time, which is the number of seconds
124
    /// elapsed since Jan 1st, 1970.
125
    static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch );
126
};
127
128
inline DateTime& DateTime::operator =( const DateTime& rDateTime )
129
122k
{
130
122k
    Date::operator=( rDateTime );
131
122k
    Time::operator=( rDateTime );
132
122k
    return *this;
133
122k
}
134
135
template< typename charT, typename traits >
136
inline std::basic_ostream<charT, traits> & operator <<(
137
    std::basic_ostream<charT, traits> & stream, const DateTime& datetime)
138
{
139
    stream << datetime.GetYear() << '-';
140
    stream.fill('0');
141
    stream.width(2);
142
    stream << datetime.GetMonth() << '-';
143
    stream.width(2);
144
    stream << datetime.GetDay() << ' ';
145
    stream.width(2);
146
    stream << datetime.GetHour() << ':';
147
    stream.width(2);
148
    stream << datetime.GetMin() << ':';
149
    stream.width(2);
150
    stream << datetime.GetSec() << ".";
151
    stream.width(9);
152
    return stream << datetime.GetNanoSec();
153
}
154
155
#endif
156
157
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */