Branch data 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 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 : 34355 : Date( DateInitEmpty)
48 : 34355 : { nDate = 0; }
49 : : Date( DateInitSystem );
50 : : Date( const ResId & rResId );
51 : 564 : Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
52 : 86116 : Date( const Date& rDate )
53 : 86116 : { nDate = rDate.nDate; }
54 : 48659 : Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
55 : : { nDate = ( sal_uInt32( nDay % 100 ) ) +
56 : : ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
57 : 48659 : ( ( sal_uInt32( nYear % 10000 ) ) * 10000); }
58 : :
59 : 0 : void SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
60 : 4906 : 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 : 2587 : sal_uInt16 GetDay() const { return (sal_uInt16)(nDate % 100); }
66 : 2679 : sal_uInt16 GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
67 : 4481 : 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 : : sal_Bool IsBetween( const Date& rFrom, const Date& rTo ) const
112 : : { return ((nDate >= rFrom.nDate) &&
113 : : (nDate <= rTo.nDate)); }
114 : :
115 : 473 : sal_Bool operator ==( const Date& rDate ) const
116 : 473 : { return (nDate == rDate.nDate); }
117 : 34 : sal_Bool operator !=( const Date& rDate ) const
118 : 34 : { return (nDate != rDate.nDate); }
119 : 521 : sal_Bool operator >( const Date& rDate ) const
120 : 521 : { return (nDate > rDate.nDate); }
121 : 437 : sal_Bool operator <( const Date& rDate ) const
122 : 437 : { 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 : 78875 : Date& operator =( const Date& rDate )
129 : 78875 : { nDate = rDate.nDate; return *this; }
130 : : Date& operator +=( long nDays );
131 : : Date& operator -=( long nDays );
132 : : Date& operator ++();
133 : : Date& operator --();
134 : : #ifndef MPW33
135 : : Date operator ++( int );
136 : : Date operator --( int );
137 : : #endif
138 : :
139 : : TOOLS_DLLPUBLIC friend Date operator +( const Date& rDate, long nDays );
140 : : TOOLS_DLLPUBLIC friend Date operator -( const Date& rDate, long nDays );
141 : : TOOLS_DLLPUBLIC friend long operator -( const Date& rDate1, const Date& rDate2 );
142 : :
143 : : /// Internally normalizes values.
144 : : static long DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
145 : : /// Semantically identical to IsValidDate() member method.
146 : : static bool IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
147 : : /// Semantically identical to Normalize() member method.
148 : : static bool Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear );
149 : :
150 : : };
151 : :
152 : : #endif
153 : :
154 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|