LibreOffice Module comphelper (master) 1
date.cxx
Go to the documentation of this file.
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
20#include <comphelper/date.hxx>
21
22#include <cassert>
23
25{
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. */
38
39constexpr sal_Int32 MIN_DAYS = -11968265; // -32768-01-01
40constexpr sal_Int32 MAX_DAYS = 11967900; // 32767-12-31
41
42constexpr sal_Int16 kYearMax = SAL_MAX_INT16;
43constexpr sal_Int16 kYearMin = SAL_MIN_INT16;
44
45constexpr sal_Int32 nNullDateDays = convertDateToDays(30, 12, 1899);
46static_assert(nNullDateDays == 693594);
47
48sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
49{
50 // Speed-up the common null-date 1899-12-30.
51 if (nYear == 1899 && nMonth == 12 && nDay == 30)
52 return nNullDateDays;
53
54 normalize(nDay, nMonth, nYear);
55 return convertDateToDays(nDay, nMonth, nYear);
56}
57
58bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
59{
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;
67}
68
69void convertDaysToDate(sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear)
70{
71 if (nDays <= MIN_DAYS)
72 {
73 rDay = 1;
74 rMonth = 1;
75 rYear = kYearMin;
76 return;
77 }
78 if (nDays >= MAX_DAYS)
79 {
80 rDay = 31;
81 rMonth = 12;
82 rYear = kYearMax;
83 return;
84 }
85
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;
91
92 do
93 {
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)
100 {
101 i += nSign;
102 bCalc = true;
103 }
104 else
105 {
106 if (nTempDays > 365)
107 {
108 if ((nTempDays != 366) || !isLeapYear(rYear))
109 {
110 i -= nSign;
111 bCalc = true;
112 }
113 }
114 }
115 } while (bCalc);
116
117 rMonth = 1;
118 while (nTempDays > getDaysInMonth(rMonth, rYear))
119 {
120 nTempDays -= getDaysInMonth(rMonth, rYear);
121 ++rMonth;
122 }
123
124 rDay = static_cast<sal_uInt16>(nTempDays);
125}
126
127bool normalize(sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear)
128{
129 if (isValidDate(rDay, rMonth, rYear))
130 return false;
131
132 if (rDay == 0 && rMonth == 0 && rYear == 0)
133 return false; // empty date
134
135 if (rDay == 0)
136 {
137 if (rMonth == 0)
138 ; // nothing, handled below
139 else
140 --rMonth;
141 // Last day of month is determined at the end.
142 }
143
144 if (rMonth > 12)
145 {
146 rYear += rMonth / 12;
147 rMonth = rMonth % 12;
148 if (rYear == 0)
149 rYear = 1;
150 }
151 if (rMonth == 0)
152 {
153 --rYear;
154 if (rYear == 0)
155 rYear = -1;
156 rMonth = 12;
157 }
158
159 if (rYear < 0)
160 {
161 sal_uInt16 nDays;
162 while (rDay > (nDays = getDaysInMonth(rMonth, rYear)))
163 {
164 rDay -= nDays;
165 if (rMonth > 1)
166 --rMonth;
167 else
168 {
169 if (rYear == kYearMin)
170 {
171 rDay = 1;
172 rMonth = 1;
173 return true;
174 }
175 --rYear;
176 rMonth = 12;
177 }
178 }
179 }
180 else
181 {
182 sal_uInt16 nDays;
183 while (rDay > (nDays = getDaysInMonth(rMonth, rYear)))
184 {
185 rDay -= nDays;
186 if (rMonth < 12)
187 ++rMonth;
188 else
189 {
190 if (rYear == kYearMax)
191 {
192 rDay = 31;
193 rMonth = 12;
194 return true;
195 }
196 ++rYear;
197 rMonth = 1;
198 }
199 }
200 }
201
202 if (rDay == 0)
203 rDay = getDaysInMonth(rMonth, rYear);
204
205 return true;
206}
207
208} // namespace comphelper::date
209
210/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
constexpr sal_uInt16 getDaysInMonth(sal_uInt16 nMonth, sal_Int16 nYear)
Get number of days in month of year.
Definition: date.hxx:67
constexpr sal_Int32 MAX_DAYS
Definition: date.cxx:40
constexpr bool isLeapYear(sal_Int16 nYear)
Whether year is a leap year.
Definition: date.hxx:54
constexpr sal_Int16 kYearMax
Definition: date.cxx:42
void convertDaysToDate(sal_Int32 nDays, sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
Obtain date for a days from zero value.
Definition: date.cxx:69
bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Whether date is a valid date.
Definition: date.cxx:58
constexpr sal_Int32 MIN_DAYS
Definition: date.cxx:39
constexpr sal_Int16 kYearMin
Definition: date.cxx:43
constexpr sal_Int32 nNullDateDays
Definition: date.cxx:45
constexpr sal_Int32 YearToDays(sal_Int16 nYear)
Days until start of year from zero, so month and day of month can be added.
Definition: date.hxx:37
constexpr sal_Int32 convertDateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Obtain days from zero for a given date, without normalizing.
Definition: date.hxx:82
bool normalize(sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
Normalize date, i.e.
Definition: date.cxx:127
sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Obtain days from zero for a given date, with normalizing.
Definition: date.cxx:48
int i
#define SAL_MIN_INT16
#define SAL_MAX_INT16