LibreOffice Module tools (master) 1
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Date Class Reference

Represents a date in the proleptic Gregorian calendar. More...

#include <date.hxx>

Inheritance diagram for Date:
[legend]

Public Types

enum  DateInitSystem { SYSTEM }
 
enum  DateInitEmpty { EMPTY }
 

Public Member Functions

 Date (DateInitEmpty)
 
 Date (DateInitSystem)
 
 Date (sal_Int32 nDate)
 
 Date (const Date &rDate)
 
 Date (sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
 nDay and nMonth both must be <100, nYear must be != 0 More...
 
 Date (const css::util::Date &rUDate)
 
 Date (const css::util::DateTime &_rDateTime)
 
bool IsEmpty () const
 
void SetDate (sal_Int32 nNewDate)
 
sal_Int32 GetDate () const
 
sal_uInt32 GetDateUnsigned () const
 Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. More...
 
css::util::Date GetUNODate () const
 
void SetDay (sal_uInt16 nNewDay)
 nNewDay must be <100 More...
 
void SetMonth (sal_uInt16 nNewMonth)
 nNewMonth must be <100 More...
 
void SetYear (sal_Int16 nNewYear)
 nNewYear must be != 0 More...
 
sal_uInt16 GetDay () const
 
sal_uInt16 GetMonth () const
 
sal_Int16 GetYear () const
 
sal_uInt16 GetYearUnsigned () const
 Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. More...
 
sal_Int16 GetNextYear () const
 
sal_Int16 GetPrevYear () const
 
void AddYears (sal_Int16 nAddYears)
 Add years skipping year 0 and truncating at limits. More...
 
void AddMonths (sal_Int32 nAddMonths)
 Add months skipping year 0 and truncating at limits. More...
 
void AddDays (sal_Int32 nAddDays)
 Add days skipping year 0 and truncating at limits. More...
 
DayOfWeek GetDayOfWeek () const
 Obtain the day of the week for the date. More...
 
sal_uInt16 GetDayOfYear () const
 Obtain the day of the year for the date. More...
 
sal_uInt16 GetWeekOfYear (DayOfWeek eStartDay=MONDAY, sal_Int16 nMinimumNumberOfDaysInWeek=4) const
 Obtain the week of the year for a date. More...
 
sal_uInt16 GetDaysInMonth () const
 Obtain the number of days in the month of the year of the date. More...
 
sal_uInt16 GetDaysInYear () const
 
bool IsLeapYear () const
 
bool IsValidAndGregorian () const
 If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year) AND is of the Gregorian calendar (1582-10-15 <= date) More...
 
bool IsValidDate () const
 If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year) More...
 
bool IsEndOfMonth () const
 
void Normalize ()
 Normalize date, invalid day or month values are adapted such that they carry over to the next month or/and year, for example 1999-02-32 becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes 2000-02-11. More...
 
bool IsBetween (const Date &rFrom, const Date &rTo) const
 
bool operator== (const Date &rDate) const
 
bool operator!= (const Date &rDate) const
 
bool operator> (const Date &rDate) const
 
bool operator< (const Date &rDate) const
 
bool operator>= (const Date &rDate) const
 
bool operator<= (const Date &rDate) const
 
Dateoperator= (const Date &rDate)
 
Dateoperator= (const css::util::Date &rUDate)
 
Dateoperator++ ()
 
Dateoperator-- ()
 

Static Public Member Functions

static sal_uInt16 GetDaysInMonth (sal_uInt16 nMonth, sal_Int16 nYear)
 Obtain number of days in a month of a year. More...
 
static sal_Int32 DateToDays (sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
 Internally normalizes values. More...
 
static bool IsValidDate (sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
 Semantically identical to IsValidDate() member method. More...
 
static bool IsEndOfMonth (sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
 Semantically identical to IsEndOfMonth() member method. More...
 
static bool Normalize (sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
 Semantically identical to Normalize() member method. More...
 

Private Member Functions

void setDateFromDMY (sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
 
sal_Int32 GetAsNormalizedDays () const
 An accelerated form of DateToDays on this date. More...
 

Private Attributes

sal_Int32 mnDate
 

Friends

TOOLS_DLLPUBLIC friend Date operator+ (const Date &rDate, sal_Int32 nDays)
 
TOOLS_DLLPUBLIC friend Date operator- (const Date &rDate, sal_Int32 nDays)
 
TOOLS_DLLPUBLIC friend sal_Int32 operator- (const Date &rDate1, const Date &rDate2)
 

Detailed Description

Represents a date in the proleptic Gregorian calendar.

Largest representable date is 32767-12-31 = 327671231

Smallest representable date is -32768-01-01 = -327680101

Due to possible conversions to css::util::Date, which has a short Year member variable, these limits are fix.

Year value 0 is unused. The year before year 1 CE is year 1 BCE, which is the traditional proleptic Gregorian calendar.

This is not how ISO 8601:2000 defines things (but ISO 8601:1998 Draft Revision did), but it enables class Date to be used for writing XML files as XML Schema Part 2 in D.3.2 No Year Zero says "The year "0000" is an illegal year value.", see https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#noYearZero and furthermore the note for 3.2.7 dateTime https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#dateTime

Definition at line 54 of file date.hxx.

Member Enumeration Documentation

◆ DateInitEmpty

Enumerator
EMPTY 

Definition at line 66 of file date.hxx.

◆ DateInitSystem

Enumerator
SYSTEM 

Definition at line 61 of file date.hxx.

Constructor & Destructor Documentation

◆ Date() [1/7]

Date::Date ( DateInitEmpty  )
inlineexplicit

◆ Date() [2/7]

Date::Date ( DateInitSystem  )
explicit

Definition at line 104 of file tdate.cxx.

References GetSystemDateTime(), mnDate, and setDateFromDMY().

◆ Date() [3/7]

Date::Date ( sal_Int32  nDate)
inlineexplicit

Definition at line 73 of file date.hxx.

◆ Date() [4/7]

Date::Date ( const Date rDate)
inline

Definition at line 74 of file date.hxx.

◆ Date() [5/7]

Date::Date ( sal_uInt16  nDay,
sal_uInt16  nMonth,
sal_Int16  nYear 
)
inline

nDay and nMonth both must be <100, nYear must be != 0

Definition at line 77 of file date.hxx.

◆ Date() [6/7]

Date::Date ( const css::util::Date &  rUDate)
inline

Definition at line 80 of file date.hxx.

◆ Date() [7/7]

Date::Date ( const css::util::DateTime &  _rDateTime)

Definition at line 110 of file tdate.cxx.

References setDateFromDMY().

Member Function Documentation

◆ AddDays()

void Date::AddDays ( sal_Int32  nAddDays)

Add days skipping year 0 and truncating at limits.

Definition at line 375 of file tdate.cxx.

References GetAsNormalizedDays(), and lcl_DaysToDate().

Referenced by DateTime::CreateFromUnixTime(), DateTime::CreateFromWin32FileDateTime(), DateTime::NormalizeTimeRemainderAndApply(), and DateTime::operator+=().

◆ AddMonths()

void Date::AddMonths ( sal_Int32  nAddMonths)

Add months skipping year 0 and truncating at limits.

If the original date was on Feb-29 or day 31 and the resulting date is not a leap year or a month with fewer days, the result is adjusted to Feb-28 or day 30.

Definition at line 172 of file tdate.cxx.

References GetMonth(), GetYear(), kYearMax, kYearMin, Normalize(), SetMonth(), and SetYear().

◆ AddYears()

void Date::AddYears ( sal_Int16  nAddYears)

Add years skipping year 0 and truncating at limits.

If the original date was on Feb-29 and the resulting date is not a leap year, the result is adjusted to Feb-28.

Definition at line 131 of file tdate.cxx.

References GetDay(), GetMonth(), GetYear(), comphelper::date::isLeapYear(), kYearMax, kYearMin, SetDay(), and SetYear().

◆ DateToDays()

sal_Int32 Date::DateToDays ( sal_uInt16  nDay,
sal_uInt16  nMonth,
sal_Int16  nYear 
)
static

Internally normalizes values.

Definition at line 90 of file tdate.cxx.

References comphelper::date::convertDateToDaysNormalizing().

Referenced by GetAsNormalizedDays().

◆ GetAsNormalizedDays()

sal_Int32 Date::GetAsNormalizedDays ( ) const
private

An accelerated form of DateToDays on this date.

Definition at line 70 of file tdate.cxx.

References comphelper::date::convertDateToDays(), DateToDays(), GetDay(), GetMonth(), GetYear(), mnDate, and comphelper::date::normalize().

Referenced by AddDays(), GetDayOfWeek(), GetWeekOfYear(), operator++(), and operator--().

◆ GetDate()

sal_Int32 Date::GetDate ( ) const
inline

Definition at line 89 of file date.hxx.

◆ GetDateUnsigned()

sal_uInt32 Date::GetDateUnsigned ( ) const
inline

Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM.

Definition at line 91 of file date.hxx.

◆ GetDay()

sal_uInt16 Date::GetDay ( ) const
inline

◆ GetDayOfWeek()

DayOfWeek Date::GetDayOfWeek ( ) const

Obtain the day of the week for the date.

Internally normalizes a copy of values. The result may be unexpected for a non-normalized invalid date like Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);

Definition at line 192 of file tdate.cxx.

References GetAsNormalizedDays().

Referenced by GetWeekOfYear().

◆ GetDayOfYear()

sal_uInt16 Date::GetDayOfYear ( ) const

Obtain the day of the year for the date.

Internally normalizes a copy of values. The result may be unexpected for a non-normalized invalid date like Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);

Definition at line 197 of file tdate.cxx.

References GetDay(), comphelper::date::getDaysInMonth(), GetMonth(), GetYear(), i, and Normalize().

Referenced by GetWeekOfYear().

◆ GetDaysInMonth() [1/2]

sal_uInt16 Date::GetDaysInMonth ( ) const

Obtain the number of days in the month of the year of the date.

Internally normalizes a copy of values.

The result may be unexpected for a non-normalized invalid date like Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);

These would result in 31 as –11-31 rolls over to –12-01 and the number of days in December is returned.

Instead, to obtain the value for the actual set use the static method Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.

Definition at line 295 of file tdate.cxx.

References GetDay(), comphelper::date::getDaysInMonth(), GetMonth(), GetYear(), and Normalize().

◆ GetDaysInMonth() [2/2]

sal_uInt16 Date::GetDaysInMonth ( sal_uInt16  nMonth,
sal_Int16  nYear 
)
static

Obtain number of days in a month of a year.

Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not normalize values.

Definition at line 60 of file tdate.cxx.

References comphelper::date::getDaysInMonth(), and SAL_WARN_IF.

◆ GetDaysInYear()

sal_uInt16 Date::GetDaysInYear ( ) const
inline

Definition at line 178 of file date.hxx.

References IsLeapYear.

Referenced by GetWeekOfYear().

◆ GetMonth()

sal_uInt16 Date::GetMonth ( ) const
inline

◆ GetNextYear()

sal_Int16 Date::GetNextYear ( ) const
inline

Definition at line 116 of file date.hxx.

Referenced by GetWeekOfYear().

◆ GetPrevYear()

sal_Int16 Date::GetPrevYear ( ) const
inline

Definition at line 117 of file date.hxx.

Referenced by GetWeekOfYear().

◆ GetUNODate()

css::util::Date Date::GetUNODate ( ) const
inline

Definition at line 92 of file date.hxx.

◆ GetWeekOfYear()

sal_uInt16 Date::GetWeekOfYear ( DayOfWeek  eStartDay = MONDAY,
sal_Int16  nMinimumNumberOfDaysInWeek = 4 
) const

Obtain the week of the year for a date.

Parameters
nMinimumNumberOfDaysInWeekHow many days of a week must reside in the first week of a year.

Internally normalizes a copy of values. The result may be unexpected for a non-normalized invalid date like Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);

Definition at line 209 of file tdate.cxx.

References Date(), GetAsNormalizedDays(), GetDayOfWeek(), GetDayOfYear(), GetDaysInYear(), GetNextYear(), GetPrevYear(), GetWeekOfYear(), GetYear(), IsLeapYear(), lcl_DaysToDate(), and SAL_WARN.

Referenced by GetWeekOfYear().

◆ GetYear()

sal_Int16 Date::GetYear ( ) const
inline

◆ GetYearUnsigned()

sal_uInt16 Date::GetYearUnsigned ( ) const
inline

Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM.

Definition at line 115 of file date.hxx.

◆ IsBetween()

bool Date::IsBetween ( const Date rFrom,
const Date rTo 
) const
inline

Definition at line 206 of file date.hxx.

References mnDate.

◆ IsEmpty()

bool Date::IsEmpty ( ) const
inline

Definition at line 86 of file date.hxx.

◆ IsEndOfMonth() [1/2]

bool Date::IsEndOfMonth ( ) const

Definition at line 345 of file tdate.cxx.

References GetDay(), GetMonth(), GetYear(), and IsEndOfMonth().

Referenced by IsEndOfMonth().

◆ IsEndOfMonth() [2/2]

bool Date::IsEndOfMonth ( sal_uInt16  nDay,
sal_uInt16  nMonth,
sal_Int16  nYear 
)
static

Semantically identical to IsEndOfMonth() member method.

Definition at line 351 of file tdate.cxx.

References comphelper::date::getDaysInMonth(), and comphelper::date::isValidDate().

◆ IsLeapYear()

bool Date::IsLeapYear ( ) const

Definition at line 305 of file tdate.cxx.

References GetYear(), and comphelper::date::isLeapYear().

Referenced by GetWeekOfYear().

◆ IsValidAndGregorian()

bool Date::IsValidAndGregorian ( ) const

If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year) AND is of the Gregorian calendar (1582-10-15 <= date)

