Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/comphelper/date.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
 * 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
#pragma once
20
21
#include <sal/config.h>
22
#include <sal/types.h>
23
#include <comphelper/comphelperdllapi.h>
24
25
#include <cassert>
26
27
namespace comphelper::date
28
{
29
/** Days until start of year from zero, so month and day of month can be added.
30
31
    year 1 => 0 days, year 2 => 365 days, ...
32
    year -1 => -366 days, year -2 => -731 days, ...
33
34
    @param  nYear
35
            MUST be != 0.
36
 */
37
constexpr inline sal_Int32 YearToDays(sal_Int16 nYear)
38
12.1M
{
39
12.1M
    assert(nYear != 0);
40
12.1M
    auto val = [](int off, int y) { return off + y * 365 + y / 4 - y / 100 + y / 400; };
41
12.1M
    return nYear < 0 ? val(-366, nYear + 1) : val(0, nYear - 1);
42
12.1M
}
43
44
/** Whether year is a leap year.
45
46
    Leap years BCE are -1, -5, -9, ...
47
    See
48
    https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Usage
49
    https://en.wikipedia.org/wiki/0_(year)#History_of_astronomical_usage
50
51
    @param  nYear
52
            MUST be != 0.
53
 */
54
constexpr inline bool isLeapYear(sal_Int16 nYear)
55
1.15M
{
56
1.15M
    assert(nYear != 0);
57
1.15M
    if (nYear < 0)
58
190k
        nYear = -nYear - 1;
59
1.15M
    return (((nYear % 4) == 0) && ((nYear % 100) != 0)) || ((nYear % 400) == 0);
60
1.15M
}
61
62
/** Get number of days in month of year.
63
64
    @param  nYear
65
            MUST be != 0.
66
 */
67
constexpr inline sal_uInt16 getDaysInMonth(sal_uInt16 nMonth, sal_Int16 nYear)
68
10.4M
{
69
10.4M
    assert(1 <= nMonth && nMonth <= 12);
70
10.4M
    if (nMonth < 1 || 12 < nMonth)
71
0
        return 0;
72
73
10.4M
    constexpr sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
74
10.4M
    sal_uInt16 n = aDaysInMonth[nMonth - 1];
75
10.4M
    return nMonth == 2 && isLeapYear(nYear) ? n + 1 : n;
76
10.4M
}
77
78
/** Obtain days from zero for a given date, without normalizing.
79
80
    nDay, nMonth, nYear MUST form a valid proleptic Gregorian calendar date.
81
 */
82
constexpr inline sal_Int32 convertDateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
83
824k
{
84
824k
    sal_Int32 nDays = YearToDays(nYear);
85
3.62M
    for (sal_uInt16 i = 1; i < nMonth; ++i)
86
2.80M
        nDays += getDaysInMonth(i, nYear);
87
824k
    nDays += nDay;
88
824k
    return nDays;
89
824k
}
90
91
/** Obtain days from zero for a given date, with normalizing.
92
93
    nDay, nMonth, nYear may be out-of-bounds and are adjusted/normalized.
94
95
    @param  nYear
96
            Must be != 0, unless nMonth > 12.
97
 */
98
COMPHELPER_DLLPUBLIC sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth,
99
                                                            sal_Int16 nYear);
100
101
/** Whether date is a valid date.
102
 */
103
COMPHELPER_DLLPUBLIC bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear);
104
105
/** Obtain date for a days from zero value.
106
 */
107
COMPHELPER_DLLPUBLIC void convertDaysToDate(sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth,
108
                                            sal_Int16& rYear);
109
110
/** Normalize date, i.e. add days or months to form a proper proleptic
111
    Gregorian calendar date, unless all values are 0.
112
113
    @param  rYear
114
            Must be != 0, unless rMonth > 12.
115
116
    @return <TRUE/> if date was normalized, <FALSE/> if it was valid already
117
    or empty (all values 0).
118
 */
119
COMPHELPER_DLLPUBLIC bool normalize(sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear);
120
121
} // namespace comphelper::date
122
123
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */