Line data Source code
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 _DATE_HXX
20 : #define _DATE_HXX
21 :
22 : #include "tools/toolsdllapi.h"
23 : #include <tools/solar.h>
24 :
25 : class ResId;
26 :
27 : enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
28 : SATURDAY, SUNDAY };
29 :
30 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Date
31 : {
32 : private:
33 : sal_uInt32 nDate;
34 :
35 : public:
36 : enum DateInitSystem
37 : {
38 : SYSTEM
39 : };
40 :
41 : // TODO temporary until all uses are inspected and resolved
42 : enum DateInitEmpty
43 : {
44 : EMPTY
45 : };
46 :
47 7693 : Date( DateInitEmpty)
48 7693 : { nDate = 0; }
49 : Date( DateInitSystem );
50 : Date( const ResId & rResId );
51 43 : Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
52 5457 : Date( const Date& rDate )
53 5457 : { nDate = rDate.nDate; }
54 3586 : Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
55 : { nDate = ( sal_uInt32( nDay % 100 ) ) +
56 : ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
57 3586 : ( ( sal_uInt32( nYear % 10000 ) ) * 10000); }
58 :
59 0 : void SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
60 119 : sal_uInt32 GetDate() const { return nDate; }
61 :
62 : void SetDay( sal_uInt16 nNewDay );
63 : void SetMonth( sal_uInt16 nNewMonth );
64 : void SetYear( sal_uInt16 nNewYear );
65 331 : sal_uInt16 GetDay() const { return (sal_uInt16)(nDate % 100); }
66 363 : sal_uInt16 GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
67 647 : sal_uInt16 GetYear() const { return (sal_uInt16)(nDate / 10000); }
68 :
69 : /// Internally normalizes a copy of values.
70 : DayOfWeek GetDayOfWeek() const;
71 :
72 : /// Internally normalizes a copy of values.
73 : sal_uInt16 GetDayOfYear() const;
74 :
75 : /** nMinimumNumberOfDaysInWeek: how many days of a week must reside in the
76 : first week of a year.
77 : Internally normalizes a copy of values. */
78 : sal_uInt16 GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
79 : sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
80 :
81 : /// Internally normalizes a copy of values.
82 : sal_uInt16 GetDaysInMonth() const;
83 :
84 : sal_uInt16 GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
85 : sal_Bool IsLeapYear() const;
86 :
87 : /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
88 : depending on month/year) AND is of the Gregorian calendar (1582-10-15
89 : <= date) (AND implicitly date <= 9999-12-31 due to internal
90 : representation) */
91 : sal_Bool IsValidAndGregorian() const;
92 :
93 : /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
94 : depending on month/year) */
95 : bool IsValidDate() const;
96 :
97 : /** Normalize date, invalid day or month values are adapted such that they
98 : carry over to the next month or/and year, for example 1999-02-32
99 : becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes
100 : 2000-02-11. Truncates at 9999-12-31, 0000-00-x will yield the
101 : normalized value of 0000-01-max(1,(x-31))
102 :
103 : This may be necessary after Date ctors or if the SetDate(), SetDay(),
104 : SetMonth(), SetYear() methods set individual non-matching values.
105 : Adding/subtracting to/from dates never produces invalid dates.
106 :
107 : @returns TRUE if the date was normalized, i.e. not valid before.
108 : */
109 : bool Normalize();
110 :
111 0 : sal_Bool IsBetween( const Date& rFrom, const Date& rTo ) const
112 : { return ((nDate >= rFrom.nDate) &&
113 0 : (nDate <= rTo.nDate)); }
114 :
115 169 : sal_Bool operator ==( const Date& rDate ) const
116 169 : { return (nDate == rDate.nDate); }
117 0 : sal_Bool operator !=( const Date& rDate ) const
118 0 : { return (nDate != rDate.nDate); }
119 0 : sal_Bool operator >( const Date& rDate ) const
120 0 : { return (nDate > rDate.nDate); }
121 0 : sal_Bool operator <( const Date& rDate ) const
122 0 : { return (nDate < rDate.nDate); }
123 0 : sal_Bool operator >=( const Date& rDate ) const
124 0 : { return (nDate >= rDate.nDate); }
125 0 : sal_Bool operator <=( const Date& rDate ) const
126 0 : { return (nDate <= rDate.nDate); }
127 :
128 4311 : Date& operator =( const Date& rDate )
129 4311 : { nDate = rDate.nDate; return *this; }
130 : Date& operator +=( long nDays );
131 : Date& operator -=( long nDays );
132 : Date& operator ++();
133 : Date& operator --();
134 : Date operator ++( int );
135 : Date operator --( int );
136 :
137 : TOOLS_DLLPUBLIC friend Date operator +( const Date& rDate, long nDays );
138 : TOOLS_DLLPUBLIC friend Date operator -( const Date& rDate, long nDays );
139 : TOOLS_DLLPUBLIC friend long operator -( const Date& rDate1, const Date& rDate2 );
140 :
141 : /// Internally normalizes values.
142 : static long DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
143 : /// Semantically identical to IsValidDate() member method.
144 : static bool IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
145 : /// Semantically identical to Normalize() member method.
146 : static bool Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear );
147 :
148 : };
149 :
150 : #endif
151 :
152 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|