Definition at line 311 of file tdate.cxx.

References GetDay(), comphelper::date::getDaysInMonth(), GetMonth(), and GetYear().

◆ IsValidDate() [1/2]

bool Date::IsValidDate ( ) const

If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year)

Definition at line 334 of file tdate.cxx.

References GetDay(), GetMonth(), GetYear(), and comphelper::date::isValidDate().

◆ IsValidDate() [2/2]

bool Date::IsValidDate ( sal_uInt16  nDay,
sal_uInt16  nMonth,
sal_Int16  nYear 
)
static

Semantically identical to IsValidDate() member method.

Definition at line 340 of file tdate.cxx.

References comphelper::date::isValidDate().

◆ Normalize() [1/2]

void Date::Normalize ( )

Normalize date, invalid day or month values are adapted such that they carry over to the next month or/and year, for example 1999-02-32 becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes 2000-02-11.

Truncates at -32768-01-01 or 32767-12-31, 0001-00-x will yield the normalized value of -0001-12-x

This may be necessary after Date ctors or if the SetDate(), SetDay(), SetMonth(), SetYear() methods set individual non-matching values. Adding/subtracting to/from dates never produces invalid dates.

Definition at line 357 of file tdate.cxx.

References GetDay(), GetMonth(), GetYear(), Normalize(), and setDateFromDMY().

Referenced by AddMonths(), GetDayOfYear(), GetDaysInMonth(), and Normalize().

◆ Normalize() [2/2]

bool Date::Normalize ( sal_uInt16 &  rDay,
sal_uInt16 &  rMonth,
sal_Int16 &  rYear 
)
static

Semantically identical to Normalize() member method.

Definition at line 370 of file tdate.cxx.

References comphelper::date::normalize().

◆ operator!=()

bool Date::operator!= ( const Date rDate) const
inline

Definition at line 212 of file date.hxx.

References mnDate.

◆ operator++()

Date & Date::operator++ ( void  )

Definition at line 381 of file tdate.cxx.

References GetAsNormalizedDays(), and lcl_DaysToDate().

◆ operator--()

Date & Date::operator-- ( )

Definition at line 387 of file tdate.cxx.

References GetAsNormalizedDays(), and lcl_DaysToDate().

Referenced by DateTime::NormalizeTimeRemainderAndApply().

◆ operator<()

bool Date::operator< ( const Date rDate) const
inline

Definition at line 216 of file date.hxx.

References mnDate.

◆ operator<=()

bool Date::operator<= ( const Date rDate) const
inline

Definition at line 220 of file date.hxx.

References mnDate.

◆ operator=() [1/2]

Date & Date::operator= ( const css::util::Date &  rUDate)
inline

Definition at line 225 of file date.hxx.

◆ operator=() [2/2]

Date & Date::operator= ( const Date rDate)
inline

Definition at line 223 of file date.hxx.

References mnDate.

Referenced by DateTime::DateTime(), and DateTime::operator=().

◆ operator==()

bool Date::operator== ( const Date rDate) const
inline

Definition at line 210 of file date.hxx.

References mnDate.

Referenced by DateTime::operator<(), DateTime::operator<=(), DateTime::operator>(), and DateTime::operator>=().

◆ operator>()

bool Date::operator> ( const Date rDate) const
inline

Definition at line 214 of file date.hxx.

References mnDate.

◆ operator>=()

bool Date::operator>= ( const Date rDate) const
inline

Definition at line 218 of file date.hxx.

References mnDate.

◆ SetDate()

void Date::SetDate ( sal_Int32  nNewDate)

Definition at line 53 of file tdate.cxx.

References mnDate.

◆ setDateFromDMY()

void Date::setDateFromDMY ( sal_uInt16  nDay,
sal_uInt16  nMonth,
sal_Int16  nYear 
)
private

Definition at line 34 of file tdate.cxx.

References mnDate, and SAL_WARN_IF.

Referenced by Date(), Normalize(), SetDay(), SetMonth(), and SetYear().

◆ SetDay()

void Date::SetDay ( sal_uInt16  nNewDay)

nNewDay must be <100

Definition at line 115 of file tdate.cxx.

References GetMonth(), GetYear(), and setDateFromDMY().

Referenced by AddYears().

◆ SetMonth()

void Date::SetMonth ( sal_uInt16  nNewMonth)

nNewMonth must be <100

Definition at line 120 of file tdate.cxx.

References GetDay(), GetYear(), and setDateFromDMY().

Referenced by AddMonths().

◆ SetYear()

void Date::SetYear ( sal_Int16  nNewYear)

nNewYear must be != 0

Definition at line 125 of file tdate.cxx.

References GetDay(), GetMonth(), and setDateFromDMY().

Referenced by AddMonths(), and AddYears().

Friends And Related Function Documentation

◆ operator+

TOOLS_DLLPUBLIC friend Date operator+ ( const Date rDate,
sal_Int32  nDays 
)
friend

Definition at line 393 of file tdate.cxx.

◆ operator- [1/2]

TOOLS_DLLPUBLIC friend Date operator- ( const Date rDate,
sal_Int32  nDays 
)
friend

Definition at line 400 of file tdate.cxx.

◆ operator- [2/2]

TOOLS_DLLPUBLIC friend sal_Int32 operator- ( const Date rDate1,
const Date rDate2 
)
friend

Definition at line 407 of file tdate.cxx.

Member Data Documentation

◆ mnDate

sal_Int32 Date::mnDate
private

The documentation for this class was generated from the following files: