Evo C++ Library v0.5.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Date Struct Reference

#include <evo/time.h>

Detailed Description

Structure holding a calendar date.

Example
#include <evo/time.h>
#include <evo/io.h>
using namespace evo;
int main() {
Date date(1999, 12, 31);
date.add_days(1);
date.format(con().out) << NL;
return 0;
}

Output:

2000-01-01

Public Member Functions

 Date ()
 Constructor. More...
 
 Date (int year, int month, int day)
 Constructor to initialize with date. More...
 
 Date (const Date &src)
 Copy constructor. More...
 
bool add_days (int days)
 Add days to current date, subtract if negative. More...
 
bool add_months (int months)
 Add months to current date, subtract if negative. More...
 
bool add_years (int years)
 Add years to current date, subtract if negative. More...
 
int compare (const Date &oth) const
 Compare to another date. More...
 
template<class T >
T & format (T &out, char delim='-') const
 Format date to String or Stream. More...
 
template<class T >
T & format_yearday (T &out, char delim='-')
 Format ordinal date to String or Stream. More...
 
ulong get_jdn () const
 Get Julian Day Number for current date. More...
 
bool operator!= (const Date &oth) const
 Compare for inequality with another date. More...
 
bool operator< (const Date &oth) const
 Compare whether less than another date. More...
 
bool operator<= (const Date &oth) const
 Compare whether less than or equal to another date. More...
 
Dateoperator= (const Date &src)
 Assignment operator. More...
 
bool operator== (const Date &oth) const
 Compare for equality with another date. More...
 
bool operator> (const Date &oth) const
 Compare whether greater than another date. More...
 
bool operator>= (const Date &oth) const
 Compare whether greater than or equal to another date. More...
 
bool parse (const SubString &str)
 Set date from parsing standard date string (used internally). More...
 
bool parse_std_impl (const char *&inp, const char *end)
 Set date from parsing standard date string (used internally). More...
 
void set ()
 Set all fields to 0, which is not a valid date. More...
 
bool set (int new_year, int new_month, int new_day)
 Set new date fields. More...
 
bool set_jdn (ulong jdn)
 Set date converted from Julian Day Number. More...
 
void set_local ()
 Set to current date in local time zone. More...
 
void set_utc ()
 Set to current date in UTC. More...
 
bool validate () const
 Validate current date. More...
 

Static Public Member Functions

static ulong calc_jdn (int year, int month, int day)
 Calculate Julian Day Number from date fields. More...
 
static int days_per_month (int month, bool leap_year)
 Helper to get number of days per given month. More...
 
static int days_per_month (int month, int year)
 Helper to get number of days per given month in given year. More...
 
static bool is_leap_year (int year)
 Helper to check whether given year is a leap year. More...
 
static SubString month_name (int month)
 Get name of given month. More...
 
static SubString month_name3 (int month)
 Get abbreviated name of given month. More...
 

Public Attributes

int day
 Day of month (1 - 31) More...
 
int month
 Month of year (1 - 12) More...
 
int year
 4 digit year (1000 - 9999) More...
 

Static Public Attributes

static const int DAY_MAX = 31
 Max day for any month – see days_per_month() More...
 
static const int DAY_MIN = 1
 First day per month. More...
 
static const ulong JDN_MIN = 2086302
 Minimum Julian Day Number for Jan 1, 1000. More...
 
static const ulong JDN_MOD = 2400000
 Julian Day Number for Modified Julian Day, subtract from JDN to get MJD (smaller number), add back for normal JDN. More...
 
static const int MONTH_MAX = 12
 Last month per year. More...
 
static const int MONTH_MIN = 1
 First month per year. More...
 
static const int YEAR_MAX = 9999
 Maximum year for 4 digits. More...
 
static const int YEAR_MIN = 1000
 Minimum year for 4 digits. More...
 

Constructor & Destructor Documentation

◆ Date() [1/3]

Date ( )
inline

Constructor.

◆ Date() [2/3]

Date ( int  year,
int  month,
int  day 
)
inline

Constructor to initialize with date.

  • This does not validate the fields, use valid_date() to check this
Parameters
year4 digit year
monthMonth of year
dayDay of month

◆ Date() [3/3]

Date ( const Date src)
inline

Copy constructor.

Parameters
srcSource to copy

Member Function Documentation

◆ add_days()

bool add_days ( int  days)
inline

Add days to current date, subtract if negative.

  • This adds the given days and adjusts months and years accordingly
Parameters
daysNumber of days to add, negative to subtract
Returns
Whether successful, false if current date is invalid before or after modification

◆ add_months()

bool add_months ( int  months)
inline

Add months to current date, subtract if negative.

  • This adds the given months and adjusts day of month to last day of month if not valid for the new year and month
Parameters
monthsNumber of months to add, negative to subtract
Returns
Whether successful, false if current date is invalid before or after modification

◆ add_years()

bool add_years ( int  years)
inline

Add years to current date, subtract if negative.

  • This adds the given years and adjusts day of month to last day of month if not valid for the new year and month
Parameters
yearsNumber of years to add, negative to subtract
Returns
Whether successful, false if current date is invalid before or after modification

◆ calc_jdn()

static ulong calc_jdn ( int  year,
int  month,
int  day 
)
inlinestatic

Calculate Julian Day Number from date fields.

  • This does not validate the input
  • Notes:
    • Julian Days normally begin at noon, but this uses it purely as a date and pretends they begin at midnight – time of day can be tracked separately
    • Not related to the Julian calendar
Parameters
yearYear to use (1000 - 9999)
monthMonth of year to use (1 - 12)
dayDay of month to use (1 - 31)
Returns
Julian Day Number for date

◆ compare()

int compare ( const Date oth) const
inline

Compare to another date.

Parameters
othOther date to compare to
Returns
Result (<0 if this is less, 0 if equal, >0 if this is greater)

◆ days_per_month() [1/2]

static int days_per_month ( int  month,
bool  leap_year 
)
inlinestatic

Helper to get number of days per given month.

Parameters
monthMonth to get number of days of (1 - 12)
leap_yearWhether this is for a leap year, which affects the result for month=2
Returns
Number of days in month

◆ days_per_month() [2/2]

static int days_per_month ( int  month,
int  year 
)
inlinestatic

Helper to get number of days per given month in given year.

Parameters
monthMonth to get number of days of (1 - 12)
yearYear for month, used to determine whether a leap year applies for month=2
Returns
Number of days in month

◆ format()

T& format ( T &  out,
char  delim = '-' 
) const
inline

Format date to String or Stream.

  • Date syntax is based on ISO 8601:
    • YYYY-MM-DD – with default delimiter, other delimiters may be use but aren't considered standard
    • YYYYMMDD – with no delimiter
  • See also: format_yearday()
Template Parameters
TString/Stream type, inferred from argument
Parameters
outOutput String/Stream to format to
delimDelimiter to use between fields, 0 for none
Returns
Reference to out

◆ format_yearday()

T& format_yearday ( T &  out,
char  delim = '-' 
)
inline

Format ordinal date to String or Stream.

  • Date syntax is based on ISO 8601 ordinal date:
    • YYYY-DDD – ordinal date with default delimiter, other delimiters may be use but aren't considered standard
    • YYYYDDD – ordinal date with no delimiter
  • See also: format()
Template Parameters
OutputString/Stream type, inferred from argument
Parameters
outOutput String/Stream to format to
delimDelimiter to use between fields, 0 for none
Returns
Reference to out

◆ get_jdn()

ulong get_jdn ( ) const
inline

Get Julian Day Number for current date.

  • This is useful for turning a date into a single number in day units that is easy to add/subtract and convert back to a date
  • Call set_jdn() to convert a JDN back into date fields
  • For a Modified JDN: subtract JDN_MOD from a valid (non-zero) result, then add it back again
  • Notes:
    • Julian Days normally begin at noon, but this uses it purely as a date and pretends they begin at midnight – time of day can be tracked separately
    • Not related to the Julian calendar
Returns
Julian Day Number for current date, 0 if current date is not valid

◆ is_leap_year()

static bool is_leap_year ( int  year)
inlinestatic

Helper to check whether given year is a leap year.

  • Leap years are years divisible by 400, or years divisible by 4 but not divisible by 100
Parameters
yearYear to check
Returns
Whether a leap year

◆ month_name()

static SubString month_name ( int  month)
inlinestatic

Get name of given month.

  • String is always static/immutable and terminated
Parameters
monthMonth to get name of (1 - 12)
Returns
Name of month, null if invalid month

◆ month_name3()

static SubString month_name3 ( int  month)
inlinestatic

Get abbreviated name of given month.

  • String is always static/immutable and terminated
Parameters
monthMonth to get name of (1 - 12)
Returns
Name of month, null if invalid month

◆ operator!=()

bool operator!= ( const Date oth) const
inline

Compare for inequality with another date.

Parameters
othOther date to compare to
Returns
Whether inequal

◆ operator<()

bool operator< ( const Date oth) const
inline

Compare whether less than another date.

Parameters
othOther date to compare to
Returns
Whether less than

◆ operator<=()

bool operator<= ( const Date oth) const
inline

Compare whether less than or equal to another date.

Parameters
othOther date to compare to
Returns
Whether less than or equal

◆ operator=()

Date& operator= ( const Date src)
inline

Assignment operator.

Parameters
srcSource to copy
Returns
This

◆ operator==()

bool operator== ( const Date oth) const
inline

Compare for equality with another date.

Parameters
othOther date to compare to
Returns
Whether equal

◆ operator>()

bool operator> ( const Date oth) const
inline

Compare whether greater than another date.

Parameters
othOther date to compare to
Returns
Whether greater than

◆ operator>=()

bool operator>= ( const Date oth) const
inline

Compare whether greater than or equal to another date.

Parameters
othOther date to compare to
Returns
Whether greater than or equal

◆ parse()

bool parse ( const SubString str)
inline

Set date from parsing standard date string (used internally).

  • Supported date syntax is based on ISO 8601:
    • YYYY-MM-DD – full date with delimiter
    • YYYYMMDD – shorter version without delimiters
    • YYYY-DDD – ordinal date with delimiter, with 4 digit year (1000 - 999) and day of year (1 - 366)
    • YYYYDDD – ordinal date, shorter version without delimiters
    • Alternate delimiters supported: SPACE, SLASH (/), PERIOD (.)
Parameters
strString to parse
Returns
Whether successful, false on bad input or unrecognized format

◆ parse_std_impl()

bool parse_std_impl ( const char *&  inp,
const char *  end 
)
inline

Set date from parsing standard date string (used internally).

  • Supported date syntax is based on ISO 8601:
    • YYYY-MM-DD – full date with delimiter
    • YYYYMMDD – shorter version without delimiters
    • YYYY-DDD – ordinal date with delimiter, with 4 digit year (1000 - 999) and day of year (1 - 366)
    • YYYYDDD – ordinal date, shorter version without delimiters
    • Alternate delimiters supported: SPACE, SLASH (/), PERIOD (.)
Parameters
inpStart parsing pointer (must be <= end), set to stop position [in/out]
endEnd of string pointer, must not be NULL
Returns
Whether successful, false on bad input or unrecognized format

◆ set() [1/2]

void set ( )
inline

Set all fields to 0, which is not a valid date.

◆ set() [2/2]

bool set ( int  new_year,
int  new_month,
int  new_day 
)
inline

Set new date fields.

  • This does validate the fields before setting them
Parameters
new_yearNew year value
new_monthNew month value
new_dayNew day value
Returns
Whether successful, false if one of the fields is invalid

◆ set_jdn()

bool set_jdn ( ulong  jdn)
inline

Set date converted from Julian Day Number.

  • Call get_jdn() to convert date into a JDN first – this is useful for turning a date into a single number in day units that is easy to add/subtract
  • For a Modified JDN: subtract JDN_MOD from a valid (non-zero) result, then add it back again before calling this
  • Notes:
    • Julian Days normally begin at noon, but this uses it purely as a date and pretends they begin at midnight – time of day can be tracked separately
    • Not related to the Julian calendar
Parameters
jdnJulian Day Number to set
Returns
Whether successful, false if JDN is invalid (0 or less than JDN_MIN)

◆ set_local()

void set_local ( )
inline

Set to current date in local time zone.

◆ set_utc()

void set_utc ( )
inline

Set to current date in UTC.

◆ validate()

bool validate ( ) const
inline

Validate current date.

Returns
Whether date is valid, false if any field is invalid (out of range)

Member Data Documentation

◆ day

int day

Day of month (1 - 31)

◆ DAY_MAX

const int DAY_MAX = 31
static

Max day for any month – see days_per_month()

◆ DAY_MIN

const int DAY_MIN = 1
static

First day per month.

◆ JDN_MIN

const ulong JDN_MIN = 2086302
static

Minimum Julian Day Number for Jan 1, 1000.

◆ JDN_MOD

const ulong JDN_MOD = 2400000
static

Julian Day Number for Modified Julian Day, subtract from JDN to get MJD (smaller number), add back for normal JDN.

◆ month

int month

Month of year (1 - 12)

◆ MONTH_MAX

const int MONTH_MAX = 12
static

Last month per year.

◆ MONTH_MIN

const int MONTH_MIN = 1
static

First month per year.

◆ year

int year

4 digit year (1000 - 9999)

◆ YEAR_MAX

const int YEAR_MAX = 9999
static

Maximum year for 4 digits.

◆ YEAR_MIN

const int YEAR_MIN = 1000
static

Minimum year for 4 digits.


The documentation for this struct was generated from the following file: