LibreOffice Module tools (master) 1
date.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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#ifndef INCLUDED_TOOLS_DATE_HXX
20#define INCLUDED_TOOLS_DATE_HXX
21
22#include <tools/toolsdllapi.h>
23
24#include <ostream>
25
26#include <com/sun/star/util/Date.hpp>
27
28namespace com::sun::star::util { struct DateTime; }
29
32
55{
56private:
57 sal_Int32 mnDate;
58 void setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear );
59
60public:
62 {
63 SYSTEM
64 };
65
67 {
68 EMPTY
69 };
70
71 explicit Date( DateInitEmpty ) : mnDate(0) {}
72 explicit Date( DateInitSystem );
73 explicit Date( sal_Int32 nDate ) : mnDate(nDate) {}
74 Date( const Date& rDate ) : mnDate(rDate.mnDate) {}
75
77 Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
78 { setDateFromDMY(nDay, nMonth, nYear); }
79
80 Date( const css::util::Date& rUDate )
81 {
82 setDateFromDMY(rUDate.Day, rUDate.Month, rUDate.Year);
83 }
84 Date( const css::util::DateTime& _rDateTime );
85
86 bool IsEmpty() const { return mnDate == 0; }
87
88 void SetDate( sal_Int32 nNewDate );
89 sal_Int32 GetDate() const { return mnDate; }
91 sal_uInt32 GetDateUnsigned() const { return static_cast<sal_uInt32>(mnDate < 0 ? -mnDate : mnDate); }
92 css::util::Date GetUNODate() const { return css::util::Date(GetDay(), GetMonth(), GetYear()); }
93
95 void SetDay( sal_uInt16 nNewDay );
97 void SetMonth( sal_uInt16 nNewMonth );
99 void SetYear( sal_Int16 nNewYear );
100
101 sal_uInt16 GetDay() const
102 {
103 return mnDate < 0 ?
104 static_cast<sal_uInt16>(-mnDate % 100) :
105 static_cast<sal_uInt16>( mnDate % 100);
106 }
107 sal_uInt16 GetMonth() const
108 {
109 return mnDate < 0 ?
110 static_cast<sal_uInt16>((-mnDate / 100) % 100) :
111 static_cast<sal_uInt16>(( mnDate / 100) % 100);
112 }
113 sal_Int16 GetYear() const { return static_cast<sal_Int16>(mnDate / 10000); }
115 sal_uInt16 GetYearUnsigned() const { return static_cast<sal_uInt16>((mnDate < 0 ? -mnDate : mnDate) / 10000); }
116 sal_Int16 GetNextYear() const { sal_Int16 nY = GetYear(); return nY == -1 ? 1 : nY + 1; }
117 sal_Int16 GetPrevYear() const { sal_Int16 nY = GetYear(); return nY == 1 ? -1 : nY - 1; }
118
123 void AddYears( sal_Int16 nAddYears );
124
129 void AddMonths( sal_Int32 nAddMonths );
130
133 void AddDays( sal_Int32 nAddDays );
134
141 DayOfWeek GetDayOfWeek() const;
142
149 sal_uInt16 GetDayOfYear() const;
150
160 sal_uInt16 GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
161 sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
162
176 sal_uInt16 GetDaysInMonth() const;
177
178 sal_uInt16 GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
179 bool IsLeapYear() const;
180
185 bool IsValidAndGregorian() const;
186
189 bool IsValidDate() const;
190
191 // Returns true, if the date is the end of the month, false otherwise.
192 bool IsEndOfMonth() const;
193
204 void Normalize();
205
206 bool IsBetween( const Date& rFrom, const Date& rTo ) const
207 { return ((mnDate >= rFrom.mnDate) &&
208 (mnDate <= rTo.mnDate)); }
209
210 bool operator ==( const Date& rDate ) const
211 { return (mnDate == rDate.mnDate); }
212 bool operator !=( const Date& rDate ) const
213 { return (mnDate != rDate.mnDate); }
214 bool operator >( const Date& rDate ) const
215 { return (mnDate > rDate.mnDate); }
216 bool operator <( const Date& rDate ) const
217 { return (mnDate < rDate.mnDate); }
218 bool operator >=( const Date& rDate ) const
219 { return (mnDate >= rDate.mnDate); }
220 bool operator <=( const Date& rDate ) const
221 { return (mnDate <= rDate.mnDate); }
222
223 Date& operator =( const Date& rDate )
224 { mnDate = rDate.mnDate; return *this; }
225 Date& operator =( const css::util::Date& rUDate )
226 { setDateFromDMY( rUDate.Day, rUDate.Month, rUDate.Year); return *this; }
227 Date& operator ++();
228 Date& operator --();
229
230 TOOLS_DLLPUBLIC friend Date operator +( const Date& rDate, sal_Int32 nDays );
231 TOOLS_DLLPUBLIC friend Date operator -( const Date& rDate, sal_Int32 nDays );
232 TOOLS_DLLPUBLIC friend sal_Int32 operator -( const Date& rDate1, const Date& rDate2 );
233
239 static sal_uInt16 GetDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear );
240
242 static sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear );
244 static bool IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear );
246 static bool IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear);
248 static bool Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear );
249
250 private:
252 sal_Int32 GetAsNormalizedDays() const;
253};
254
255TOOLS_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const Date& rDate);
256
257#endif
258
259/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:806
bool operator<(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:818
Represents a date in the proleptic Gregorian calendar.
Definition: date.hxx:55
sal_Int32 GetDate() const
Definition: date.hxx:89
Date(DateInitEmpty)
Definition: date.hxx:71
sal_uInt16 GetDaysInYear() const
Definition: date.hxx:178
Date(sal_Int32 nDate)
Definition: date.hxx:73
Date(const Date &rDate)
Definition: date.hxx:74
sal_Int16 GetNextYear() const
Definition: date.hxx:116
css::util::Date GetUNODate() const
Definition: date.hxx:92
DateInitEmpty
Definition: date.hxx:67
bool IsBetween(const Date &rFrom, const Date &rTo) const
Definition: date.hxx:206
Date(const css::util::Date &rUDate)
Definition: date.hxx:80
Date(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
nDay and nMonth both must be <100, nYear must be != 0
Definition: date.hxx:77
sal_Int16 GetYear() const
Definition: date.hxx:113
sal_uInt16 GetDay() const
Definition: date.hxx:101
sal_Int16 GetPrevYear() const
Definition: date.hxx:117
bool IsEmpty() const
Definition: date.hxx:86
sal_uInt32 GetDateUnsigned() const
Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM.
Definition: date.hxx:91
sal_uInt16 GetYearUnsigned() const
Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM.
Definition: date.hxx:115
sal_Int32 mnDate
Definition: date.hxx:57
sal_uInt16 GetMonth() const
Definition: date.hxx:107
DateInitSystem
Definition: date.hxx:62
DayOfWeek
Definition: date.hxx:30
@ SATURDAY
Definition: date.hxx:31
@ TUESDAY
Definition: date.hxx:30
@ WEDNESDAY
Definition: date.hxx:30
@ FRIDAY
Definition: date.hxx:30
@ THURSDAY
Definition: date.hxx:30
@ MONDAY
Definition: date.hxx:30
@ SUNDAY
Definition: date.hxx:31
TOOLS_DLLPUBLIC std::ostream & operator<<(std::ostream &os, const Date &rDate)
DateTime operator-(const DateTime &rDateTime, sal_Int32 nDays)
Definition: datetime.cxx:154
DateTime operator+(const DateTime &rDateTime, sal_Int32 nDays)
Definition: datetime.cxx:147
bool operator>(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:377
bool operator>=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:350
bool operator!=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:340
bool operator<=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:345
sal_Int16 GetDayOfWeek(sal_Int32 nDate)
constexpr OUStringLiteral EMPTY
const char DateToDays[]
const char IsLeapYear[]
#define TOOLS_DLLPUBLIC
Definition: toolsdllapi.h:28
#define SAL_WARN_UNUSED