docthemes: Save themes def. to a file when added to ColorSets
[LibreOffice.git] / comphelper / source / misc / date.cxx
blobb95f63f75c97a47dc5e07991580d7491fd626033
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.
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>
22 #include <cassert>
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)
52 return nNullDateDays;
54 normalize(nDay, nMonth, nYear);
55 return convertDateToDays(nDay, nMonth, nYear);
58 bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
60 if (nYear == 0)
61 return false;
62 if (nMonth < 1 || 12 < nMonth)
63 return false;
64 if (nDay < 1 || (nDay > comphelper::date::getDaysInMonth(nMonth, nYear)))
65 return false;
66 return true;
69 void convertDaysToDate(sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear)
71 if (nDays <= MIN_DAYS)
73 rDay = 1;
74 rMonth = 1;
75 rYear = kYearMin;
76 return;
78 if (nDays >= MAX_DAYS)
80 rDay = 31;
81 rMonth = 12;
82 rYear = kYearMax;
83 return;
86 // Day 0 is -0001-12-31, day 1 is 0001-01-01
87 const sal_Int16 nSign = (nDays <= 0 ? -1 : 1);
88 sal_Int32 nTempDays;
89 sal_Int32 i = 0;
90 bool bCalc;
94 rYear = static_cast<sal_Int16>((nDays / 365) - (i * nSign));
95 if (rYear == 0)
96 rYear = nSign;
97 nTempDays = nDays - YearToDays(rYear);
98 bCalc = false;
99 if (nTempDays < 1)
101 i += nSign;
102 bCalc = true;
104 else
106 if (nTempDays > 365)
108 if ((nTempDays != 366) || !isLeapYear(rYear))
110 i -= nSign;
111 bCalc = true;
115 } while (bCalc);
117 rMonth = 1;
118 while (nTempDays > getDaysInMonth(rMonth, rYear))
120 nTempDays -= getDaysInMonth(rMonth, rYear);
121 ++rMonth;
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))
130 return false;
132 if (rDay == 0 && rMonth == 0 && rYear == 0)
133 return false; // empty date
135 if (rDay == 0)
137 if (rMonth == 0)
138 ; // nothing, handled below
139 else
140 --rMonth;
141 // Last day of month is determined at the end.
144 if (rMonth > 12)
146 rYear += rMonth / 12;
147 rMonth = rMonth % 12;
148 if (rYear == 0)
149 rYear = 1;
151 if (rMonth == 0)
153 --rYear;
154 if (rYear == 0)
155 rYear = -1;
156 rMonth = 12;
159 if (rYear < 0)
161 sal_uInt16 nDays;
162 while (rDay > (nDays = getDaysInMonth(rMonth, rYear)))
164 rDay -= nDays;
165 if (rMonth > 1)
166 --rMonth;
167 else
169 if (rYear == kYearMin)
171 rDay = 1;
172 rMonth = 1;
173 return true;
175 --rYear;
176 rMonth = 12;
180 else
182 sal_uInt16 nDays;
183 while (rDay > (nDays = getDaysInMonth(rMonth, rYear)))
185 rDay -= nDays;
186 if (rMonth < 12)
187 ++rMonth;
188 else
190 if (rYear == kYearMax)
192 rDay = 31;
193 rMonth = 12;
194 return true;
196 ++rYear;
197 rMonth = 1;
202 if (rDay == 0)
203 rDay = getDaysInMonth(rMonth, rYear);
205 return true;
208 } // namespace comphelper::date
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */