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 .
20 #include <comphelper/date.hxx>
24 namespace comphelper::date
26 // Once upon a time the number of days we internally handled in tools' class
27 // Date was limited to MAX_DAYS 3636532. That changed with a full 16-bit year.
28 // Assuming the first valid positive date in a proleptic Gregorian calendar is
29 // 0001-01-01, this resulted in an end date of 9957-06-26.
30 // Hence we documented that years up to and including 9956 are handled.
31 /* XXX: it is unclear history why this value was chosen, the representable
32 * 9999-12-31 would be 3652060 days from 0001-01-01. Even 9998-12-31 to
33 * distinguish from a maximum possible date would be 3651695.
34 * There is connectivity/source/commontools/dbconversion.cxx that still has the
35 * same value to calculate with css::util::Date */
36 /* XXX can that dbconversion cope with years > 9999 or negative years at all?
37 * Database fields may be limited to positive 4 digits. */
39 constexpr sal_Int32 MIN_DAYS
= -11968265; // -32768-01-01
40 constexpr sal_Int32 MAX_DAYS
= 11967900; // 32767-12-31
42 constexpr sal_Int16 kYearMax
= SAL_MAX_INT16
;
43 constexpr sal_Int16 kYearMin
= SAL_MIN_INT16
;
45 constexpr sal_Int32 nNullDateDays
= convertDateToDays(30, 12, 1899);
46 static_assert(nNullDateDays
== 693594);
48 sal_Int32
convertDateToDaysNormalizing(sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
)
50 // Speed-up the common null-date 1899-12-30.
51 if (nYear
== 1899 && nMonth
== 12 && nDay
== 30)
54 normalize(nDay
, nMonth
, nYear
);
55 return convertDateToDays(nDay
, nMonth
, nYear
);
58 bool isValidDate(sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
)
62 if (nMonth
< 1 || 12 < nMonth
)
64 if (nDay
< 1 || (nDay
> comphelper::date::getDaysInMonth(nMonth
, nYear
)))
69 void convertDaysToDate(sal_Int32 nDays
, sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
)
71 if (nDays
<= MIN_DAYS
)
78 if (nDays
>= MAX_DAYS
)
86 // Day 0 is -0001-12-31, day 1 is 0001-01-01
87 const sal_Int16 nSign
= (nDays
<= 0 ? -1 : 1);
94 rYear
= static_cast<sal_Int16
>((nDays
/ 365) - (i
* nSign
));
97 nTempDays
= nDays
- YearToDays(rYear
);
108 if ((nTempDays
!= 366) || !isLeapYear(rYear
))
118 while (nTempDays
> getDaysInMonth(rMonth
, rYear
))
120 nTempDays
-= getDaysInMonth(rMonth
, rYear
);
124 rDay
= static_cast<sal_uInt16
>(nTempDays
);
127 bool normalize(sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
)
129 if (isValidDate(rDay
, rMonth
, rYear
))
132 if (rDay
== 0 && rMonth
== 0 && rYear
== 0)
133 return false; // empty date
138 ; // nothing, handled below
141 // Last day of month is determined at the end.
146 rYear
+= rMonth
/ 12;
147 rMonth
= rMonth
% 12;
162 while (rDay
> (nDays
= getDaysInMonth(rMonth
, rYear
)))
169 if (rYear
== kYearMin
)
183 while (rDay
> (nDays
= getDaysInMonth(rMonth
, rYear
)))
190 if (rYear
== kYearMax
)
203 rDay
= getDaysInMonth(rMonth
, rYear
);
208 } // namespace comphelper::date
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */