cxWidgets 1.0
Public Member Functions | Static Public Member Functions | Friends | List of all members
cx::cxDate Class Reference

Enables easy management of dates. C++17-enhanced version of the original 'date' class. More...

#include <cxDate.h>

Collaboration diagram for cx::cxDate:
Collaboration graph

Public Member Functions

 cxDate (eDateFormats pDateFormat=YYYY_MM_DD, char pSepChar='-')
 
 cxDate (int pYear, int pMonth, int pDay, eDateFormats pDateFormat=YYYY_MM_DD, char pSepChar='-') noexcept
 
 cxDate (std::string_view pDateStr, eDateFormats pDateFormat=YYYY_MM_DD, char pSepChar='-')
 
 cxDate (std::string_view pDateStr, char pSepChar)
 
 cxDate (long pJulianDate, eDateFormats pDateFormat=YYYY_MM_DD, char pSepChar='-') noexcept
 
virtual ~cxDate ()=default
 
 cxDate (const cxDate &)=default
 
cxDateoperator= (const cxDate &)=default
 
int getYear () const noexcept
 
int getMonth () const noexcept
 
int getDay () const noexcept
 
eDateFormats getDateFormat () const noexcept
 
char getSepChar () const noexcept
 
bool getDisplayLong () const noexcept
 
void setYear (int pYear) noexcept
 
void setMonth (int pMonth) noexcept
 
void setDay (int pDay) noexcept
 
void setDateFormat (eDateFormats pDateFormat) noexcept
 
void setSepChar (char pSepChar) noexcept
 
void setDisplayLong (bool pDisplay) noexcept
 
bool setDate (int pYear, int pMonth, int pDay) noexcept
 
void addYears (int pNumYears) noexcept
 
void subtractYears (int pNumYears) noexcept
 
void addMonths (int pNumMonths) noexcept
 
void subtractMonths (int pNumMonths) noexcept
 
void addDays (int pNumDays) noexcept
 
void subtractDays (int pNumDays) noexcept
 
void operator+= (int pNumDays) noexcept
 
void operator-= (int pNumDays) noexcept
 
bool operator== (const cxDate &pThat) const noexcept
 
bool operator!= (const cxDate &pThat) const noexcept
 
bool operator> (const cxDate &pThat) const noexcept
 
bool operator>= (const cxDate &pThat) const noexcept
 
bool operator< (const cxDate &pThat) const noexcept
 
bool operator<= (const cxDate &pThat) const noexcept
 
cxDate operator++ (int) noexcept
 
cxDateoperator++ () noexcept
 
cxDate operator-- (int) noexcept
 
cxDateoperator-- () noexcept
 
cxDate operator+ (int pNumDays) const noexcept
 
cxDate operator- (int pNumDays) const noexcept
 
int operator- (const cxDate &pThat) const noexcept
 
std::string toString () const
 
bool fromString (std::string_view pDateStr)
 
int numMonthDays () const noexcept
 
bool isLeapYear () const noexcept
 
eWeekDays dayOfWeek () const noexcept
 
std::string dayOfWeekStr () const
 
long toJulian () const noexcept
 
void fromJulian (long pJulianDate) noexcept
 

Static Public Member Functions

static int numMonthDays (int pMonth, int pYear) noexcept
 
static bool isLeapYear (int pYear) noexcept
 
static eWeekDays dayOfWeek (int pYear, int pMonth, int pDay) noexcept
 
static std::string dayOfWeekStr (eWeekDays pWeekDay)
 
static std::string dayOfWeekStr (int pYear, int pMonth, int pDay)
 
static std::string monthName (int pMonth, bool pAbbreviated=false)
 
static std::string getToday (bool pLong=false)
 
static int getCurrentYear () noexcept
 
static cxDate today () noexcept
 
static std::optional< cxDatetryParse (std::string_view pDateStr, eDateFormats pDateFormat=YYYY_MM_DD, char pSepChar='-')
 
static long toJulian (int pYear, int pMonth, int pDay) noexcept
 
static void fromJulian (long pJulianDate, int &pYear, int &pMonth, int &pDay) noexcept
 
static eDateFormats getDateFormat (std::string_view pDateStr)
 
static std::string getDateFormatStr (eDateFormats pDateFormat)
 
static void monthCalendar (int pYear, int pMonth, std::ostream &pOutStream=std::cout, int pTrailingEndlines=2)
 
static void yearCalendar (int pYear, std::ostream &pOutStream=std::cout, int pTrailingEndlines=2)
 

Friends

std::ostream & operator<< (std::ostream &pOS, const cxDate &pDate)
 
std::istream & operator>> (std::istream &pIS, cxDate &pDate)
 

Detailed Description

Enables easy management of dates. C++17-enhanced version of the original 'date' class.

Note: Months and days are 1-based (months 1-12, days 1-31). Julian date calculations assume noon for time of day, so Julian dates are whole numbers.

Constructor & Destructor Documentation

◆ cxDate() [1/6]

cx::cxDate::cxDate ( eDateFormats  pDateFormat = YYYY_MM_DD,
char  pSepChar = '-' 
)
explicit

Default constructor - initializes to today's date.

◆ cxDate() [2/6]

cx::cxDate::cxDate ( int  pYear,
int  pMonth,
int  pDay,
eDateFormats  pDateFormat = YYYY_MM_DD,
char  pSepChar = '-' 
)
noexcept

Constructor with explicit year, month, day.

◆ cxDate() [3/6]

cx::cxDate::cxDate ( std::string_view  pDateStr,
eDateFormats  pDateFormat = YYYY_MM_DD,
char  pSepChar = '-' 
)

Constructor from a date string (e.g. "2026-03-16").

References fromString().

◆ cxDate() [4/6]

cx::cxDate::cxDate ( std::string_view  pDateStr,
char  pSepChar 
)

Constructor from a date string with auto-detected format.

References fromString(), getDateFormat(), and cx::UNKNOWN.

◆ cxDate() [5/6]

cx::cxDate::cxDate ( long  pJulianDate,
eDateFormats  pDateFormat = YYYY_MM_DD,
char  pSepChar = '-' 
)
noexcept

Constructor from a Julian date number.

◆ ~cxDate()

virtual cx::cxDate::~cxDate ( )
virtualdefault

◆ cxDate() [6/6]

cx::cxDate::cxDate ( const cxDate )
default

Member Function Documentation

◆ addDays()

void cx::cxDate::addDays ( int  pNumDays)
noexcept

Adds pNumDays days to the date.

References cx::CXDATE_END_MONTH, cx::CXDATE_START_DAY, and cx::CXDATE_START_MONTH.

Referenced by operator+(), operator++(), and test_Arithmetic().

◆ addMonths()

void cx::cxDate::addMonths ( int  pNumMonths)
noexcept

Adds pNumMonths months to the date, adjusting the year as needed.

References cx::CXDATE_END_MONTH, cx::CXDATE_MONTHS_IN_YEAR, and cx::CXDATE_START_MONTH.

Referenced by test_Arithmetic().

◆ addYears()

void cx::cxDate::addYears ( int  pNumYears)
noexcept

Adds pNumYears years to the date.

◆ dayOfWeek() [1/2]

eWeekDays cx::cxDate::dayOfWeek ( ) const
noexcept
Returns
The day of the week for this date.

References dayOfWeek().

Referenced by dayOfWeek(), dayOfWeekStr(), dayOfWeekStr(), and monthCalendar().

◆ dayOfWeek() [2/2]

eWeekDays cx::cxDate::dayOfWeek ( int  pYear,
int  pMonth,
int  pDay 
)
staticnoexcept
Returns
The day of the week for the given year/month/day.

◆ dayOfWeekStr() [1/3]

string cx::cxDate::dayOfWeekStr ( ) const
Returns
The day-of-week name as a string (e.g. "Monday").

References dayOfWeek(), and dayOfWeekStr().

Referenced by dayOfWeekStr(), dayOfWeekStr(), and cx::operator<<().

◆ dayOfWeekStr() [2/3]

string cx::cxDate::dayOfWeekStr ( eWeekDays  pWeekDay)
static
Returns
The name of pWeekDay as a string (e.g. "Monday").

References cx::eFRIDAY, cx::eMONDAY, cx::eSATURDAY, cx::eSUNDAY, cx::eTHURSDAY, cx::eTUESDAY, and cx::eWEDNESDAY.

◆ dayOfWeekStr() [3/3]

string cx::cxDate::dayOfWeekStr ( int  pYear,
int  pMonth,
int  pDay 
)
static
Returns
The day-of-week name for the given year/month/day.

References dayOfWeek(), and dayOfWeekStr().

◆ fromJulian() [1/2]

void cx::cxDate::fromJulian ( long  pJulianDate)
noexcept

Sets this date from a Julian day number.

◆ fromJulian() [2/2]

void cx::cxDate::fromJulian ( long  pJulianDate,
int &  pYear,
int &  pMonth,
int &  pDay 
)
staticnoexcept

Converts pJulianDate to year/month/day components.

◆ fromString()

bool cx::cxDate::fromString ( std::string_view  pDateStr)

Parses pDateStr and updates this date.

Returns
True on success; false if the string could not be parsed.

References cx::CXDATE_DEFAULT_YEAR, cx::CXDATE_YEAR_2DIGIT_LAST_CENT, cx::DD_MM_YY, cx::DD_MM_YYYY, cx::DDMMYY, cx::DDMMYYYY, cx::MM_DD_YY, cx::MM_DD_YYYY, cx::MMDDYY, cx::MMDDYYYY, cx::YY_MM_DD, cx::YYMMDD, cx::YYYY_MM_DD, and cx::YYYYMMDD.

Referenced by cxDate(), cxDate(), cx::operator>>(), and tryParse().

◆ getCurrentYear()

int cx::cxDate::getCurrentYear ( )
staticnoexcept
Returns
The current 4-digit year.

◆ getDateFormat() [1/2]

eDateFormats cx::cxDate::getDateFormat ( ) const
inlinenoexcept
Returns
The date format used by toString() and stream output.

Referenced by cxDate().

◆ getDateFormat() [2/2]

eDateFormats cx::cxDate::getDateFormat ( std::string_view  pDateStr)
static
Returns
The date format detected from pDateStr, or UNKNOWN.

References cx::DD_MM_YY, cx::DD_MM_YYYY, cx::DDMMYY, cx::MM_DD_YY, cx::MM_DD_YYYY, cx::MMDDYY, cx::UNKNOWN, cx::YYYY_MM_DD, and cx::YYYYMMDD.

◆ getDateFormatStr()

string cx::cxDate::getDateFormatStr ( eDateFormats  pDateFormat)
static

◆ getDay()

int cx::cxDate::getDay ( ) const
inlinenoexcept
Returns
The day-of-month component of the date (1–31).

Referenced by cx::cxDatePicker::cxDatePicker(), cx::cxDatePicker::setDate(), test_Arithmetic(), and test_Constructors().

◆ getDisplayLong()

bool cx::cxDate::getDisplayLong ( ) const
inlinenoexcept
Returns
True if the date is formatted in long human-readable form.

◆ getMonth()

int cx::cxDate::getMonth ( ) const
inlinenoexcept
Returns
The month component of the date (1–12).

Referenced by cx::cxDatePicker::setDate(), cx::cxDatePicker::showModal(), test_Arithmetic(), and test_Constructors().

◆ getSepChar()

char cx::cxDate::getSepChar ( ) const
inlinenoexcept
Returns
The separator character used in formatted date strings (e.g. '-').

◆ getToday()

string cx::cxDate::getToday ( bool  pLong = false)
static

Returns today's date as "YYYY-MM-DD", or in long format if pLong=true.

References setDisplayLong(), and toString().

◆ getYear()

int cx::cxDate::getYear ( ) const
inlinenoexcept
Returns
The 4-digit year component of the date.

Referenced by cx::cxDatePicker::showModal(), test_Arithmetic(), and test_Constructors().

◆ isLeapYear() [1/2]

bool cx::cxDate::isLeapYear ( ) const
noexcept
Returns
True if the current year is a leap year.

References isLeapYear().

Referenced by isLeapYear().

◆ isLeapYear() [2/2]

bool cx::cxDate::isLeapYear ( int  pYear)
staticnoexcept
Returns
True if pYear is a leap year.

◆ monthCalendar()

void cx::cxDate::monthCalendar ( int  pYear,
int  pMonth,
std::ostream &  pOutStream = std::cout,
int  pTrailingEndlines = 2 
)
static

Prints a one-month calendar to pOutStream.

Parameters
pTrailingEndlinesNumber of blank lines printed after the calendar.

References dayOfWeek(), monthName(), and numMonthDays().

Referenced by yearCalendar().

◆ monthName()

string cx::cxDate::monthName ( int  pMonth,
bool  pAbbreviated = false 
)
static
Returns
The full or abbreviated name of pMonth (1–12).
Parameters
pAbbreviatedIf true, returns a 3-letter abbreviation.

Referenced by monthCalendar(), cx::cxDatePicker::setDate(), and cx::cxDatePicker::showModal().

◆ numMonthDays() [1/2]

int cx::cxDate::numMonthDays ( ) const
noexcept
Returns
The number of days in the current month.

References numMonthDays().

Referenced by monthCalendar(), and numMonthDays().

◆ numMonthDays() [2/2]

int cx::cxDate::numMonthDays ( int  pMonth,
int  pYear 
)
staticnoexcept
Returns
The number of days in pMonth of pYear, accounting for leap years.

References cx::CXDATE_DEFAULT_YEAR, and cx::CXDATE_YEAR_2DIGIT_LAST_CENT.

◆ operator!=()

bool cx::cxDate::operator!= ( const cxDate pThat) const
noexcept
Returns
True if this date differs from pThat.

◆ operator+()

cxDate cx::cxDate::operator+ ( int  pNumDays) const
noexcept
Returns
A new date that is pNumDays days after this date.

References addDays().

◆ operator++() [1/2]

cxDate & cx::cxDate::operator++ ( )
noexcept

References addDays().

◆ operator++() [2/2]

cxDate cx::cxDate::operator++ ( int  )
noexcept

◆ operator+=()

void cx::cxDate::operator+= ( int  pNumDays)
noexcept

Adds pNumDays days in-place.

◆ operator-() [1/2]

int cx::cxDate::operator- ( const cxDate pThat) const
noexcept
Returns
The signed number of days between this date and pThat.

◆ operator-() [2/2]

cxDate cx::cxDate::operator- ( int  pNumDays) const
noexcept
Returns
A new date that is pNumDays days before this date.

References subtractDays().

◆ operator--() [1/2]

cxDate & cx::cxDate::operator-- ( )
noexcept

References subtractDays().

◆ operator--() [2/2]

cxDate cx::cxDate::operator-- ( int  )
noexcept

◆ operator-=()

void cx::cxDate::operator-= ( int  pNumDays)
noexcept

Subtracts pNumDays days in-place.

◆ operator<()

bool cx::cxDate::operator< ( const cxDate pThat) const
noexcept
Returns
True if this date is earlier than pThat.

◆ operator<=()

bool cx::cxDate::operator<= ( const cxDate pThat) const
noexcept
Returns
True if this date is earlier than or equal to pThat.

◆ operator=()

cxDate & cx::cxDate::operator= ( const cxDate )
default

◆ operator==()

bool cx::cxDate::operator== ( const cxDate pThat) const
noexcept
Returns
True if this date equals pThat.

◆ operator>()

bool cx::cxDate::operator> ( const cxDate pThat) const
noexcept
Returns
True if this date is later than pThat.

◆ operator>=()

bool cx::cxDate::operator>= ( const cxDate pThat) const
noexcept
Returns
True if this date is later than or equal to pThat.

◆ setDate()

bool cx::cxDate::setDate ( int  pYear,
int  pMonth,
int  pDay 
)
noexcept

Sets year, month, and day simultaneously.

Returns
true if the date is valid; false if values were out of range.

Referenced by cx::cxDatePicker::cxDatePicker().

◆ setDateFormat()

void cx::cxDate::setDateFormat ( eDateFormats  pDateFormat)
noexcept

Sets the date format used by toString() and stream output.

References cx::UNKNOWN.

◆ setDay()

void cx::cxDate::setDay ( int  pDay)
noexcept

Sets the day-of-month component, clamped to the valid range for the current month.

◆ setDisplayLong()

void cx::cxDate::setDisplayLong ( bool  pDisplay)
inlinenoexcept

Enables or disables long human-readable date output.

Referenced by getToday().

◆ setMonth()

void cx::cxDate::setMonth ( int  pMonth)
noexcept

Sets the month component, clamped to [1, 12].

◆ setSepChar()

void cx::cxDate::setSepChar ( char  pSepChar)
inlinenoexcept

Sets the separator character used in formatted date strings.

◆ setYear()

void cx::cxDate::setYear ( int  pYear)
noexcept

Sets the year component, clamped to [CXDATE_YEAR_MIN, CXDATE_YEAR_MAX].

◆ subtractDays()

void cx::cxDate::subtractDays ( int  pNumDays)
noexcept

Subtracts pNumDays days from the date.

Referenced by operator-(), operator--(), and test_Arithmetic().

◆ subtractMonths()

void cx::cxDate::subtractMonths ( int  pNumMonths)
noexcept

Subtracts pNumMonths months from the date, adjusting the year as needed.

◆ subtractYears()

void cx::cxDate::subtractYears ( int  pNumYears)
noexcept

Subtracts pNumYears years from the date.

◆ today()

cxDate cx::cxDate::today ( )
staticnoexcept

C++17 addition: returns today's date as a cxDate object. More expressive than the string-based getToday().

Referenced by cx::cxDatePicker::cxDatePicker().

◆ toJulian() [1/2]

long cx::cxDate::toJulian ( ) const
noexcept
Returns
This date as a Julian day number.

References toJulian().

Referenced by toJulian().

◆ toJulian() [2/2]

long cx::cxDate::toJulian ( int  pYear,
int  pMonth,
int  pDay 
)
staticnoexcept
Returns
The Julian day number for the given year/month/day.

◆ toString()

string cx::cxDate::toString ( ) const
Returns
The date as a formatted string (format determined by mDateFormat).

Referenced by cxDatePickerTest(), and getToday().

◆ tryParse()

std::optional< cxDate > cx::cxDate::tryParse ( std::string_view  pDateStr,
eDateFormats  pDateFormat = YYYY_MM_DD,
char  pSepChar = '-' 
)
static

C++17 addition: tries to parse a date string, returning an std::optional<cxDate> so the caller can check success without exceptions or sentinel values.

References fromString().

◆ yearCalendar()

void cx::cxDate::yearCalendar ( int  pYear,
std::ostream &  pOutStream = std::cout,
int  pTrailingEndlines = 2 
)
static

Prints a full-year calendar to pOutStream.

Parameters
pTrailingEndlinesNumber of blank lines printed after the calendar.

References monthCalendar().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  pOS,
const cxDate pDate 
)
friend

Writes the formatted date to pOS.

◆ operator>>

std::istream & operator>> ( std::istream &  pIS,
cxDate pDate 
)
friend

Reads a formatted date from pIS.


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