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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/config.h>
22 #include <sal/types.h>
23 #include <comphelper/comphelperdllapi.h>
27 namespace comphelper::date
29 /** Days until start of year from zero, so month and day of month can be added.
31 year 1 => 0 days, year 2 => 365 days, ...
32 year -1 => -366 days, year -2 => -731 days, ...
37 constexpr inline sal_Int32
YearToDays(sal_Int16 nYear
)
40 auto val
= [](int off
, int y
) { return off
+ y
* 365 + y
/ 4 - y
/ 100 + y
/ 400; };
41 return nYear
< 0 ? val(-366, nYear
+ 1) : val(0, nYear
- 1);
44 /** Whether year is a leap year.
46 Leap years BCE are -1, -5, -9, ...
48 https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Usage
49 https://en.wikipedia.org/wiki/0_(year)#History_of_astronomical_usage
54 constexpr inline bool isLeapYear(sal_Int16 nYear
)
59 return (((nYear
% 4) == 0) && ((nYear
% 100) != 0)) || ((nYear
% 400) == 0);
62 /** Get number of days in month of year.
67 constexpr inline sal_uInt16
getDaysInMonth(sal_uInt16 nMonth
, sal_Int16 nYear
)
69 assert(1 <= nMonth
&& nMonth
<= 12);
70 if (nMonth
< 1 || 12 < nMonth
)
73 constexpr sal_uInt16 aDaysInMonth
[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
74 sal_uInt16 n
= aDaysInMonth
[nMonth
- 1];
75 return nMonth
== 2 && isLeapYear(nYear
) ? n
+ 1 : n
;
78 /** Obtain days from zero for a given date, without normalizing.
80 nDay, nMonth, nYear MUST form a valid proleptic Gregorian calendar date.
82 constexpr inline sal_Int32
convertDateToDays(sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
)
84 sal_Int32 nDays
= YearToDays(nYear
);
85 for (sal_uInt16 i
= 1; i
< nMonth
; ++i
)
86 nDays
+= getDaysInMonth(i
, nYear
);
91 /** Obtain days from zero for a given date, with normalizing.
93 nDay, nMonth, nYear may be out-of-bounds and are adjusted/normalized.
96 Must be != 0, unless nMonth > 12.
98 COMPHELPER_DLLPUBLIC sal_Int32
convertDateToDaysNormalizing(sal_uInt16 nDay
, sal_uInt16 nMonth
,
101 /** Whether date is a valid date.
103 COMPHELPER_DLLPUBLIC
bool isValidDate(sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
);
105 /** Obtain date for a days from zero value.
107 COMPHELPER_DLLPUBLIC
void convertDaysToDate(sal_Int32 nDays
, sal_uInt16
& rDay
, sal_uInt16
& rMonth
,
110 /** Normalize date, i.e. add days or months to form a proper proleptic
111 Gregorian calendar date, unless all values are 0.
114 Must be != 0, unless rMonth > 12.
116 @return <TRUE/> if date was normalized, <FALSE/> if it was valid already
117 or empty (all values 0).
119 COMPHELPER_DLLPUBLIC
bool normalize(sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
);
121 } // namespace comphelper::date
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */